• Resolved iversoncru

    (@iversoncru)


    Hi, i want to display my blog in a different page, not in index, and i want to show only the posts on a certain category.
    I placed ‘cat=7’ but it is not working, it shows all the post as the category was not specify :S

    This is my code:

    <article>
    	<?php
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query('cat=7');
    	$wp_query->query('showposts=5' . '&paged='.$paged);
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    	?>
    	<div class="post">
    		<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    		<p><?php echo substr(get_the_excerpt(), 0,300); ?></p>
    	</div>
    	<?php endwhile; ?>
    
    	<?php if ($paged > 1) { ?>
    	<nav id="nav-posts">
    	<div><?php next_posts_link(' Previous'); ?></div>
    	<div><?php previous_posts_link('Next'); ?></div>
    	</nav>
    	<?php } else { ?>
    		<nav id="nav-posts">
    		<div><?php next_posts_link('Previous '); ?></div>
    	        </nav>
    	<?php } ?>
    
    	<?php wp_reset_postdata(); ?>
    </article>

    Thanks a lot for your time.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    <article>
    	<?php
    	$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query();
    	$args = array(
    		'posts_per_page' => 5,
    		'cat'            => 7,
    		'paged'          => $paged,
    	);
    	$wp_query->query( $args );
    	while ( $wp_query->have_posts() ) : $wp_query->the_post();
    ?>
    	<div class="post">
    		<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    		<p><?php echo substr( get_the_excerpt(), 0, 300 ); ?></p>
    	</div>
    	<?php endwhile; ?>
    
    	<?php if ( $paged > 1 ) { ?>
    	<nav id="nav-posts">
    	<div><?php next_posts_link( ' Previous' ); ?></div>
    	<div><?php previous_posts_link( 'Next' ); ?></div>
    	</nav>
    	<?php } else { ?>
    		<nav id="nav-posts">
    		<div><?php next_posts_link( 'Previous ' ); ?></div>
    	        </nav>
    	<?php } ?>
    	<?php wp_reset_postdata(); ?>
    	<?php $wp_query = null; $wp_query = $temp; ?>
    </article>

    Thread Starter iversoncru

    (@iversoncru)

    keesiemeijer, thanks a lot, that works perfectly.
    Anyway, I was checking other posts, and i found this, which helped, although it does not use wp_query.

    http://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved?replies=37

    <article>
    	<?php
    		$args = array(
    		'cat' => 7,
    		'posts_per_page' => 5,
    		'paged' => $paged
    	);
    	query_posts($args);
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    	?>
    	<div class="post">
    		<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    		<p><?php echo substr(get_the_excerpt(), 0,300); ?></p>
    	</div>
    	<?php endwhile; ?>
    	<?php if ($paged > 1) { ?>
    	<nav id="nav-posts">
    		<div><?php next_posts_link(' Previous'); ?></div>
    		<div><?php previous_posts_link('Next'); ?></div>
    	</nav>
    	<?php } else { ?>
    	<nav id="nav-posts">
    		<div><?php next_posts_link('Previous '); ?></div>
    	</nav>
    	<?php } ?>
    	<?php wp_reset_postdata(); ?>
    </article>
    Moderator keesiemeijer

    (@keesiemeijer)

    $wp_query->query( $args ); is essentially the same as query_posts($args);
    To clean up after a call to query_posts, make a call to wp_reset_query() instead of wp_reset_postdata();

    I’m glad you’ve got it resolved.

    Thread Starter iversoncru

    (@iversoncru)

    thanks for all

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show posts by a category not working’ is closed to new replies.