SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Wordpress: Can't Get Pagination Links to Appear

    Hello there.

    I'm making my first attempt at a paginated Wordpress wp_query page, but for some reason I just can't get Wordpress to load the "next" links.

    It's a news page for a website, and I'm treating Wordpress's Posts as the website's news articles. So on this page, five posts should load, with their titles, dates, excerpts, and permalinks, then I'd like to have links appear at the end to allow users to view the previous five posts (or the next five posts, or both, as the case may be).

    I've gotten everything working, but the "next" links.

    Code below;

    PHP Code:
    <?php
    // Create WP_Query
        
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        
    $args = array(
            
    'post_type' => 'post',
            
    'posts_per_page' => 5,
            
    'order' => DESC,
            
    'orderby' => date,
            
    'paged' => $paged
        
    );

        
    $my_query = new WP_Query($args);

    // Start Wordpress Loop
        
    if ( $my_query -> have_posts() ) : while ( $my_query -> have_posts() ) : $my_query -> the_post();

    // Generate Content
        
    $output[] = '<h3>' get_the_title($post->ID) . '</h3></a></p>';
        
    $output[] = '<p>';
        
    $output[] = '    Published on ' get_the_date() . ' by ' get_the_author() . '<br />';
        
    $output[] = '    ' get_the_excerpt($post->ID) . '<br />';
        
    $output[] = '    <a href="' get_permalink($post->ID) . '" target="_blank" rel="nofollow">read more &raquo;</a>';
        
    $output[] = '</p>';
        
    $output[] = '';
        
    $output[] = '<hr />';
        
    $output[] = '';

    // End Wordpress Loop
        
    endwhile; else:
        
    $output[] = _e('<p>Sorry, there are no news posts at this time.</p>');
        endif;

    // Implode and Echo Output Array
        
    if(isset($output))
            {
            
    $output implode("\n",$output);
            echo(
    $output);
            }

    // Unset MySQL Loop Variables
        
    unset($output);
    ?>
    <?php previous_posts_link
    ('Newer Entries &raquo;'0?>
    <?php next_posts_link
    ('&laquo; Older Entries'0); ?>
    The funny thing is that if I manually place myself on an older page (by adding ?paged=3 for example to my URL), I'm getting the "Newer Entries >" link fine. But that "Older Entries" isn't showing up at all!

    Any suggestions, anyone?

    I've been googling this for a long while now and getting nowhere.

    Thanks!
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Okay, so I've narrowed it down to something with the "0" in this Wordpress function;
    PHP Code:
    <?php previous_posts_link('Newer Entries &raquo;'0?>
    According to the Codex, zero in this function is supposed to mean limitless, but that's not happening or maybe I'm doing something wrong.

    When I change it to;
    PHP Code:
    <?php previous_posts_link('Newer Entries &raquo;',20?>
    (arbitrarily hardcoding it to "20"), I get my "Newer Entries" links!

    Problem is that obviously I can't leave it with an arbitrary number.

    I suppose I could find a way to determine the maximum number of posts and divide by "5" (my number of posts per page) to get the correct number of pages to enter into that function, right?

    Well, we'll see how that one goes.

    Again, if anyone knows a more elegant or simpler solution, please share!

    Thanks!
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    YES!

    It worked perfectly!

    PHP Code:
    <?php
    // Create WP_Query
        
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        
    $args = array(
            
    'post_type' => 'post',
            
    'posts_per_page' => 5,
            
    'order' => DESC,
            
    'orderby' => date,
            
    'paged' => $paged
        
    );

        
    $my_query = new WP_Query($args);

    // Start Wordpress Loop
        
    if ( $my_query -> have_posts() ) : while ( $my_query -> have_posts() ) : $my_query -> the_post();

    // Generate Content
        
    $output[] = '<h3>' get_the_title($post->ID) . '</h3>';
        
    $output[] = '<p>';
        
    $output[] = '    Published on ' get_the_date() . ' by ' get_the_author() . '<br />';
        
    $output[] = '    ' get_the_excerpt($post->ID) . '<br />';
        
    $output[] = '    <a href="' get_permalink($post->ID) . '" target="_blank" rel="nofollow">read more &raquo;</a>';
        
    $output[] = '</p>';
        
    $output[] = '';
        
    $output[] = '<hr />';
        
    $output[] = '';

    // End Wordpress Loop
        
    endwhile; else:
        
    $output[] = _e('<p>Sorry, there are no news posts at this time.</p>');
        endif;

    // Implode and Echo Output Array
        
    if(isset($output))
            {
            
    $output implode("\n",$output);
            echo(
    $output);
            }

    // Unset MySQL Loop Variables
        
    unset($output);

    // Calculate Number of WP Paged
        
    $published_posts wp_count_posts()->publish;
        
    $number_of_pages = ($published_posts/5);
        
    $number_of_pages ceil($number_of_pages);
    ?>
    PHP Code:
    <?php next_posts_link('&laquo; Older Entries',$number_of_pages?>
    <?php previous_posts_link
    ('Newer Entries &raquo;',0?>
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •