Categories and archives broken
-
Hello and thanks in advance.
I’m making a new design for my blog and I’ve implemented this code https://pastebin.com/f318db5f3, to feature a different number of posts in the homepage, but the archives and categories posts are being offset and I don’t know how to fix it. Does anyone have a solution?
-
Not at all sure, but this might be what you want (UNTESTED, but the intent is to only use offset when is_home() is true):
Sorry about the recoding, but I needed the extra braces for my editor to make sure I had all the if’s and else’s balanced.
<?php // If we're on the index page, we only want the two first posts. if ( is_home() ) { if ( !is_paged() ) { query_posts('posts_per_page=2'); // If we're within the index archives, we want ten posts per page as usual. } else { // If we are past the first page, offset by an appropriate amount for the page. // I'm by no means a math guru; I don't know why this works. Trial & error rocks! if ( get_query_var('paged') > 1 ) { $offsetting = 2 + ( ( get_query_var('paged') - 2 ) * get_query_var('posts_per_page') ); } } } else { $offsetting = 0; } // Plug it into our query. query_posts($query_string . "&offset=$offsetting"); ?>
Thank you!. This sort of worked but one tiny problem is that its not showing the amount of posts I want on the index page… no idea why. But it does offset the second page and the archives work properly…
Just this first part seems not to work… and of course its probably the most important part…any ideas?
<?php
// If we’re on the index page, we only want the two first posts.
if ( is_home() ) {
if ( !is_paged() ) {
query_posts(‘posts_per_page=2’);If your ‘home’ page is a static page, try changing ‘is_home()’ to ‘is_front_page()’.
Nope, its not a static homepage… the other code had is_home() and it worked… its strange. It just won’t change no matter what number I put on ‘posts_per_page= ‘…
Well, if you are willing, please try this variation:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // If we're on the index page, we only want the two first posts. if ( is_home() ) { if ( $paged == 1 ) { query_posts('posts_per_page=2'); // If we're within the index archives, we want ten posts per page as usual. } else { // If we are past the first page, offset by an appropriate amount for the page. // I'm by no means a math guru; I don't know why this works. Trial & error rocks! if ( $paged > 1 ) { $offsetting = 2 + ( $paged - 2 ) * get_query_var('posts_per_page') ); } } } else { $offsetting = 0; } // Plug it into our query. query_posts($query_string . "&offset=$offsetting"); ?>
EDIT: Is this code in index.php, or somewhere else?
This code is in index.php. This new code gives me an error:
Parse error: syntax error, unexpected ‘)’ on line 33
this line: $offsetting = 2 + ( $paged – 2 ) * get_query_var(‘posts_per_page’) );
Thank you for your effort tho! ??
Sorry for the typo. Please change to this:
`$offsetting = 2 + ( ( $paged – 2 ) * get_query_var(‘posts_per_page’) );
Sadly no change with the fixed new code.. ??
Gives me no errors, the second page is offsetting properly, the archives work, but its not showing the amoung of posts I want on the first page…
Problem is, its important for the design, if I can’t achieve the different number of posts I’ll have to re-think everything…
Thanks for all your responses and patience!!
Of course! I am blind!!
Try changing to this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // If we're on the index page, we only want the two first posts. if ( is_home() ) { if ( $paged == 1 ) { $offsetting = '&posts_per_page=2'; // If we're within the index archives, we want ten posts per page as usual. } else { // If we are past the first page, offset by an appropriate amount for the page. // I'm by no means a math guru; I don't know why this works. Trial & error rocks! if ( $paged > 1 ) { $offsetting = 2 + ( ( $paged - 2 ) * get_option('posts_per_page') ); $offsetting = "&offset=$offset"; } } } else { $offsetting = '&offset=0'; } // Plug it into our query. query_posts($query_string . $offsetting); ?>
My only question is whether get_query_var(‘posts_per_page’) will be 2 after the first page. If so, you will be forced to hard-code a value for it.
EDIT: I changed get_query_var(‘posts_per_page’) to get_option(‘posts_per_page’) so it should be OK.
Alright. Here’s what happened..
On the index page, I have the number of posts I want: 11 posts.
On the second page, I have 10 posts, so that’s perfect. BUT the first post should be offset and its not, so I have it both in the index and the second page. Its not such a big deal anyway…
Everything OK on the archives pages.
So for the most part, everything went well, except for that repeated post on the second page…
If I can’t fix it, its not a major issue. So thank you soo much for all your help!!!!!!!!!
Do you have a sticky post on the first page? If so, that confuses WP on the count because stickies are retrieved in a separate internal query and merged into the result array from the normal query. So the array is bigger than it should be, but the offset count is not changed.
Try making the post un-sticky and see how it works.
I have no stickies…
I have it set up like this:
There’s 2 colums with post thumbnails, and on the first page a “featured post” with thumbnail and excerpt (achieved giving the first post a different class and styled with CSS), it takes up the width of the 2 columns. So naturally I need 11 posts on the first page istead of 10, otherwise one column shows less posts, wich is what we’ve accomplished.
The last post from the first page, getting repeated on the second one is not a big deal tho…
So what is the posts_per_page = 2, doing on page 1? And, where did the 11 come from? Is the code you are using different from that posted here?
Please help me understand.
No, I’ve changed the post_per_page = 2, to 11. Simple as that ??
I changed the offset accordingly too, but it won’t work…
Once again – my mistake!
Change this:
$offsetting = 11 + ( ( $paged - 2 ) * get_option('posts_per_page') ); $offsetting = "&offset=$offset";
to this:
$offset = 11 + ( ( $paged - 2 ) * get_option('posts_per_page') ); $offsetting = "&offset=$offset";
- The topic ‘Categories and archives broken’ is closed to new replies.