zoukankan      html  css  js  c++  java
  • wordpress可以自定义获取相关文章的代码

    将下面的代码插入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(!empty($posttags)) {
                foreach($posttags as $tag) {
                    $tagsid[] = $tag->term_id;
                }
            }
            if(!empty($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即可,多篇文章用英文 , 隔开

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

    文章参考自http://www.2zzt.com/jcandcj/7280.html

  • 相关阅读:
    求大神回答这个管理系统不知道为啥不成功急!
    这个函数到底什么意思如何调用
    判断浮点数是否为零的问题
    字符串与列表的 常用方法
    变量名命名规范 运算符 流程控制
    ACM C++
    struts s:iterator循环遍历数据 自动生成序号
    JAVA将一个EXCEL多行订单产品字符串分解成一个个子订单 +连接符连接
    JS在HTML中获取到所有选中的checkbox的值
    自己做的java-WEB项目。希望360浏览器能够默认使用极速模式打开
  • 原文地址:https://www.cnblogs.com/hxqseo/p/5556058.html
Copyright © 2011-2022 走看看