WordPress.org

Ready to get started?Download WordPress

Forums

Finding multiple page ID's within Template (7 posts)

  1. RachelGoldstein71
    Member
    Posted 4 years ago #

    I would like to use a single template for multiple pages. All of those pages will have the same set of custom fields but the fields will have unique values on each of the pages. Right now, the template, which is so far only being used for one page, has this code:
    <?php $my_query = new WP_Query('page_id=6');
    How can I get it to discover the current page ID?

  2. MichaelH
    Member
    Posted 4 years ago #

    Typically $posts[0]->ID will hold the queried page id.

  3. RachelGoldstein71
    Member
    Posted 4 years ago #

    Thanks! Please pardon my ignorance, but how would I put that together with the code

    <?php $my_query = new WP_Query('page_id=6');

    Thanks again.

  4. MichaelH
    Member
    Posted 4 years ago #

    <?php $my_query = new WP_Query('page_id='.$posts[0]->ID);

    But not sure why you really need to query the ALREADY queried current page, as that page should be in the posts object--in fact it should be the only post there.

    Take a look at the wp-content/themes/default/page.php because that just displays the page so don't know why you would need more than the loop that is in that template.

  5. RachelGoldstein71
    Member
    Posted 4 years ago #

    The code in the current template (originally only being used for one page but now needs to be used for 20 pages) reads:
    <?php $my_query = new WP_Query('page_id=6')
    whether I'm on page 6 or 43 or whatever. It only pulls in the data from page 6. What I want is to be able to pull in data from the page I'm on. And use the same template for all. I tried the line of code above but to no avail.

  6. MichaelH
    Member
    Posted 4 years ago #

    Understood, but again if you go back and look at the page.php in the WordPress Default theme you will see how to display any page with just a simple loop, because WordPress automatically populates the posts object for you 'before' the page template is executed so you don't need to use a query to get the current page because WordPress has already done that for you.

  7. RachelGoldstein71
    Member
    Posted 4 years ago #

    I've tried the code from above and the code from page.php and no matter what I do the whole site fails to load when I change anything. Existing code, which appears several times on the template is:

    <?php $my_query = new WP_Query('page_id=6');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

    followed by some HTML/PHP that pulls in custom data, right now only from page 6.
    If anyone can help me out here, I'd really appreciate it.
    Thanks.
    -Rachel

Topic Closed

This topic has been closed to new replies.

About this Topic