當前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress 獲取自定義文章類型的相關(guān)文章

WordPress 獲取自定義文章類型的相關(guān)文章

在 WordPress 的文章頁面,一般我們都會推薦一些相關(guān)文章,對于WordPress 自帶的文章類型“post”,可以根據(jù)參考下面的文章來實現(xiàn):

WordPress添加相關(guān)文章功能(標題/縮略圖樣式)

WordPress相關(guān)文章插件:Yet Another Related Posts Plugin

WordPress相關(guān)文章插件:WordPress Related Posts

但如果是自定義文章類型(Custom Post type),要調(diào)用相關(guān)文章就需要修改查詢參數(shù)了。如果你還不知道什么是自定義文章類型,請查看:WordPress 自定義文章類型 介紹及實例解說

下面分享兩種情況下的調(diào)用方法,默認都是添加到主題的 single.php  或 single-custom_post_type.php 文件。

默認分類的相關(guān)文章

所謂“默認分類”是指自帶的文章類型post的分類“category”。也就是在注冊自定義文章類型時,你注冊的分類法就是 category 的時候。以下是代碼樣例:

<?php
/**
 * WordPress 獲取自定義文章類型的相關(guān)文章(默認分類)
 * http://www.ydqwiac.cn/related-custom-post-type-taxonomy.html
 */
// 獲取自定義文章類型的分類項目
$custom_taxterms = wp_get_object_terms( $post->ID,'category', array('fields' => 'ids') );
// 參數(shù)
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',// 文章類型
'post_status' => 'publish',
'posts_per_page' => 3, // 文章數(shù)量
'orderby' => 'rand', // 隨機排序
'tax_query' => array(
    array(
        'taxonomy' => 'category', // 分類法
        'field' => 'id',
        'terms' => $custom_taxterms
    )
),
'post__not_in' => array ($post->ID), // 排除當前文章
);
$related_items = new WP_Query( $args );
// 查詢循環(huán)
if ($related_items->have_posts()) :
    echo '<h3 class="related-posts-title">Related Posts</h3><ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    endwhile;
    echo '</ul>';
endif;
// 重置文章數(shù)據(jù)
wp_reset_postdata();
?>

你需要根據(jù)自己的需要,修改參數(shù):

11行:’YOUR_CUSTOM_POST_TYPE’ 你需要修改為你的自定義文章類型

12行: 修改文章數(shù)量

還可以修改 26、29、32行的代碼來輸出自己想要的內(nèi)容,比如添加縮略圖,以及更多文章meta信息等。

自定義分類的相關(guān)文章

所謂“自定義分類”是指非默認的 category 這個分類法。以下是代碼樣例:

<?php
/**
 * WordPress 獲取自定義文章類型的相關(guān)文章(自定義分類)
 * http://www.ydqwiac.cn/related-custom-post-type-taxonomy.html
 */
// 獲取自定義文章類型的分類項目
$custom_taxterms = wp_get_object_terms( $post->ID,'your_taxonomy', array('fields' => 'ids') );
// 參數(shù)
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',// 文章類型
'post_status' => 'publish',
'posts_per_page' => 3, // 文章數(shù)量
'orderby' => 'rand', // 隨機排序
'tax_query' => array(
    array(
        'taxonomy' => 'your_taxonomy', // 分類法
        'field' => 'id',
        'terms' => $custom_taxterms
    )
),
'post__not_in' => array ($post->ID), // 排除當前文章
);
$related_items = new WP_Query( $args );
// 查詢循環(huán)
if ($related_items->have_posts()) :
    echo '<h3 class="related-posts-title">Related Posts</h3><ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    endwhile;
    echo '</ul>';
endif;
// 重置文章數(shù)據(jù)
wp_reset_postdata();
?>

你需要根據(jù)自己的實際,修改如下參數(shù):

第 7 行和第 16 行:修改 your_taxonomy 為你的自定義分類法

11行:’YOUR_CUSTOM_POST_TYPE’ 你需要修改為你的自定義文章類型

12行: 修改文章數(shù)量

還可以修改 26、29、32行的代碼來輸出自己想要的內(nèi)容,比如添加縮略圖,以及更多文章meta信息等。

參考資料:

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress開發(fā)

介紹50個 WordPress 動作掛鉤(總結(jié))

2016-1-17 10:35:34

WordPress開發(fā)

50個WordPress過濾鉤子(介紹過濾鉤子)

2016-1-19 11:52:31

0 條回復(fù) A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

吕梁市| 双城市| 汝阳县| 内丘县| 利辛县| 纳雍县| 栾川县| 天台县| 陆良县| 柯坪县| 永修县| 长沙县| 鹤山市| 龙山县| 昌邑市| 洛浦县| 德化县| 广州市| 景谷| 大石桥市| 江口县| 晴隆县| 无锡市| 班戈县| 玛曲县| 黎城县| 大足县| 玉树县| 漳平市| 聂荣县| 卓尼县| 兴宁市| 屏东市| 谢通门县| 泽普县| 郓城县| 高青县| 扎鲁特旗| 西城区| 旺苍县| 辽阳市|