WordPress can customize the code to get related articles

Source: Internet
Author: User

Insert the following code into the functions.php

Get policies for related articles: Manually specify > tags > Categories > Random

//related articlesfunctionAdd_related_posts ($content){    return $content.wp_related_posts ();} Add_filter (' The_content ', ' add_related_posts ');//Hookfunctionwp_related_posts () {Global $post; $num= 5;//Number of articles    $counter= 1; $exclude _id= Get_post_meta ($post->id, ' related ',true);//get related articles for manual pinning    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> ';//Customizable Styles            $i++; }    }    if($i<$num){//after the number of custom articles is insufficient, it is processed by classification and labeling        $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 ',//change to and must be in the same category as the label                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> ';//Customizable Styles            $i++; }    }    $final= ' $output. ' </ul> '; return $final;}

If you want to add a custom related article, just create a new custom column, add the article ID, multiple articles in English, separated

If you want to customize the position and adjust the style, the_content then remove the hooks and then call the wp_related_posts function manually

Article reference from http://www.2zzt.com/jcandcj/7280.html

WordPress can customize the code to get related articles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.