简体   繁体   中英

How can I get all the posts of a category in Wordpress

I am trying to get in PHP using wordpress, to be able to list my posts that contain the name of my category

Example: I have the category fruit and vegetables And within those categories I have an article about Best Vegetables and Best Fruits, and I would like to print the name of the category, but that the link will take me to the post about the best fruit or vegetables.

I only managed to get the categories.

foreach($categories as $category) {
   echo '<div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>';
}```

You can use 'category_name' in parameters. get_posts() docs

// Get posts by category name
    $posts = get_posts( [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'category_name'  => 'csharp',
    ] );
    // Get posts by category id
    $posts = get_posts( [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'category'       => '1,2,3',
    ] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM