為了增加閱讀量,方便讀者找到需要的東西,我們常常在文章的底部添加相關文章的功能,我們可以從兩個角度考慮,讀者可能感興趣的文章,以及相關文章,前者我們調取相同 tags下的文章隨機生成,而相關文章我們可以調取分類categories下的文章隨機生成,實作的方法可以通過 wp_get_post_tags 和 wp_get_post_categories來實作,網上的文章都可能源于露兜博客
相關文章標簽tags
首先獲取文章的所有標簽,接著獲取這些標簽下的 n 篇文章,那么這 n 篇文章就是與該文章相關的文章了,現在可以見到的WordPress相關文章插件都是使用的這個方法,
<ul>
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
// 獲取標簽串列
$tag_list[] .= $tag->term_id;
}
// 隨機獲取標簽串列中的一個標簽
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 該方法使用 query_posts() 函式來呼叫相關文章,以下是引數串列
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL), // 不包括的分類ID
'post__not_in' => array($post->ID),
'showposts' => 6, // 顯示相關文章數量
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暫無相關文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暫無相關文章</li>';
}
?>
</ul>
使用說明:”不包括的分類ID” 指的是相關文章不顯示該分類下的文章,將同行的 NULL 改成文章分類的ID即可,多個ID就用半角逗號隔開,因為這里限制只顯示6篇相關文章,所以不管給 query_posts() 的引數 tag__in 賦多少個值,都是只顯示一個標簽下的 6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇,,,,,,所以如果這篇文章有多個標簽,那么我們采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個引數,獲取該標簽下的6篇文章
相關文章呼叫分類categories
本方法是通過獲取該文章的分類id,然后獲取該分類下的文章,來達到獲取相關文章的目的,
<ul>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
'category__in' => array( $cats[0] ),
'post__not_in' => array( $post->ID ),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暫無相關文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暫無相關文章</li>';
}
?>
</ul>
本站采用了網上的這兩種方式實作了你感興趣的文章和相關文章的功能,
511遇見不一樣的遇見
不一樣的遇見技術交流建站免費學習IT的平臺
Skip to content
511遇見
不一樣的遇見
您當前的位置: 首頁 > Wordpress > 正文內容 WordPress無插件查詢tags和categories實作相關文章
WordPress無插件查詢tags和categories實作相關文章
為了增加閱讀量,方便讀者找到需要的東西,我們常常在文章的底部添加相關文章的功能,我們可以從兩個角度考慮,讀者可能感興趣的文章,以及相關文章,前者我們調取相同 tags下的文章隨機生成,而相關文章我們可以調取分類categories下的文章隨機生成,實作的方法可以通過 wp_get_post_tags 和 wp_get_post_categories來實作,網上的文章都可能源于露兜博客
相關文章標簽tags
首先獲取文章的所有標簽,接著獲取這些標簽下的 n 篇文章,那么這 n 篇文章就是與該文章相關的文章了,現在可以見到的WordPress相關文章插件都是使用的這個方法,
<ul>
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
// 獲取標簽串列
$tag_list[] .= $tag->term_id;
}
// 隨機獲取標簽串列中的一個標簽
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 該方法使用 query_posts() 函式來呼叫相關文章,以下是引數串列
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL), // 不包括的分類ID
'post__not_in' => array($post->ID),
'showposts' => 6, // 顯示相關文章數量
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暫無相關文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暫無相關文章</li>';
}
?>
</ul>
使用說明:”不包括的分類ID” 指的是相關文章不顯示該分類下的文章,將同行的 NULL 改成文章分類的ID即可,多個ID就用半角逗號隔開,因為這里限制只顯示6篇相關文章,所以不管給 query_posts() 的引數 tag__in 賦多少個值,都是只顯示一個標簽下的 6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇,,,,,,所以如果這篇文章有多個標簽,那么我們采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個引數,獲取該標簽下的6篇文章
相關文章呼叫分類categories
本方法是通過獲取該文章的分類id,然后獲取該分類下的文章,來達到獲取相關文章的目的,
<ul>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
'category__in' => array( $cats[0] ),
'post__not_in' => array( $post->ID ),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="https://blog.csdn.net/zcp528/article/details/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暫無相關文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暫無相關文章</li>';
}
?>
</ul>
本站采用了網上的這兩種方式實作了你感興趣的文章和相關文章的功能,
發布日期: 2016-07-07 作者: 511遇見
所屬分類: Wordpress, Wordpress 綜合 標簽: categories tags WordPress主題開發 分類 標簽 相關文章
您可能感興趣的文章:
? 第七課WordPress主題制作綜合教程頭部Brand設計
? Wordpress基于bootstrap自適應主題制作
? wordpress讓pre支持自動換行
? Wordpress主題綜合免費視頻系列教程
? wordpress網站安全的建議和優化
? WordPress評論框DIY自定義增加欄位
? 第五課WordPress主題制作頭部檔案header.php制作
? wordpress郵件地址混淆 你沒權限訪問整個郵件地址造成的死鏈接
? 第三課511遇見WordPress主題開發教程基本檔案的建立
? WordPress網站遷移教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/4834.html
標籤:其他
