我的主要投资组合页面设置正确,显示所有工作,我遇到问题的下一步是仅显示特定类别的工作。
在自定义内容类型管理器下,我检查了"启用类别"在Taxonomies下,我可以控制向内容类型添加类别,如下面的URL。
例如:http://localhost/category/narrative
会显示所有类别的作品"叙述"附上,现在它显示了我复制后的所有工作。粘贴了工作页面中的代码。
如何获取此category.php模板以检测并显示与其加载的类别相关的工作?
<?php
/**
* The template for displaying Category Archive pages
*
* @package WordPress
*/
$res = get_posts(array('post_type' => 'work', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1));
get_header(); ?>
<section role="main" class="container">
<div id="da-thumbs" class="row work-list da-thumbs">
<? foreach($res as $post) : setup_postdata($post) ?>
<?
$thumbnail = get_custom_field('thumbnail');
?>
<div class="col four">
<a href="<?php echo get_permalink(); ?>">
<img src="<?=$thumbnail?>" />
<div><span><?php the_title( '<h3>', '</h3>' ); ?></span></div>
</a>
</div>
<? endforeach; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('(more...)')); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</section>
<?php get_footer(); ?>
答案 0 :(得分:1)
您的get_posts未按类别进行过滤。
做类似的事情:
$the_category = get_queried_object();
$cat_id = $the_category->term_id;
$res = get_posts(array('category' => $cat_id, 'post_type' => 'work', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1));