[英]Showing a single Category only on a Wordpress Custom Post Type page
I have made a custom post type with custom fields etc for a page on my website...Currently it works fine and shows all of the courses I have created: http://seedcreativehub.co.uk/book-seedcreativehub-courses/ 我在网站上的页面上创建了带有自定义字段等的自定义帖子类型...目前,它可以正常工作并显示我创建的所有课程: http : //seedcreativehub.co.uk/book-seedcreativehub-courses/
However, I would now only like the page to display one specific course category. 但是,我现在只希望页面显示一个特定的课程类别。
The code I'm using is below, and I thought it would be as simple as adding in 'category_name' => 'upcoming-event' underneath the post type, however it doesn't seem to showing anything at all... 我正在使用的代码如下,我认为这就像在帖子类型的下面添加'category_name'=>'upcoming-event'一样简单,但是似乎根本没有显示任何内容...
I can't think what I'm doing wrong, if someone could help, it would be greatly appreciated. 我不能认为我做错了什么,如果有人可以提供帮助,将不胜感激。
<?php
$args = array(
'post_type' => 'courses',
'category_name' => 'upcoming-event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-xs-3 course">
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php the_title(); ?>" graphic></a></p>
<h3><?php the_field('event_date'); ?></h3>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</div>
<?php $portfolio_count = $the_query->current_post + 1; ?>
<?php if ( $portfolio_count % 4 == 0): ?>
</div><div class="row">
<?php endif; ?>
<?php endwhile; endif; ?>
Thanks for looking, 感谢您的关注,
Shaun. 肖恩
You'll have to use the tax_query
parameter while querying posts based on taxonomy. 在根据分类法查询帖子时,必须使用
tax_query
参数。
<?php
$args = array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course-category', // Taxonomy name
'field' => 'slug',
'terms' => 'upcoming-event', // Term name
),
),
);
$the_query = new WP_Query( $args );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.