• Rachel Goldstein

    (@rachelgoldstein71)


    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?

Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

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

    Thread Starter Rachel Goldstein

    (@rachelgoldstein71)

    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.

    MichaelH

    (@michaelh)

    <?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.

    Thread Starter Rachel Goldstein

    (@rachelgoldstein71)

    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.

    MichaelH

    (@michaelh)

    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.

    Thread Starter Rachel Goldstein

    (@rachelgoldstein71)

    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Finding multiple page ID’s within Template’ is closed to new replies.