【问题标题】:How to display posts using custom texonomy in wordpress?如何在 wordpress 中使用自定义分类法显示帖子?
【发布时间】:2015-10-15 15:26:15
【问题描述】:

我已经在我的 wordpress 博客中安装了 Wp-Types 插件。我创建了一个名为“演示”的自定义分类法。我在其中添加了两个类别。

  • 第一个演示
  • 第二个演示

现在我想显示来自这个自定义分类的帖子,所以我使用这个:

<?php


$custom_terms = get_terms('demo');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'demo',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        endwhile;
     }
}


?>

但它显示了所有使用该分类法的帖子。我想显示来自 seconddemo 的帖子。怎么办?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在您的数组中,您缺少'terms' =&gt; array('seconddemo')

    <?php
    
    
    $custom_terms = get_terms('demo');
    
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'demo',
                    'field' => 'slug',
                    'terms' => array('seconddemo'),
                ),
            ),
         );
    
         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            echo '<h2>'.$custom_term->name.'</h2>';
    
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
            endwhile;
         }
    }
    
    
    ?>
    

    【讨论】:

    • 不确定你的terms' =&gt; $custom_term-&gt;slug 应该做什么。
    • 它仍然无法工作......它显示相同的输出
    • 删除该 Foreach 循环条件并尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2013-03-08
    相关资源
    最近更新 更多