問題描述
我是新的 wordpress 開發,只是試圖將我的 HTML 轉換為 WordPress 主題,我開始與克里斯·科耶的空白主題。
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
如果有 next_posts_link(),我該如何輸出。我需要這個,因為我將使用<ul> 作為我的分頁。如果我不這樣做,我會得到一個空的子彈
最佳解決方案
您可以使用 get_previous_posts_link 和 get_next_posts_link 來確定它們是否存在如下:
$prev_link = get_previous_posts_link(__('« Older Entries'));
$next_link = get_next_posts_link(__('Newer Entries »'));
// as suggested in comments
if ($prev_link || $next_link) {
echo '<ul class="navigation">';
if ($prev_link){
echo '<li>'.$prev_link .'</li>';
}
if ($next_link){
echo '<li>'.$next_link .'</li>';
}
echo '</ul>';
}
希望這可以幫助
次佳解決方案
我寫了一段時間以前,但仍然有效:
http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/
您可以將以下功能新增到 functions.php 檔案中:
/**
* If more than one page exists, return TRUE.
*/
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}
將您的程式碼更新為:
<?php if (show_posts_nav()) : ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php endif; ?>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。