doulang6013 2019-07-04 15:31
浏览 65

基于自定义字段的术语名称(动态)

I want to get terms based on a custom field. I created some custom fields as terms that you can choose with a dropdown. For example: I want to display related posts to a single posts by choosing the term.

Thank you for your Help.

<?php
$terms = wp_get_post_terms( $post->ID, 'referenzen_kategorie'); 
$terms_ids = [];

foreach ( $terms as $term ) {
    $terms_ids[] = $term->term_id;
}

$args = array(
    'post_type' => 'referenzen',
    'orderby' => 'asc',
    'posts_per_page' => 3,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'referenzen_kategorie',
            'field'    => 'slug',
            'terms'    => bauberatung
        )
    ),
);                        

$query = new WP_Query($args);
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
?>
    <?php $query->the_post();?>
<?php } } ?> 

The code that I provide works but I want to replace "bauberatung" with a custom field so that it dynamically displays.

  • 写回答

1条回答 默认 最新

  • doulu6929 2019-07-04 17:16
    关注

    Yay, i got it work ;)

    Here my solution:

      <?php
    
                    $terms = get_field('your_custom_field'); 
    
                    $args = array(
                        'post_type' => 'your_post_type',
                        'orderby' => 'asc',
                        'posts_per_page' => 3,
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'your_taxonomy',
                                'field'    => 'slug',
                                'terms'    => $terms
                            )
                        ),
                    );                        
    
                    $query = new WP_Query($args);
                    if ( $query->have_posts() ) {
                        while ( $query->have_posts() ) {
                    ?>
    
    
                       <?php $query->the_post();?>
    

    With this your able get posts from a specific taxonomy -> term based on custom field (outside the loop), for Example to display to single Services posts related references.

    Hope you will find it helpful Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?