最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - How to display Common posts from specific Tag & Category with Shortcode

programmeradmin0浏览0评论

How to display posts that are common in Category A and Tag A, and I want to display posts by shortcode so that the Shortcode can be used on multiple locations with different category Ids and Tags.

I found the below code by google but don't know how to implement it in functions.php and not sure how to use it in the shortcode. I want to display Post Title, And Some Custom Field Value [For Example - Value A, Value B] in a table structure.

        $args = array(
        'category__and' => 'category', //must use category id for this field
        'tag__in' => 'post_tag', //must use tag id for this field
        'posts_per_page' => -1); //get all posts
        $posts = get_posts($args);
        foreach ($posts as $post) :
       //do stuff 
        endforeach;```

How to display posts that are common in Category A and Tag A, and I want to display posts by shortcode so that the Shortcode can be used on multiple locations with different category Ids and Tags.

I found the below code by google but don't know how to implement it in functions.php and not sure how to use it in the shortcode. I want to display Post Title, And Some Custom Field Value [For Example - Value A, Value B] in a table structure.

        $args = array(
        'category__and' => 'category', //must use category id for this field
        'tag__in' => 'post_tag', //must use tag id for this field
        'posts_per_page' => -1); //get all posts
        $posts = get_posts($args);
        foreach ($posts as $post) :
       //do stuff 
        endforeach;```
Share Improve this question asked Jan 4, 2021 at 13:34 PuneetPuneet 477 bronze badges 3
  • Are you asking how to create a shortcode? Or how to retrieve post meta inside a loop? We have a 1 question per question policy, so be specific. You can always ask follow up questions on new questions – Tom J Nowell Commented Jan 4, 2021 at 16:56
  • I want to display common posts from specific Tag A and Category A, But this should happen with help of shortcode. Apologize for the lack of my explanation skill. – Puneet Commented Jan 4, 2021 at 17:21
  • I understood that, but you appear to be trying to do several things and combine them, but don't know how to do each part. You're asking 3 separate questions 1: "How do i create a shortcode?" 2: "How do I list posts in a category?" 3: "How do I display a custom field?", there are 3 separate questions, not 1. – Tom J Nowell Commented Jan 4, 2021 at 19:31
Add a comment  | 

1 Answer 1

Reset to default 0
function common_cats($att){
 $args = array(
        'category__and' => $att['category'], //must use category id
        'tag__in' => $att['tag'], //must use tag id for this field
        'posts_per_page' => $att['posts_per_page']); //get all posts
        $posts = get_posts($args);
        $output = "<ul>";
        foreach ($posts as $post) :
           $output .= "<li>".get_the_title(). "</li>";
        endforeach;
        $output .= "</ul>"; 
return $output;
}
add_shortcode('commoncats', 'common_cats');

use [commoncats] where you like to show the output of the above code. above code will return a list of titles.

发布评论

评论列表(0)

  1. 暂无评论