WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Previous Post Link on index.php (5 posts)

  1. geoff67
    Member
    Posted 5 years ago #

    I've been trying to figure this out for hours. I've built a theme that displays one post on the index page, and I don't want users using the archive (/page/2/) next and prev links.

    I'd like to link to the previous post, because once there, users will be on single.php, which supports next/prev post links.

    Basically, I want a work around for this:
    <?php previous_post_link('%link','&laquo; Previous',FALSE,'47') ?>

    Without using this:
    <?php posts_nav_link('','','&laquo; Previous') ?>

    I've been trying to use something Otto wrote:

    function get_next_post_id() {
    global $wp_query;
    if ($wp_query->$current_post == $wp_query->$post_count) return 0;
    $next_post = $wp_query->posts[$wp_query->$current_post - 1];
    return $next_post->ID;
    }

    In combination with this:
    <a href="<?php echo get_permalink(); ?>" />&laquo; Previous</a>

    And this:
    <a href="<?php echo get_option('home'); ?>/<?php echo get_next_post_id(); ?>" />&laquo; Previous</a>

    Website link

  2. geoff67
    Member
    Posted 5 years ago #

    Figures once I post here, I find a semi-solution.

    Installing this plug-in allows for what I wanted, but I can't get &laquo; to be in the link.

    I guess I'll live.

    For those who want to know how, you put this somewhere in your template:
    <?php mdv_recent_posts(1,'','',false,1,false,false); ?>

    And I modified line26 of the plug-in so that Previous is displayed, instead of the title of the post (which is default):
    $output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars(Previous) . '</a>';

  3. ksek
    Member
    Posted 5 years ago #

    Do you know if this allows linking to the previous post in the same category?

  4. Shifty88
    Member
    Posted 5 years ago #

    Yea, how would I go about ensuring that the link only goes to posts in the same category?

  5. henkholland
    Member
    Posted 5 years ago #

    <?php previous_post_link('&laquo; %link','%title','TRUE','') ?>
    With TRUE it goes to previous in same category.

    http://codex.wordpress.org/Template_Tags/next_post_link

Topic Closed

This topic has been closed to new replies.

About this Topic