如何按类别显示与博客相关的自定义帖子?

时间:2021-03-08 作者:Naren Verma

我正在使用WordPress,我必须按类别显示相关的博客。我已经创建了一个自定义类型的帖子。我尝试了下面的代码,但代码显示的是文章的最后一个类别。

你能帮我解决这个问题吗?

function relatedBlogPost($atts){
global $post;
$custom_terms = get_terms(\'blogs_cat\');
foreach($custom_terms as $custom_term) {
    $args = array(
        \'post_type\' => \'blog\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 6,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'blogs_cat\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug
            ),
        ),
        \'post__not_in\' => array ($post->ID),
        //\'order\' => \'DEC\'
     );

$loop = new WP_Query($args);
     if($loop->have_posts()) {
$data=\'\';
$data .= \'<ul>\';
while($loop->have_posts()){
      $loop->the_post();
/*get category name*/
$terms = get_the_terms( $loop->ID , \'blogs_cat\' );
foreach ( $terms as $term ) {
$catname=$term->name;
}
  $data.= \'<li> <a href="\'.get_permalink().\'">
                <div class="main-blogBoxwrapper">
                <img src="\'.get_the_post_thumbnail_url().\'">
                <div class="blogCatname">
                <h6><span>\'.$catname.\'</span></h6>
                <h4>\'.wp_trim_words(get_the_title(), 14, \'...\').\'</h4>
                <p>\'.wp_trim_words(get_the_excerpt(), 20, \'...\').\'</p>
                </div>
                </div>
                </a></li>\';
   }
    $data.=\'</ul>\';
    return $data;
wp_reset_postdata();

}
}

}
add_shortcode( \'related-blog-post\', \'relatedBlogPost\');
你能帮我解决这个问题吗?

2 个回复
SO网友:Naren Verma

我不知道下面的代码是最好的代码,但它解决了我的问题

function relatedBlogPost($atts){

        global $post;
       //this the the custom post type 
    
        $args_cat = array( 
            \'taxonomy\'     => \'blogs_cat\',
            \'orderby\'      => \'name\',
            \'show_count\'   => 1,
            \'pad_counts\'   => 1, 
            \'hierarchical\' => 1,
            \'echo\'         => 0
        );
        $categories = get_the_terms($post->ID, "blogs_cat"); // getting the category id of the current post
        $args = array(
                \'post_type\' => \'blog\',
                \'post_status\' => \'publish\',
                \'posts_per_page\' => 3,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'blogs_cat\',
                        \'field\' => \'slug\',
                        \'terms\' => $categories[0]->slug // sending the current category slug name
                    ),
                ),
                \'post__not_in\' => array ($post->ID),
                \'order\' => \'DEC\'
            );
        
        $related = new WP_Query($args);
        if( $related->have_posts() ) { 
            $data=\'<h2 class="sectionHeading pb-5">Related Blog</h2>\';
            $data .= \'<div class="main-Blog"><ul>\';
            while( $related->have_posts() ) { 
                $related->the_post(); 
                //displaying the category name
        $terms = get_the_terms( $related->ID , \'blogs_cat\' );
        foreach ( $terms as $term ) {
        $catname=$term->name;
        }
                
     $data.= \'<li> <a href="\'.get_permalink().\'">
                            <div class="main-blogBoxwrapper">
                            <img src="\'.get_the_post_thumbnail_url().\'">
                            <div class="blogCatname">
                            <h6><span>\'.$catname.\'</span></h6>
                            <h4>\'.wp_trim_words(get_the_title(), 14, \'...\').\'</h4>
                            <p>\'.wp_trim_words(get_the_excerpt(), 20, \'...\').\'</p>
                            </div>
                            </div>
                            </a></li>\';
            }
            $data.=\'</ul></div>\';
            return $data;
            wp_reset_postdata();
        }
        
        }
        add_shortcode( \'related-blog-post\', \'relatedBlogPost\');

SO网友:Mehmet Cemil

如果您这样做,将列出特定于类别的主题将其放在页面上,然后重试

query_posts(\'category_name=product\');