外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>WordPress开发>WordPress 获取自定义文章类型的相关文章

WordPress 获取自定义文章类型的相关文章

在 WordPress 的文章页面,一般我们都会推荐一些相关文章,对于WordPress 自带的文章类型“post”,可以根据参考下面的文章来实现:

WordPress添加相关文章功能(标题/缩略图样式)

WordPress相关文章插件:Yet Another Related Posts Plugin

WordPress相关文章插件:WordPress Related Posts

但如果是自定义文章类型(Custom Post type),要调用相关文章就需要修改查询参数了。如果你还不知道什么是自定义文章类型,请查看:WordPress 自定义文章类型 介绍及实例解说

下面分享两种情况下的调用方法,默认都是添加到主题的 single.php  或 single-custom_post_type.php 文件。

默认分类的相关文章

所谓“默认分类”是指自带的文章类型post的分类“category”。也就是在注册自定义文章类型时,你注册的分类法就是 category 的时候。以下是代码样例:

<?php
/**
 * WordPress 获取自定义文章类型的相关文章(默认分类)
 * https://www.wpdaxue.com/related-custom-post-type-taxonomy.html
 */
// 获取自定义文章类型的分类项目
$custom_taxterms = wp_get_object_terms( $post->ID,'category', array('fields' => 'ids') );
// 参数
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',// 文章类型
'post_status' => 'publish',
'posts_per_page' => 3, // 文章数量
'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 );
// 查询循环
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;
// 重置文章数据
wp_reset_postdata();
?>

你需要根据自己的需要,修改参数:

11行:’YOUR_CUSTOM_POST_TYPE’ 你需要修改为你的自定义文章类型

12行: 修改文章数量

还可以修改 26、29、32行的代码来输出自己想要的内容,比如添加缩略图,以及更多文章meta信息等。

自定义分类的相关文章

所谓“自定义分类”是指非默认的 category 这个分类法。以下是代码样例:

<?php
/**
 * WordPress 获取自定义文章类型的相关文章(自定义分类)
 * https://www.wpdaxue.com/related-custom-post-type-taxonomy.html
 */
// 获取自定义文章类型的分类项目
$custom_taxterms = wp_get_object_terms( $post->ID,'your_taxonomy', array('fields' => 'ids') );
// 参数
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',// 文章类型
'post_status' => 'publish',
'posts_per_page' => 3, // 文章数量
'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 );
// 查询循环
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;
// 重置文章数据
wp_reset_postdata();
?>

你需要根据自己的实际,修改如下参数:

第 7 行和第 16 行:修改 your_taxonomy 为你的自定义分类法

11行:’YOUR_CUSTOM_POST_TYPE’ 你需要修改为你的自定义文章类型

12行: 修改文章数量

还可以修改 26、29、32行的代码来输出自己想要的内容,比如添加缩略图,以及更多文章meta信息等。

参考资料:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

介绍50个 WordPress 动作挂钩(总结)

2016-1-17 10:35:34

WordPress开发

50个WordPress过滤钩子(介绍过滤钩子)

2016-1-19 11:52:31

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索