Flatsome theme article without the need for plugins

Code bài viết liên quan theme Flatsome không cần plugin

This article provides code for displaying related articles in the Flatsome theme without using plugins. The code can be copied and added to the functions.php file of the theme. It includes functions for showing related posts in the main content area and in the sidebar. Users can customize the number of related posts to display and use a shortcode to insert the related posts in the sidebar using a HTML widget. The article also offers instructions for editing the CSS style of the related articles section. Overall, this code snippet allows users to easily display related articles on their website.

Today, I’ll be sharing a code snippet for displaying related articles in the Flatsome theme without the need for a plugin. After inserting the code, you’ll see the results like the image below.

Just copy the formula and paste it into the functions.php file of your theme, and you’re good to go.

//code bài viết liên quan theme flatsome
function flatsome_related_post($content) {
    if(is_singular('post')) {
        global $post;
        ob_start();
        $categories = get_the_category($post->ID);
        if ($categories) {
            $category_ids = array();
            foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
            $args=array(
                'category__in' => $category_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>6,
                'ignore_sticky_posts'=>1
            );

            $my_query = new WP_Query( $args );
            if( $my_query->have_posts() ) {?>

                <?php } // end if has post
        } // end if $categories
        $related_post = ob_get_contents();
        ob_end_clean();
        return $content.$related_post;
    } //end if is single post
    else return $content;
}
add_filter('the_content', 'flatsome_related_post');

Code for Displaying Related Articles in the Sidebar

Based on a user’s request, here’s the code for displaying related articles in the sidebar. The demo looks like this:

Related Post in Sidebar

Insert the following code snippet into your functions.php file:

// code bài viết liên quan sidebar
function related_flatsome_sidebar() {
    if(is_singular('post')) {
        global $post;
        ob_start();
        $categories = get_the_category($post->ID);
        if ($categories) {
            $category_ids = array();
            foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
            $args=array(
                'category__in' => $category_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>3,
                'ignore_sticky_posts'=>1
            );

            $my_query = new WP_Query( $args );
            if( $my_query->have_posts() ) {?>

To use this code, go to Interface -> Widgets, drag the HTML widget into the desired sidebar, paste the shortcode [related_flatsome_sidebar] into the content box, and click Save. Refer to the image below.

See also  Choosing a standard domain name: Criteria to consider from the start

Related Post in Sidebar Setup

If you want to customize the appearance, you can do so in the CSS section of the code above. Best of luck with your related articles!

Rate this post

Related posts