Support » Plugins » Hacks » Cycle through Single Posts of a particular category

  • Is there a way to hack this code so it only cycles through posts, right and left buttons added, in a particular category such as ‘news’

    <?php get_header(); ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
       		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<?php if ( has_post_thumbnail() ) { ?>			
    
                 <?php } ?>                   
    
           			<div class="gridly-copy">
                    <h1><?php the_title(); ?> </h1>
    
               	<div class="detail"><?php the_content(); ?></div>
                     <?php the_tags(); ?>
    
                    <div class="clear"></div>
    				<?php comments_template(); ?>
                    </div>
    
           </div>
    
    		<?php endwhile; endif; ?>
    
           <div class="post-nav">
                   <div class="post-prev"><?php previous_post_link('%link'); ?> </div>
    			   <div class="post-next"><?php next_post_link('%link'); ?></div>
            </div>      
    
    <?php get_footer(); ?>

    or alternatively change it to a foreach array to cycle through only posts with these parameters

    <?php $args = array(
        'numberposts'     => 50,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'suppress_filters' => 'true',
    	'meta_query' => array(
    		array(
    			'key' => 'news',
    			'value' => 'no',
    		) )
    	); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Try using the query_posts() function right after the call to get_header() and pass in the $args array as parameter like this:

    <?php get_header(); ?>
    
    	<?php
    	$args = array(
    	    'numberposts'     => 50,
    	    'offset'          => 0,
    	    'orderby'         => 'post_date',
    	    'order'           => 'DESC',
    	    'post_type'       => 'post',
    	    'post_status'     => 'publish',
    	    'suppress_filters' => 'true',
    		'meta_query' => array(
    			array(
    				'key' => 'news',
    				'value' => 'no',
    			) )
    		);
    	query_posts( $args );
    	?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
       		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<?php if ( has_post_thumbnail() ) { ?>			
    
                 <?php } ?>

    Just remember though, this will alter the whole loop used in the page.
    http://codex.wordpress.org/Function_Reference/query_posts

    No that wont do it as it just displays all the posts on the page where as I want them to scroll through using the Post next and post Prev buttons I have.

    Moderator bcworkz

    (@bcworkz)

    I wonder if you could temporarily change the posts per page option to 1 to get the effect you want. No idea if this would work, just a thought.

    All the posts are available as $wp_query->posts. If you manage the current post yourself depending on the use of prev and next buttons, you could do $post = $wp_query->posts[$current]; and output with echo $post->post_content;, etc.

    In the $args array, change the numberposts key to posts_per_page and set this to 1.

    This works in displaying one post but doesn’t cycle through the remanding posts, any ideas?

    Plus its displaying only the most recent post

    Basically this code below cycles works, it cycles through all the posts but I want it to omit posts in category ‘news’ any help would be appreciated

    <?php get_header(); ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
       		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<?php if ( has_post_thumbnail() ) { ?>			
    
                 <?php } ?>
    
           			<div class="gridly-copy">
                    <h1><?php the_title(); ?> </h1>
    
               	<div class="detail"><?php the_content(); ?></div>
                     <?php the_tags(); ?>
    
                    <div class="clear"></div>
    				<?php comments_template(); ?>
                    </div>
    
           </div>
    
    		<?php endwhile; endif; ?>
    
           <div class="post-nav">
                   <div class="post-prev"><?php previous_post_link('%link'); ?> </div>
    			   <div class="post-next"><?php next_post_link('%link'); ?></div>
            </div>      
    
    <?php get_footer(); ?>
    Moderator bcworkz

    (@bcworkz)

    I had a feeling the page length trick was too easy :/

    To exclude news, you’ll need to determine the id of the news category. You could var_dump() the array returned by get_the_category() of a news post to get it. Let’s say it was ‘3’. Then do something like this:

    <?php
      query_posts( 'cat=-3' );
      if (have_posts()) : while ....

    which means you must also do this:

    <?php endwhile; endif;
      wp_reset_query(); ?>

    If you have a very large database, this method is inefficient, in which case you should review Plugin API/Action Reference/pre get posts

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Cycle through Single Posts of a particular category’ is closed to new replies.