2013-10-21 21 views
0

我有以下問題。當我點擊相同類別的上一個/下一個按鈕時,不幸的是,我按照時間順序接收了上一個/下一個帖子,而不是同一類別的上一個/下一個帖子。當你點擊上一個/下一個按鈕wordpress時,顯示同一類別的帖子

下面是代碼:

< div id="previous_post"> 
< ?php previous_post_link('%link', __('<span class="prev"><< Preivous Post</span>', 'esquire')); ? > 
< /div> 

< div id="next_post"> < ?php next_post_link('%link', __('<span class="next">Next Post >></span>', 'esquire')); ? > 

< /div> 

我試圖解決我的問題,本文http://codex.wordpress.org/Function_Reference/next_post_link 但有些事我做錯了。

有什麼建議嗎?

+4

你可能想繼續閱讀進一步下來,直到你看到這樣 <?PHP next_post_link(「%鏈接」,「下一步POS t','TRUE','13'); ?> – xlordt

+0

嘿@xlordt。我已經嘗試了參數TRUE,但它沒有幫助。當然,我不確定我是否把它放在正確的地方。 –

+0

你究竟如何嘗試這個?你可以用你嘗試過的解決方案更新你的文章嗎? – xlordt

回答

1

例如

previous_post_link('%link', __('<span class="prev"><< Preivous Post</span>', TRUE, 'esquire')); 

我不想指定每次每個類別。我希望這是自動完成的。例如,當它顯示類別機器中的帖子並打開以閱讀第一篇文章時,當您點擊下一篇文章時,則必須看到相同類別的下一篇文章,而不是下一篇文章。如果該類別是食品,則是同一類別的下一個帖子。

我很清楚,或者我更困惑嗎?

+0

OOOOOOHHH神,多麼失敗!你是對的xlordt。我試圖把真實的地方放在一個錯誤的地方。我把它放在最後的兩個括號之間,它起作用。 –

0

這裏是最後的/正確的語法

previous_post_link('%link', __('<span class="prev"><< Previous Post</span>', 'esquire'), TRUE); 

謝謝!

+0

但這不適用於next_post_link? –

1

從您的父主題中複製single.php頁面並將其粘貼到您的Child-theme的目錄中。從child-theme目錄打開single.php,並在文件末尾添加以下代碼[get_footer();之前)。 ]

<?php 
$post_id = $post->ID; // Get current post ID 
$cat = get_the_category(); 
$current_cat_id = $cat[0]->cat_ID; // Get current Category ID 

$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC'); 
$posts = get_posts($args); 
// Get IDs of posts retrieved by get_posts function 
$ids = array(); 
foreach ($posts as $thepost) { 
    $ids[] = $thepost->ID; 
} 
// Get and Echo the Previous and Next post link within same Category 
$index = array_search($post->ID, $ids); 
$prev_post = $ids[$index-1]; 
$next_post = $ids[$index+1]; 
?> 

<?php if (!empty($prev_post)){ ?> <a class="previous-post" rel="prev" href="<?php echo get_permalink($prev_post) ?>"> <span class="meta-icon"><i class="fa fa-angle-left fa-lg"></i></span> Previous</a> <?php } ?> 

<?php if (!empty($next_post)){ ?> <a class="next-post" rel="next" href="<?php echo get_permalink($next_post) ?>">Next <span class="meta-icon"><i class="fa fa-angle-right fa-lg"></i></span> </a> <?php } ?> 

添加此代碼後,下面的代碼粘貼到你的孩子爲主題的style.css的樣式鏈接:

a.previous-post, a.next-post { 
    color: #fff; 
    background-color: #4498e7; 
    text-align: center; 
    height: 34px; 
    line-height: 34px; 
    font-size: 14px; 
    border: 1px solid; 
    padding: 0 20px; 
    margin-bottom: 30px; 
    text-transform: uppercase; 
    border-radius: 4px; 
    font-weight: bold; 
} 

a.previous-post:hover, a.next-post:hover { 
    color: #4498e7; 
    background-color: #fff; 
} 

a.previous-post { 
    float: left !important; 
} 

a.next-post { 
    float: right !important; 
} 

要看到的這些動作鏈接,訪問我的網站:www.techtutsonline.com

讓我知道結果:)

相關問題