Php 如何在wordpress中使用自定义texonomy显示帖子?
我已经在我的wordpress博客中安装了Wp类型插件。我创建了一个名为“demo”的自定义分类法。其中我添加了两个类别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' =>
- 第一次演示
- 第二次演示
<?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;
}
}
?>