Forums

display post title in next/previous links when NOT in single mode (7 posts)

  1. rebeccacotu
    Member
    Posted 3 years ago #

    I've been searching and searching, and can't find the answer.

    In my theme, I want to always display just one post, with comments. So for the "previous" and "next" links, I want to display the titles of the previous and next posts -- not the static text "previous page/next page" or whatever.

    In other words, I want the "index" and "archive" to look just like a permalink page -- even down to the previous/next links containing titles.

    previous_post_link is documented to only work in a permalink page; I find it does work in other contexts, except that it shows the static text "%link" instead of the title.

    I thought I might be able to fake it out by assigning $single=1, but that also didn't work.

  2. rebeccacotu
    Member
    Posted 3 years ago #

    Hoping more people are around, now that the holiday is over, to help me out...

  3. bekabug
    Member
    Posted 3 years ago #

    On the homepage you'd only have a previous link since the one being displayed is the most recent. On your permalink pages you'd use the previous_post_link the way the documentation says.

    So here's how i did it...

    <?php
     $prevposts = get_posts('numberposts=2&offset=1');
     foreach($prevposts as $post) :
        setup_postdata($post);
     ?>
     <h3><a href="<?php the_permalink(); ?>" id="prev-post-<?php the_ID(); ?>"><?php the_title(); ?></a></h3>
    <?php endforeach; ?>

    I tested this on my blog...worked like a charm.
    http://codex.wordpress.org/Template_Tags/get_posts

  4. rebeccacotu
    Member
    Posted 3 years ago #

    Thanks! I've made progress using your code, but I'm stuck on the archive/categories/tags screens. In those cases I don't want the permalink -- I have to navigate within the tag or category. Also, offset=-1 doesn't seem to work for getting the "next" item.

    Any suggestions?

  5. bekabug
    Member
    Posted 3 years ago #

    Sorry I wasn't clear. The code I gave you is only useful for the homepage.

    As far as the other pages...I'm tapped. I don't think there is a way to make this within the wordpress framework. You would have to somehow structure a custom function/query with PHP. That said...there is probably a reason this functionality has never been attempted in a theme before.

  6. bekabug
    Member
    Posted 3 years ago #

    Sorry I wasn't clear. The code I gave you is only useful for the homepage.

    As far as the other pages...I'm tapped. I don't think there is a way to make this within the wordpress framework. You would have to somehow structure a custom function/query with PHP. That said...there is probably a reason this functionality has never been attempted in a theme before.

  7. rebeccacotu
    Member
    Posted 3 years ago #

    Never been attempted--by anyone, anywhere, anytime?

    While I'd like to believe that I'm so innovative I've dreamt up something no one else would ever think of, I don't. It might be that there is a technical reason that this is impossible, but I haven't run across a clear enough indication of that, either.

    I do have a possible solution in mind. But before I attempt to reinvent this particular wheel, I'd like to ask around the community of those more experienced than myself in WP, to offer their suggestions/knowledge/solutions.

  8. jaucoin
    Member
    Posted 2 years ago #

    Try this:

    <ul>
    <li><?php previous_post_link('%link')?></li>
    <li><?php next_post_link('%link') ?></li>
    </ul>

    It would be instead of this:

    <ul>
    <li><?php next_posts_link('&laquo; Older Entries') ?></li>
    <li><?php previous_posts_link('Newer Entries &raquo;') ?></li>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic