WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using page name in a query (3 posts)

  1. sonacoach
    Member
    Posted 3 years ago #

    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.

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    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
    */
    ?>
  3. sonacoach
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags