简体   繁体   English

wordpress是否可以在所有页面上的上一篇文章下一篇文章中使用?

[英]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 . 我尝试使用Latest Post Link插件 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. 我对Wordpress有点陌生。 Any help is appreciated so thanks in advance! 感谢您的帮助,在此先感谢您!

Jeremy 杰里米

Open single.php and rename previous_post_link to custom_previous_post_link and next_post_link to custom_next_post_link. 打开single.php,将previous_post_link重命名为custom_previous_post_link,将next_post_link重命名为custom_next_post_link。 Then past the following into your functions.php file: 然后将以下内容放入您的functions.php文件中:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM