0

I have been attempting to get a solution to work that was previously posted for this question 1

I created the template as posted:

<?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();

When I select this template for a page, 'view page' displays only the theme header and footer with nothing in between.

I suspect the template code is incomplete... or do I need to do something in the page itself? Or maybe the theme I'm using is keeping this from working? Suggestions?

BTW, I want these displayed just as the theme displays individual category pages - trying to use plugins like wp_list_category_posts will not do this.

6
  • Have you actually displayed the post data, e.g. add the_title(); after that //Echo the post line?
    – Sally CJ
    Feb 21 at 0:43
  • Thank you @SallyCJ - I'm just a script kiddie here but suspected I needed somthing to actually output the post at that point and had tried some things that didn't work. I don't know how to display the full post data in the right format. Adding 'the_title()' per your suggestion does print all the post titles in black over black, all run together, with none of the other page stuff (page title, background, etc.). So clearly your suggestion is on the right track, but incomplete!
    – Thorigol
    Feb 21 at 1:00
  • You will need to learn about The Loop and functions you can use to display the post data. But as for this - "I want these displayed just as the theme displays individual category pages" - it might be easier for you to copy the category template and modify it accordingly (e.g. to make the new WP_Query call), so what theme are you using? Does it have any of the category templates?
    – Sally CJ
    Feb 21 at 1:29
  • However, if you just wanted to display posts in the categories 1, 5 and 9, then you can see it at <your site URL>?cat=1,5,9, e.g. https://example.com?cat=1,5,9 .. which also means you could create a rewrite rule which redirects internally to that page. I.e. Without a URL change and without having to creating a Page or custom page template.
    – Sally CJ
    Feb 21 at 1:39
  • Oh, that query is so simple and exactly what I needed. THANK YOU
    – Thorigol
    Feb 21 at 2:17

1 Answer 1

0

I was seeing nothing in between the header&footer because I did not actually display the posts in my custom query, which can be done using template tags like the_title() and the_content(). More details here:

https://developer.wordpress.org/themes/basics/the-loop/

But then I found out that I could simply use the cat parameter in the URL to view posts in certain categories, e.g. https://example.com?cat=1,5,9 to view posts in the categories 1, 5 and 9.

So it was then easy to just stick the query URL in a menu item's custom link, and I no longer needed to use the custom template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.