Ready to get started?Download WordPress


[resolved] Get current Page/Category from template using PHP? (3 posts)

  1. shastaw
    Posted 4 years ago #

    I'm using Pages to organize a web site with static and blog content on the same page. I do this by using static pages and then including a loop in the template that looks like this :

    <?php query_posts('category_name=testpage'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    [rest of normal display stuff here]

    This works fine if I make a new template for each page and manually change testpage in the code above to whatever the category is.

    But I'd like this template to be automatic, so that if I add a new page with name Pizza, and a new Category with name Pizza, I can just assign the same template and it would now display the Pizza category blog entries.

    I'm hoping there is some variable that holds the name of the page that this template was applied to which I could pick up and use as a variable in my code. Something like :

    $thispage = template_tag_that_gets_pagename()
    <?php query_posts("category_name=$thispage"); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    [rest of normal display stuff here]

    Anyone have a clue? I really don't want to have to make copy after copy of the template just to change this one little bit of information, but I haven't found anything that suggests how I can find out which page is currently being displayed in the template at runtime.


  2. MichaelH
    Posted 4 years ago #

    At the least $posts[0]->post_title or $posts[0]->post_title should get you there.

  3. shastaw
    Posted 4 years ago #

    Thank you!

    That appears to work outside any loops, where I needed it, and may be the fastest reply I've ever received on any forum, anywhere.

Topic Closed

This topic has been closed to new replies.

About this Topic