wordpress相关文章的优化机智和自定义设置

作者 : 轩寒 本文共2516个字,预计阅读时间需要7分钟 发布时间: 2022-04-30 共109人阅读

之前站哈源码网介绍过 wordpress相关文章实现的方法,例:《代码实现WordPress相关文章》,那么今天说的这个教程,是从优化角度来更合理的实现wordpress相关文章的,至于客观喜欢哪个,自己决定吧!

策略:文章内容相关程度: 手动指定 > 标签 >分类 > 随机

relatedposts
relatedposts

实现方式:下面代码直接加到functions.php中即可

function add_related_posts($content){
    return $content . wp_related_posts();
}
add_filter ('the_content', 'add_related_posts'); //hook
function wp_related_posts(){
    global $post;
    $num = 5;//文章数量
    $counter = 1;
    $exclude_id = get_post_meta($post->ID,'related',true);//获取手动置顶的相关文章
    if ($exclude_id){
        $args = array(
            'post_status' => 'publish',
            'post_type' => array('post'),
            'post__in' => explode(',', $exclude_id),
            'posts_per_page' => $num
        );
        $posts = get_posts($args);
        foreach($posts as $sb){
            $output .= '<li><a href="' . get_permalink($sb->ID) . '">' . $sb->post_title . '</a></li>';//可自定义样式
            $i++;
        }
    }
    if( $i < $num){//自定义文章数不足后通过分类和标签处理
        $tagsid = array();
        $catid = array();
        $thisid[] = $post->ID;
        $posttags = get_the_tags();
        $catids = get_the_category();
        if(!emptyempty($posttags)) {
            foreach($posttags as $tag) {
                $tagsid[] = $tag->term_id;
            }
        }
        if(!emptyempty($catids)) {
            foreach($catids as $cat) {
                $catid[] = $cat->term_id;
            }
        }
        $args = array(
            'post_type' => 'post',
            'post__not_in' => $thisid,
            'ignore_sticky_posts' => 1,
            'posts_per_page' => ($num - $i),
            'tax_query' => array(
                'relation' => 'OR',//改成AND则必须是同标签同分类下
                array(
                    'taxonomy' => 'post_tag',
                    'field'    => 'term_id',
                    'terms'    => $tagsid,
                ),
                array(
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => $catid,
                ),
            ),
        );
        $rsp = get_posts($args );
        foreach($rsp as $sb){
            $output .= '<li><a href="' . get_permalink($sb->ID) . '">' . $sb->post_title . '</a></li>';//可自定义样式
            $i++;
        }
    }
    $final = '<h3>相关文章</h3><ul>' . $output . '</ul>';
    return $final;
}

调用方法
如需加入自定义相关文章,只需新建自定义栏目,加入文章id即可,多篇文章用,隔开

add_related_id
add_related_id

如想自定位置,并调整样式,则去掉the_content的钩子,然后手动调用wp_related_posts函数

骚年,创作吧。。。。

版权免责声明

① 本站VIP源码资源永久免费下载 www.wayu.cn;
② 本网站资源来源于网络收集,如有侵权,请联系站长进行删除处理;
③ 分享目的仅供大家学习和交流,请不要用于商业用途,否则后果自负;
④ 如果你有源码需要出售,可以联系小编,或者点击 投稿;
⑤ 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解;
⑥ 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需;
⑦ 在您的能力范围内,为了大环境的良性发展,请尽可能的选择正版资源;
⑧ 网站资源不做任何二次加密和授权后门(原版加密除外)。


站哈源码网 » wordpress相关文章的优化机智和自定义设置

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

发表评论

Copyright© 2022 by-站哈源码网 & 站内部分资源收集于网络,若侵犯了您的合法权益,请联系站长删除!
在 2.093 秒钟有 44 次查询。
开通VIP 享更多特权,建议使用QQ登录