New to PHP.

I have a custom page template that I want to use to show a list of posts. The idea is that each post has a category and I can provide a category to a page. For example let's say there are 5 posts with category "news" and a page called latest news, where I pass the "news" category as query-args. But then I can also use the same page template for another page, let's say with category "blog" to receive another category. So I believe the page template should read the category assigned to the page dynamically.

Here is the code I have so far. It's working for multiple categories as long as I pass them through an array. But then that's not dynamic, since the categories themselves are static.

The page is not a category page, just a normal page.

                          'posts_per_page' => 6,'orderby' => 'date')); 
  if(have_posts()) : while(have_posts()) : the_post(); ?>
   <div class="custom_page">
    <div class="latest_news_cont">

     <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>

     <!--  <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a> -->
     <div id ="helloworld" class="portfolio-work"><a href="<?php the_permalink() ?>" 
        '" style="text-decoration:none;">'<h2><?php the_title(); ?></h2></a></div>
     <?php the_excerpt(); ?>
     <p style="text-align:left;"><a href="<?php the_permalink() ?>" 
                                    class="readmore_news">CONTINUE READING </a></p>
     <div class="clear"></div>
  <?php endwhile; ?>
  <?php else : ?>

I found some info about custom taxonomy, but couldn't figure out how to use it. Would really appreciate any help here.

1 Answer 1


It sounds very much like you are trying to do something that Wordpress already does? There is a template called 'category.php' that you can use. It renders any Category of posts, so it accepts the category dynamically. Assuming you want all Category pages to render the same way, it would mean creating one file for any Category you have now or create in future.

Have a quick look at the Wordpress template hierarchy to see how this all works.

Template hierarchy

And also, here is a default Category template.

Category template

You can also create category specific templates using category names or IDs in the file name, to override the output. All explained in the above.

Does that help you in the right direction?

  • The page I am using is not a category page. Had it been a category page, this would have worked. When I create a post, I assign it a category and then pass that as a query_args param to a page to list it. There are many reasons I do it this way rather than straightaway using a category page. Commented Aug 2, 2020 at 14:19
  • No thats fair enough. The design and content of Category pages is harder to control so this makes sense. So you want one custom template that has a query that can accept a variable that is a particular Category ID? I guess you could do this either by query_vars (which would the be part of the URL) or by a custom field set manually for each page you use the template on?
    – t2pe
    Commented Aug 2, 2020 at 14:27
  • wordpress.org/support/article/custom-fields
    – t2pe
    Commented Aug 2, 2020 at 14:31

Your Answer

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

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