如何在自定义帖子类型中只显示一个类别?

时间:2013-05-02 作者:Sam

我有一个名为“portcat”的自定义帖子类型,我只想在我的页面上显示一个portcat类别(“games”-ID为“3”)的结果。下面的代码显示了所有类别,我不确定需要添加什么才能使其仅显示“游戏”类别?

<h4 class="nomar"><?php echo the_title(); ?></h4>
<div class="sep"></div>

    <?php echo $firstCat[0]->cat_name; ?>

    <div class="clear"></div>
    <ul class="blogpost_list columns3">

    <?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $args = array(
    \'post_type\' => \'port\',
    \'paged\' => $paged,
    \'posts_per_page\' => get_theme_option("portfolio_work_count"),                 
    );


    if (isset($_GET[\'slug\'])) {
    $args[\'tax_query\']=array(
            array(  
                \'taxonomy\' => \'portcat\',  
                \'field\' => \'slug\',  
                \'terms\' => $_GET[\'slug\']  
            ) 
        );
    } 
好的,我完全按照上面的建议做了(除了“category\\u name”不起作用,所以我用的是“cat\\u name”)。它仍在显示所有类别。。。我只想让它显示“游戏”。我已经包含了其余的代码,希望有人能很容易地发现我遗漏了什么?

        <div class="greyblock">
            <h4 class="nomar"><?php echo the_title(); ?></h4>
            <div class="sep"></div>

                <?php echo $firstCat[0]->cat_name; ?>

                <div class="clear"></div>
                <ul class="blogpost_list columns3">

                <?php
                $args = array(
                \'post_type\' => \'port\',
                \'paged\' => $paged,
                \'posts_per_page\' => get_theme_option("portfolio_work_count"),
                \'cat_name\' => \'games\'
                );
                $wp_query = new WP_Query($args);

                ?>
                <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
                #We have:
                #get_permalink() - Full url to post;
                #get_the_title() - Post title;
                #get_the_content() - Post text;
                #get_post_time(\'U\', true) - unix timestamp

                $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' );   


                echo "
                <li>
                        <center><img alt=\'".get_the_title()."\' src=\'".TIMTHUMBURL."?w=120&h=250&src=".$featured_image[0]."\'>
                        <h4>".get_the_title()."</h4></center>";
                        $terms = get_the_terms($post->ID, \'portcat\');
                        if ( $terms && ! is_wp_error( $terms ) ) {

                        $draught_links = array();

                        foreach ( $terms as $term ) {
                            $draught_links[] = $term->name;
                        }

                        $on_draught = join( ", ", $draught_links );
                        }

                echo "
                <p>".get_the_excerpt()."</p>
                    <center><a href=\'".get_permalink()."\' class=\'read\'>Read More</a></center>                       
                    <br class=\'clear\' />
                </li>
                ";  

                 endwhile; ?>

            </ul>
        </div>
        <?php get_pagination() ?>
        <?php $wp_query = null; $wp_query = $temp;  ?>  

2 个回复
SO网友:Brad Dalton

像这样的东西应该可以工作,但您还没有说明要在哪里显示游戏的CPT页面。

function display_games_archive( $query ) {

if ( !is_admin() || $query->is_main_query() && is_post_type_archive( \'portcat\' ) ) {

    $query->set( \'category__in\', \'games\' );
    return;
    }
}
add_action( \'pre_get_posts\', \'display_games_archive\', 1 );

SO网友:tomca32

这是您要查找的查询:

$args = array(
\'post_type\' => \'port\',
\'paged\' => $paged,
\'posts_per_page\' => get_theme_option("portfolio_work_count"),
\'category_name\' => \'games\'
);
$wp_query = new WP_Query($args);

结束

相关推荐

WP_QUERY返回的结果多于预期

我编写了一个查询,该查询应该返回名为“无图像”的所有附件(这是处理没有特征图像时图像回退的函数的一部分),如果我得到一些结果,该函数将返回第一项。代码如下:function misamee_get_image_placeholder($size, $icon = false, $attr = \'\') { $transient_key = \'image_placeholder_id\'; $result = get_transient($transient_ke