admin管理员组文章数量:1425861
looking to create a page that will show all posts from several categories. for example lets say I want all of my posts from category 1, 5, and 9 together. the posts on the page should be ones from any of those categories and aren't required to be in multiple of them or all of them. is it possible to do this?
looking to create a page that will show all posts from several categories. for example lets say I want all of my posts from category 1, 5, and 9 together. the posts on the page should be ones from any of those categories and aren't required to be in multiple of them or all of them. is it possible to do this?
Share Improve this question asked May 31, 2019 at 18:38 ZakZak 131 silver badge3 bronze badges1 Answer
Reset to default 2To display a custom query in a single page you would need to create a new page template and assign that to a page.
- Create a file in your theme's main folder and call it multiple-categories.php
Paste the following code in the file
<?php /* Template Name: Multiple Categories */ get_header(); $args = array( 'cat' => '1, 5, 9', 'posts_per_page' => -1, ); $my_posts = new WP_Query( $args ); if( $my_posts->have_posts() ){ while( $my_posts->have_posts() ){ $my_posts->the_post(); //Echo the post } } wp_reset_postdata(); get_footer();
Create a new page and in the template field select "Multiple categories".
Visiting that page you should see the articles from any of those categories. The template file and name is just an example and can be changed at will without using the default ones listed here https://developer.wordpress/themes/basics/template-hierarchy/
More details about the arguments for the query relative to categories can be found here: https://codex.wordpress/Class_Reference/WP_Query#Category_Parameters
本文标签: How to create a page that shows posts from multiple categories
版权声明:本文标题:How to create a page that shows posts from multiple categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745447000a2658708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论