WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Using page name in a query

[Resolved] Using page name in a query

  • I’m using wp_query with custom fields to get the specific set of posts I want for a page. I’m doing this because I believe categories don’t work at the page level – not sure if that’s true but I think I’d have the same issue.
    Rather than writing a separate page php for each one, it would make sense for me to pick up the page name and use it in the wp-query, so I would only need one php file. Is that possible? I can’t find a way so far.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Indeed pages cannot have categories. But with wp query you can show all posts of a specific category on a Page. You do not need custom fields for this. You can use the query in your theme’s page.php or make a custom page template for these posts. make a duplicate of your theme’s page.php and rename it to your category like news.php and put this on top of that page:

    <?php
    /*
    Template Name: news
    */
    ?>

    The link to custom page template appears to have my answer. Thanks for pointing me in the right direction.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Using page name in a query’ is closed to new replies.
Skip to toolbar