[英]PHP: How to display posts of specific category using a custom query in WordPress?
[英]How to display posts using custom texonomy in wordpress?
我已经在我的wordpress博客中安装了Wp-Types插件。 我创建了一个称为“ 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' => 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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.