很多用戶在使用wordpress制作網(wǎng)站的時(shí)候,會(huì)在文章頁面的底部使用相關(guān)文章推薦,下面的代碼是提供文章頁面里面從分類下的文章列表調(diào)用方法。默認(rèn)調(diào)用的是相關(guān)分類下面最新的文章內(nèi)容。里面可以自由設(shè)置數(shù)量以及排序的方式,默認(rèn)是根據(jù)分類目錄下最新發(fā)布的時(shí)間排列的。
如果是想要顯示在側(cè)邊欄里,可以在對(duì)應(yīng)的sidebar.php文件里進(jìn)行添加,如果是顯示在文章內(nèi)容的下方,可以在文章頁面single.php里進(jìn)行添加,直接復(fù)制粘貼就可以完成,簡(jiǎn)單易操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php /* single page?show current category articles */ ?> <?php if ( is_single() ) : global $post; $categories = get_the_category(); foreach ($categories as $category) : ?> <li class="widget widget_recent_entries" id="<?php $category->term_id;?>-posts"> <h2 class="widgettitle"><?php echo $category->name; ?></h2> <ul> <?php $posts = get_posts('numberposts=5&category='. $category->term_id); foreach($posts as $post) : ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> </li> <?php endforeach; endif ; ?> <?php /* end show current category articles */ ?> |