1

I have created a wordpress query which takes the entire content from 1 particular page using the its title

So far i have the following:

<?php
query_posts('pagename=features');
if (have_posts()) :
while (have_posts()) : the_post();?>

<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<h3 class="center">No Section available</h3>
<p class="center"><?php _e("No Section available"); ?></p>
<?php endif;
//Reset Query
wp_reset_query();
?>

The main issue is that there is numerous pages which care called 'features' but they all have different parents. Is it possible to do this via parent somehow?

For example, here is my hierarchical page structure:

products > designgo silver > features products > designgo silver > screenshots products > designgo silver > benefits

products > designgo gold > features products > designgo gold > screenshots products > designgo gold > benefits

products > designgo platinum > features products > designgo platinum > screenshots products > designgo platinum > benefits

If i could do something like:

<?php
query_posts('pagename=features&parent=designgosilver');
if (have_posts()) :
while (have_posts()) : the_post();?>

that would be perfect,

can anyone give us a pointer?

Cheers!

0

You are almost there—but please, use WP_Query.

$args = array(
    'pagename' => 'features',
    'post_parent' => PARENT-PAGE-ID-HERE,
);
$query = new WP_Query($args);
if ($query->have_posts()) :
    $query->the_post();

    // ...

    wp_reset_postdata();
endif;

// EDIT:
If you don't want to or cannot use the parent page's ID, you can access it by its title, for instance like so:

    'post_parent' => ($parent = get_page_by_title('Designgo Silver')) ? $parent->ID : 0,
  • 2
    WP_QUERY also allows you to write something like this WP_Query('pagename=designgogold/features') - not sure if that's available in query_posts. – gdaniel Feb 13 '14 at 15:19
  • Thanks very much for your help. That seems absolutely perfect but its not working exactly. Am i doing something wrong here: <?php $args = array('pagename' => 'features', 'post_parent' => 352); $query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php endwhile; ?> <?php else : ?> <h3 class="center">No Section available</h3> <p class="center"><?php _e("No Section available"); ?></p> <?php endif; //Reset Query wp_reset_query(); ?> – ste Feb 13 '14 at 15:51
  • Well, you didn't exactly use the code I provided, but how does this not work? I just tested this, again, and I'm able to query a page by its slug and its parent. Don't you have any pages? Do you get not just the one you would like? – tfrommen Feb 13 '14 at 16:18
  • 1
    Hi, i have completed it using the code you provided & all seems to work perfectly now. I also have been able to query using WP_Query('pagename=designgogold/features') which is just what a i needed!! Thank you very much :-) – ste Feb 14 '14 at 14:45
  • Alright, could you then please mark this answer as accepted. Otherwise your question will haunt the site from time to time... ;) – tfrommen Feb 14 '14 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.