Skip to main content
edited title
Link
Dejo Dekic
  • 909
  • 1
  • 11
  • 28

How "Right Way" to make custom Loop respect reading settings inside admin panel?

Source Link
Dejo Dekic
  • 909
  • 1
  • 11
  • 28

How to make custom Loop respect reading settings inside admin panel?

I have created custom Wordpress loop using WP_Query, loop is working fine and I can customize it nicely, however i have a problem using "Blog pages show at most" settings, for example inside my loop if i have added following argument 'post_per_page' =>-1 my loop is showing all posts but now i can't insert my custom number of posts inside admin panel if I enter "Blog pages show at most" 3 (three representing number of posts) they are ignored and loop is showing all posts.

So to overcome this problem (because i want to control number of posts showing inside admin reading settings)
I have done following : Created a blank value variable and passed it as an argument like this:

$show_my_posts = '';
$args = array(
  
     'paged' => $paged,
     'post_not_in' => $show_my_posts
     //added blank value variable in order to respect "Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

So now I can insert custom number of posts inside admin backend and my loop is behaving as i want (I enter "Blog pages show at most" and value as 3 ) and now my loop is displaying three posts as I want.

My question is: Am I doing this the "right way" or is there a better way for achieving that behavior? THX!!

My full loop looks like this:

<?php //enable pagination on static pages and blog pages
    $show_my_posts = '';
    $col = 1; //Let's create first column
    /*Let's add pagination to post page and static page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }
       
$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,
     'post_not_in' => $show_my_posts
             //added blank value variable in order to respect 
            //"Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>        

<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
    <div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">
                            
            <?php _e('Filed under&#58;','domain'); ?> <?php the_category(', ') ?> <?php _e('by','domain'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','domain'); ?><?php the_time('l, F jS, Y'); ?><br/>
            
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>
    
    <?php /*Enable Two Column Layout*/
        if($col==1) {
        $col=2;
        echo "</div>";
        }
        else if($col==2)  {
        $col=1;
         echo "</div>";
        }
                
    endwhile; ?>
    <div class="clear"></div>
    <div class="navigation">
    
    <?php
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
    ?>
</div>
<?php endif; ?> 
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>