【问题标题】:wordpress custom content types manager - get_posts from specific categorywordpress 自定义内容类型管理器 - 来自特定类别的 get_posts
【发布时间】:2014-09-18 10:53:09
【问题描述】:

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

在自定义内容类型管理器下,我选中了分类法下的“启用类别”,这使我可以控制向内容类型添加类别,如下面的 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(); ?>

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案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));
    

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多