<var id="xpn71"></var>
<ins id="xpn71"></ins>
<cite id="xpn71"><noframes id="xpn71">
<ins id="xpn71"></ins>
<cite id="xpn71"></cite><del id="xpn71"></del> <var id="xpn71"><dl id="xpn71"></dl></var>
<ins id="xpn71"><span id="xpn71"><listing id="xpn71"></listing></span></ins>
<cite id="xpn71"><dl id="xpn71"></dl></cite><ins id="xpn71"></ins>
<thead id="xpn71"><dl id="xpn71"><progress id="xpn71"></progress></dl></thead>
<var id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></var>
<listing id="xpn71"></listing>
<var id="xpn71"></var><cite id="xpn71"><dl id="xpn71"></dl></cite> <cite id="xpn71"><dl id="xpn71"></dl></cite><menuitem id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></menuitem>
<progress id="xpn71"><ruby id="xpn71"><span id="xpn71"></span></ruby></progress>
<ins id="xpn71"></ins>
<cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><video id="xpn71"><thead id="xpn71"></thead></video></cite>
<cite id="xpn71"></cite>
<ins id="xpn71"><dl id="xpn71"></dl></ins>
<thead id="xpn71"><dl id="xpn71"><address id="xpn71"></address></dl></thead>
<thead id="xpn71"></thead><cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><dl id="xpn71"></dl></cite>
<ins id="xpn71"></ins><address id="xpn71"><i id="xpn71"><th id="xpn71"></th></i></address>

如何在WordPress功能文件文件中自定義獲取相關文章代碼

WordPress, 免費教程   2018年07月20日 10:38  

前段時間我們有討論過到底是該選擇WordPress插件還是在functions.php中添加代碼?相對來說如果是需求的功能通過插件是可以滿足的,那么使用插件的那是無可厚非的。另外如果您所需要的功能不可用插件,同時通過添加代碼段又是非常簡單的情況,那么就可以選擇到功能文件中添加代碼段的方式了。今天就來看一看怎樣在functions.php中添加一段獲取相關文章的代碼。

將下面的代碼插入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函數。

以上方式雖然操作比較簡單,但最終還是針對網站需求和操作者有兩個要求,一是對獲取相關文章的需求比較重要,而是作者以前在functions.php中經常也會添加一些代碼,并且對代碼的功能比較熟悉。

 

 

 

您或許對下面這些文章有興趣:                    本月吐槽辛苦排行榜

看貼要回貼有N種理由!看帖不回貼的后果你懂得的!


評論內容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索欄

各類專題梳理

網站導航欄

X
?
返回頂部
<var id="xpn71"></var>
<ins id="xpn71"></ins>
<cite id="xpn71"><noframes id="xpn71">
<ins id="xpn71"></ins>
<cite id="xpn71"></cite><del id="xpn71"></del> <var id="xpn71"><dl id="xpn71"></dl></var>
<ins id="xpn71"><span id="xpn71"><listing id="xpn71"></listing></span></ins>
<cite id="xpn71"><dl id="xpn71"></dl></cite><ins id="xpn71"></ins>
<thead id="xpn71"><dl id="xpn71"><progress id="xpn71"></progress></dl></thead>
<var id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></var>
<listing id="xpn71"></listing>
<var id="xpn71"></var><cite id="xpn71"><dl id="xpn71"></dl></cite> <cite id="xpn71"><dl id="xpn71"></dl></cite><menuitem id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></menuitem>
<progress id="xpn71"><ruby id="xpn71"><span id="xpn71"></span></ruby></progress>
<ins id="xpn71"></ins>
<cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><video id="xpn71"><thead id="xpn71"></thead></video></cite>
<cite id="xpn71"></cite>
<ins id="xpn71"><dl id="xpn71"></dl></ins>
<thead id="xpn71"><dl id="xpn71"><address id="xpn71"></address></dl></thead>
<thead id="xpn71"></thead><cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><dl id="xpn71"></dl></cite>
<ins id="xpn71"></ins><address id="xpn71"><i id="xpn71"><th id="xpn71"></th></i></address>
青海快三昨天
<var id="xpn71"></var>
<ins id="xpn71"></ins>
<cite id="xpn71"><noframes id="xpn71">
<ins id="xpn71"></ins>
<cite id="xpn71"></cite><del id="xpn71"></del> <var id="xpn71"><dl id="xpn71"></dl></var>
<ins id="xpn71"><span id="xpn71"><listing id="xpn71"></listing></span></ins>
<cite id="xpn71"><dl id="xpn71"></dl></cite><ins id="xpn71"></ins>
<thead id="xpn71"><dl id="xpn71"><progress id="xpn71"></progress></dl></thead>
<var id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></var>
<listing id="xpn71"></listing>
<var id="xpn71"></var><cite id="xpn71"><dl id="xpn71"></dl></cite> <cite id="xpn71"><dl id="xpn71"></dl></cite><menuitem id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></menuitem>
<progress id="xpn71"><ruby id="xpn71"><span id="xpn71"></span></ruby></progress>
<ins id="xpn71"></ins>
<cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><video id="xpn71"><thead id="xpn71"></thead></video></cite>
<cite id="xpn71"></cite>
<ins id="xpn71"><dl id="xpn71"></dl></ins>
<thead id="xpn71"><dl id="xpn71"><address id="xpn71"></address></dl></thead>
<thead id="xpn71"></thead><cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><dl id="xpn71"></dl></cite>
<ins id="xpn71"></ins><address id="xpn71"><i id="xpn71"><th id="xpn71"></th></i></address>
<var id="xpn71"></var>
<ins id="xpn71"></ins>
<cite id="xpn71"><noframes id="xpn71">
<ins id="xpn71"></ins>
<cite id="xpn71"></cite><del id="xpn71"></del> <var id="xpn71"><dl id="xpn71"></dl></var>
<ins id="xpn71"><span id="xpn71"><listing id="xpn71"></listing></span></ins>
<cite id="xpn71"><dl id="xpn71"></dl></cite><ins id="xpn71"></ins>
<thead id="xpn71"><dl id="xpn71"><progress id="xpn71"></progress></dl></thead>
<var id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></var>
<listing id="xpn71"></listing>
<var id="xpn71"></var><cite id="xpn71"><dl id="xpn71"></dl></cite> <cite id="xpn71"><dl id="xpn71"></dl></cite><menuitem id="xpn71"><dl id="xpn71"><listing id="xpn71"></listing></dl></menuitem>
<progress id="xpn71"><ruby id="xpn71"><span id="xpn71"></span></ruby></progress>
<ins id="xpn71"></ins>
<cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><video id="xpn71"><thead id="xpn71"></thead></video></cite>
<cite id="xpn71"></cite>
<ins id="xpn71"><dl id="xpn71"></dl></ins>
<thead id="xpn71"><dl id="xpn71"><address id="xpn71"></address></dl></thead>
<thead id="xpn71"></thead><cite id="xpn71"></cite>
<thead id="xpn71"></thead>
<cite id="xpn71"><dl id="xpn71"></dl></cite>
<ins id="xpn71"></ins><address id="xpn71"><i id="xpn71"><th id="xpn71"></th></i></address>