wordpress自定义内容类型管理器 - 来自特定类别的get_posts

时间:2014-09-17 21:46:00

标签: php wordpress custom-post-type

我的主要投资组合页面设置正确,显示所有工作,我遇到问题的下一步是仅显示特定类别的工作。

在自定义内容类型管理器下,我检查了"启用类别"在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(); ?>

1 个答案:

答案 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));