Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a difficult post a page problem. I have this code :

function wpse31701_4filter_query() { 
global $wp_query; 
// Check year and month 
$wp_query->set('orderby', 'date'); $wp_query->set('order', 'ASC'); 
if ( $wp_query->get( 'year' ) == 2005 and $wp_query->get( 'monthnum' ) == 9 ) 
    // Check page number to apply offset 
    if ( $wp_query->get( 'paged') == 2 ) $wp_query->set( 'offset', 1 ); 
    else 
     if ( $wp_query->get( 'paged') == 3 ) $wp_query->set( 'offset', 2 ); 
    else
      if ( $wp_query->get( 'paged') == 4 ) $wp_query->set( 'offset', 3 ); 
    else
    if ( $wp_query->get( 'paged') < 8  )  
       $wp_query->set( 'posts_per_page', 1); 

}

It's don't work because I get a 500 error. The problem I have it that on the first 8 pages only 1 article is displayed. On the rest the normal number of articles is displayed.

Who can help me find out how I get this working.

Roelof

share|improve this question
1  
Please use brackets and intend your code to make it readable. –  kaiser Oct 24 '11 at 12:45
    
Could you pls copy/paste the code from the other answer and update/edit yours? How shall later readers who search for the same solution read & understand your code? I'm close to closing this post as "too localized". –  kaiser Oct 24 '11 at 16:29

2 Answers 2

up vote 0 down vote accepted

"The problem I have it that on the first 8 pages only 1 article is displayed."

That would be due to this line of code:

if ( $wp_query->get( 'paged') < 8  )  {
  $wp_query->set( 'posts_per_page', 1);
}

See here for more info: codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

EDIT:

Try adding brackets as mentioned by @kaiser... Here's your original code you posted in pastebin with brackets/indentions added where I think they're supposed to go...

function wpse31701_filter_query() {
  global $wp_query;
  // Check year and month
  $wp_query->set('orderby', 'date'); $wp_query->set('order', 'ASC');
  if ( $wp_query->get( 'year' ) == 2005 and $wp_query->get( 'monthnum' ) == 4 ) {
    // Check page number to apply offset
    if ( $wp_query->get( 'paged') == 2 ) {
      $wp_query->set( 'offset', 1 );
    }
    else {
      if ( $wp_query->get( 'paged') == 3 ) {
        $wp_query->set( 'offset', 4 );
      }
      else {
        $wp_query->set( 'posts_per_page', 1);
      }
    }
  }
}
add_action( 'pre_get_posts', 'wpse31701_filter_query' ); 

Your first "if" statement had no brackets around what it was supposed to evaluate. If you code conditionals without brackets, then it's only going to evaluate the first line following it. If there's more than one line in the evaluation, you need to either use brackets or colons.

Now, I'm not sure if this is going to fix your problem... but it's at least a very good idea to properly format the code first before trying to debug any "problems", as the formatting very well may be one of, if not "the" problem.

share|improve this answer
    
I know it;s that line when I change it back to this : [link] (pastebin.com/Rrp3RwhM) then it works perfectly but only on page1 where I need it on page 2 and so on –  Roelof Wobbn Oct 24 '11 at 14:51
    
I see I can do the job with wp-query but I hope someone has a example how I then check which month is displayed –  Roelof Wobbn Oct 24 '11 at 15:54
    
See the edits above –  Nero_DCLXVI Oct 24 '11 at 16:10
    
Hello, using brackets and change it to this if ( $wp_query->get( 'paged') == 2 ) {$wp_query->set( 'offset', 1 ); $wp_query->set( 'posts_per_page', 1); } did the job –  Roelof Wobbn Oct 24 '11 at 16:49

First: The query var is named "page", not paged. Second: it is only set when you are on page > 1. Else it simply is empty.

share|improve this answer
    
I see I can do the job with wp-query but I hope someone has a example how I then check which month is displayed –  Roelof Wobbn Oct 24 '11 at 15:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.