admin管理员组

文章数量:1125467

I am trying to change this wordpress function given in my theme to show posts from category instead of tags.

But due to wordpress beginner i am unable to do this.

Please help me to change this code.

<div id="tabs_content_container2">
                            <div id="tab3" class="tab_content2" style="display: block;">
                                <ul class="mv_list_small">
                                <?php  
                                    $orig_post = $post;  
                                    global $post;  
                                    $tags = wp_get_post_($post->ID);  
                                    if ($tags) {  
                                    $tag_ids = array();  
                                    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;  
                                    $args=array(  
                                    'tag__in' => $tag_ids,  
                                    'post__not_in' => array($post->ID),  
                                    'posts_per_page'=>10, // Number of related posts to display.  
                                    'ignore_sticky_posts'=>1  
                                    );  
                                    $my_query = new wp_query( $args ); 
                                    $num=1; 
                                    while( $my_query->have_posts() ) {  
                                    $my_query->the_post();
                                ?>
                                    <li><a href="<?php the_permalink(); ?>">

and after searching on internet i also got the code for related post to show from category. But unable to replace it correctly. Here is the code from internet.

How to show related posts by category

help me to change my default tag code with the category code.

Thank you.

I am trying to change this wordpress function given in my theme to show posts from category instead of tags.

But due to wordpress beginner i am unable to do this.

Please help me to change this code.

<div id="tabs_content_container2">
                            <div id="tab3" class="tab_content2" style="display: block;">
                                <ul class="mv_list_small">
                                <?php  
                                    $orig_post = $post;  
                                    global $post;  
                                    $tags = wp_get_post_($post->ID);  
                                    if ($tags) {  
                                    $tag_ids = array();  
                                    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;  
                                    $args=array(  
                                    'tag__in' => $tag_ids,  
                                    'post__not_in' => array($post->ID),  
                                    'posts_per_page'=>10, // Number of related posts to display.  
                                    'ignore_sticky_posts'=>1  
                                    );  
                                    $my_query = new wp_query( $args ); 
                                    $num=1; 
                                    while( $my_query->have_posts() ) {  
                                    $my_query->the_post();
                                ?>
                                    <li><a href="<?php the_permalink(); ?>">

and after searching on internet i also got the code for related post to show from category. But unable to replace it correctly. Here is the code from internet.

How to show related posts by category

help me to change my default tag code with the category code.

Thank you.

Share Improve this question edited Feb 2, 2024 at 20:05 MikeNGarrett 2,6711 gold badge20 silver badges29 bronze badges asked Jan 20, 2018 at 11:07 SheelendraSheelendra 11 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 0

In the function wp_get_post_terms() you can select the taxonomy. the default is post_tag you can change it to category.

And in the WP_Query args you need to change the tag__in to category__in.

// get the categories
$categories = wp_get_post_terms($post->ID, 'category');
if ($categories) {
    $categories_ids = array();
    foreach($categories as $individual_cat) $categories_ids[] = $individual_cat->term_id;
    $args=array(
        'category__in' => $categories_ids, // Select posts from this categories
        'post__not_in' => array($post->ID),
        'posts_per_page'=>10, // Number of related posts to display.
        'ignore_sticky_posts'=>1
    );

    $my_query = new wp_query( $args );
    $num=1;
    while( $my_query->have_posts() ) {
        $my_query->the_post();

        ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                ...
            </a>
        </li>
        <?php
    }
    wp_reset_postdata();
}

Hello thanks for shibi's answer.

But while searching the same thing on internet i got exactly what i needed.

If someone want to have a look check here.

Initially my code was this and using tags to display related posts.

<?php $orig_post = $post; global $post; $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=>5, // Number of related posts that will be shown. 'caller_get_posts'=>1 ); $my_query = new wp_query( $args ); if( $my_query->have_posts() ) { echo '<div id="relatedposts"><h3>Related Posts</h3><ul>'; while( $my_query->have_posts() ) { $my_query->the_post(); ?> <li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div> <div class="relatedcontent"> <h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> <?php the_time('M j, Y') ?> </div> </li> <? } echo '</ul></div>'; } } $post = $orig_post; wp_reset_query(); ?>

and i wanted to change this to related post by categories. As i am a beginner i didn't know to play with wordpress php thing.

But fortunately i got what i needed.

<?php $orig_post = $post; global $post; $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'=> 2, // Number of related posts that will be shown. 'caller_get_posts'=>1 ); $my_query = new wp_query( $args ); if( $my_query->have_posts() ) { echo '<div id="related_posts"><h3>Related Posts</h3><ul>'; while( $my_query->have_posts() ) { $my_query->the_post();?> <li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div> <div class="relatedcontent"> <h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> <?php the_time('M j, Y') ?> </div> </li> <? } echo '</ul></div>'; } } $post = $orig_post; wp_reset_query(); ?>

you need to scroll to check the complete code else you can check the below page.

http://www.wpbeginner.com/wp-themes/how-to-add-related-posts-with-a-thumbnail-without-using-plugins/

Just change

wp_get_post_tags()

TO

get_the_category()

That's all

get_the_category() will help you to get category by ID. There is no need to change anything else in your code even if variable name is $tags. They are just variables, nothing else in your case

本文标签: wp queryHow do I show related posts from categories instead of tags