0

I have a custom post type called 'portcat' and I want to display the results of only one portcat category ('games' - which has an ID of '3') on my page. The below code displays all of the categories and I'm not sure what I need to add to make it display only the 'games' category?

<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']  
            ) 
        );
    } 

Ok, I have done exactly as suggested above (except 'category_name' didn't work so am using 'cat_name'). It is still displaying all of the categories...I only want it to display 'Games'. I have included the rest of the code in the hope someone can easily spot what I'm missing?

        <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 Answers 2

1

Something like this should work but you haven't stated where you want to display the CPT pages for games.

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 );
0

This is the query you're looking for:

$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);
1
  • The above didn't work...I tried to include the rest of the code but the post got deleted for some reason. I simply just want the page to display the 'Games' category - what can I add to the above code to make it work?
    – user31114
    May 2, 2013 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.