Jeremy H.
Jeremy H.

Reputation: 1

Is it possible in wordpress previous post next post on all pages?

Here is the code I working on:

<?php previous_posts_link('<img src="imageURL.com/image1.png" style="width: 250px;float: left;" />'); ?>

<?php next_posts_link('<img src="imageURL.com/image2.png" style="width: 250px;float: left;" />'); ?>

Which works great, except it only shows both images on the middle pages, but not on the first and last pages. This makes sense because there is not a previous page on the first page and not a next page on the last page. My problem is that the images look weird without the other image on the page. What I would like to do is make the image appear on the first page and link to the last post and vice-versa on the last page. I tried using the Latest Post Link plug-in. and this code:

<a href="<?php latestpostlink_permalink() ?>" title="Most Recent Post">Latest &gt;|</a>

But then three images show up. I tried putting conditional statements to only make this image show up on the first page, but I can not think or find the right variable name to tell what page I am on. I am a bit new to wordpress. Any help is appreciated so thanks in advance!

Jeremy

Upvotes: 0

Views: 85

Answers (1)

Todd
Todd

Reputation: 21

Open single.php and rename previous_post_link to custom_previous_post_link and next_post_link to custom_next_post_link. Then past the following into your functions.php file:

function custom_adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true)
    {
    if ($previous && is_attachment()) $post = get_post(get_post()->post_parent);
      else $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
    if (!$post)
        {
        $args = array(
            'posts_per_page' => 1,
            'orderby' => 'date',
            'ignore_sticky_posts' => 1
        );
        if ($previous) $args['order'] = 'DESC';
          else $args['order'] = 'ASC';
        $adjposts = get_posts($args);
        $post = $adjposts[0];
        }
    $title = $post->post_title;
    if (empty($post->post_title)) $title = $previous ? __('Previous Post') : __('Next Post');
    $title = apply_filters('the_title', $title, $post->ID);
    $date = mysql2date(get_option('date_format') , $post->post_date);
    $rel = $previous ? 'prev' : 'next';
    $string = '<a href="' . get_permalink($post) . '" rel="' . $rel . '">';
    $inlink = str_replace('%title', $title, $link);
    $inlink = str_replace('%date', $date, $inlink);
    $inlink = $string . $inlink . '</a>';
    $output = str_replace('%link', $inlink, $format);
    $adjacent = $previous ? 'previous' : 'next';
    echo apply_filters("{$adjacent}_post_link", $output, $format, $link, $post);
    }
function custom_previous_post_link($format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '')
    {
    custom_adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
    }
function custom_next_post_link($format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_categories = '')
    {
    custom_adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
    }

Upvotes: 1

Related Questions