Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [WordPress] Multiple loops & pagination (Sticky posts not on absolute top)

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38452
    Spiekerboks
    Participant

    Hi all,

    Some days ago I started Wattisduurzaam.nl, a Dutch blog on sustainable technologies and energy savings. To give some anouncements a little bit more attention I would like to be able to control the order in which posts are displayed.

    The blog runs on WordPress, which already has a sticky post function build in. This method works reasonably well but i do not like how a sticky post is always displayed on absolute top. I would rather be able to display the latest two or three posts, then a featured/sticky posts and thereafter another set of recent posts.

    I was expecting this to be a matter of finding the right code snippet but after some heavy googling I lost hope a bit. What I did find:

    – Some plugins might do the trick (with help of some workarounds). Sadly those plugins have not been maintained recently (and I am still hoping to do this seemingly simple task without plugins).

    – It might be possible to use three separate ‘loops’ to generate the desired effect but this will need some expert coding to prevent duplicate posts.

    – It is possible to manually adjust the publication date of the featured post. Easy but stupid.

    – One of my own ideas was to try something via CSS. Maybe it is possible to use nth-child or just a class to rearrange the post-order via relative positioning.

    None of the above however seems to be a very smart solution. Before I will start reinventing the wheel (probably making a fool of myself along the way – if I haven’t done so already;) ) I would like to ask the users of this forum for a little help.

    So has someone maybe encountered a better method, preferably something simple to add in the functions.php or home.php files of my template, to arrive at the desired post-order?

    Thanks in advance!

    #106952
    Spiekerboks
    Participant

    Hi all,

    Since the post above I’ve made some progress but I’m not really there yet, some help would still be greatly appreciated ;).

    Using three separate loops and an offset I managed to get close to the desired effect. Sadly pagination does not work anymore since the template file now requests a specific and static number of posts. An example applied on my author-page: http://www.wattisduurzaam.nl/author/admin/ shows exactly the same posts as
    http://www.wattisduurzaam.nl/author/admin/page/6/

    My code looks something like this:

    
    // Requests latest normal post, excluding the announcement category
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content for loop 1...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    // Requests latest post in announcement category
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content for loop 2...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    // Requests latest eight normal post, excluding the announcement category and without duplicating the first entry
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content for loop 3...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    Searching for a solution I stumbled upon this snipped quite often:

    'paged' => get_query_var('paged')

    This indeed does work for one loop but I do not yet see how to translate this to work for all three off them without duplicating posts.

    Does someone maybe know of a method to use the pagination as a multiplier in the post query and for the offset?

    It is good enough if the announcement is displayed only on the homepage, another idea therefore was to let the pagination on home.php (with the above three loops) redirect to index.php (with just the standard loop). Unfortunately I did not find a way to prevent home.php from overruling index.php on deeper pages. An attempt to put all the custom loops within an is_home block and the normal loop in an else block did not workout either (probably due to nested if loops which have to remain in place for other functions).

    So, does anyone know of a method to restore the pagination with these custom loops?

    Thanks in advance!

    #106964
    zardos
    Member

    Just do a $tempPagnation = get_query_var(‘paged’); At the top of the code and then add it to all your post querys.

    ‘);
    And do whaterv math you want with that variable;)
    /R

    #106967
    Spiekerboks
    Participant

    Ah wonderfull, thank you Zardos!

    For future reference my working code:

    
    $pageNumber = 	(get_query_var('paged')) ? get_query_var('paged') : 1;
    $offseta = ($pageNumber * 10 - 10);
    $offsetb = (1 + $pageNumber * 10 - 10);
    ?>

    // Requests latest normal post, excluding the announcement category
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    // Requests latest post in announcement category
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    // Requests latest eight normal post, excluding the announcement category and without duplicating the first entry
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    //... content...//

    endwhile; endif;// End the Loop and Check for Posts
    wp_reset_query(); // Reset the loop
    ?>

    Notice I had to use double quotes, otherwise PHP takes things to literally ;). Also the paged-var defaults to zero apparently so I had to alter that to 1 for my calculations to work (otherwise there would be a negative offset on the homepage).

    One small problem remains: The pagination plugin (WP-Paginate) outputs two pages to many which only show the announcement. I can certainly live with that but if someone knows the reason and/or a solution I’d like to hear it (therefore I have not yet checked the solved box).

    Thanks again Zardos!

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.