admin管理员组文章数量:1302408
I am using WordPress and I have to show the related blog by category. I have created a custom-type post. I tried the below code but the code is displaying the last category of the post.
Would you help me out with this issue?
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');
Would you help me out with this issue?
I am using WordPress and I have to show the related blog by category. I have created a custom-type post. I tried the below code but the code is displaying the last category of the post.
Would you help me out with this issue?
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');
Would you help me out with this issue?
Share Improve this question asked Mar 8, 2021 at 5:00 Naren VermaNaren Verma 2491 gold badge6 silver badges19 bronze badges2 Answers
Reset to default 0I don't know below code is the best code but it's solved my issues
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');
if you do this way, category specific topics will be listed put it on the page and try
query_posts('category_name=product');
本文标签: pluginsHow to display the custom post related blog by category
版权声明:本文标题:plugins - How to display the custom post related blog by category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741696147a2393016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论