2015-01-21 67 views
0

這裏是我工作的代碼:是否可以在wordpress上一篇文章的所有頁面上發佈下一篇文章?

<?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;" />'); ?> 

偉大的工程,但它只能顯示在中間頁兩個圖像,但不能在第一頁和最後一頁。這很有意義,因爲第一頁上沒有上一頁,而最後一頁上沒有下一頁。我的問題是,圖像看起來很奇怪,沒有頁面上的其他圖像。我想要做的是讓圖像出現在第一頁上,並鏈接到最後一個帖子,反之亦然。我試過使用Latest Post Link plug-in。和此代碼:

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

但是,然後顯示三個圖像。我試圖將條件語句只顯示在第一頁上,但我無法想象或找到正確的變量名稱來告訴我所處的頁面。我對wordpress有點新鮮。任何幫助表示感謝,提前謝謝!

傑里米

回答

1

開放的single.php並重命名previous_post_link到custom_previous_post_link和next_post_link到custom_next_post_link。然後通過以下到您的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); 
    } 
+0

快速的問題。我的previous_post_link和next_post_link應該位於single.php文件中嗎?現在不是。它實際上在archive.php文件中。 – 2015-01-22 02:20:19

+0

通常single.php用於單個帖子。存檔可以用作存檔模板。這真的取決於你的主題和設計師如何製作結構。如果您在archive.php文件中進行了更改並且它可以正常工作,那麼沒有什麼可做的了。 – Todd 2015-01-23 03:00:59

+0

由於某些原因,當我更改archive.php文件時,它不起作用。它實際上刪除了按鈕和下面的一切(頁腳等)。 – 2015-01-24 03:22:49

相關問題