Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress中使用自定义texonomy显示帖子?_Php_Wordpress - Fatal编程技术网

Php 如何在wordpress中使用自定义texonomy显示帖子?

Php 如何在wordpress中使用自定义texonomy显示帖子?,php,wordpress,Php,Wordpress,我已经在我的wordpress博客中安装了Wp类型插件。我创建了一个名为“demo”的自定义分类法。其中我添加了两个类别 第一次演示 第二次演示 现在我想显示来自此自定义分类法的帖子,因此我使用以下方法: <?php $custom_terms = get_terms('demo'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' =>

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

  • 第一次演示
  • 第二次演示
现在我想显示来自此自定义分类法的帖子,因此我使用以下方法:

<?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的帖子。怎么做?

在您的数组中缺少
'terms'=>数组('seconddemo')



不确定您的
术语=>$custom\u term->slug应该做什么。它仍然不起作用。。。它显示相同的输出删除Foreach循环条件并尝试它。
<?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;
     }
}


?>