0

I have this piece of code that works great: it retrieves a particular page from my WordPress database so I can wrap custom code around it in a template. In the instance below it grabs the page called Showreel:

        <?php
        $page = get_page_by_title('Showreel');
        ?>          
        <?php
             $my_id = $page;
             $post_id = get_post($my_id, ARRAY_A);
             $title = $post_id['post_title'];
             $content = $post_id['post_content'];
        ?>
        <h3><?php echo $title;?></h3>                       
        <?php 
            echo $content
        ?>

However, I have conundrum - I have some pages that do not have unique names. How can I retrieve a page only if it is a direct descendant of a certain parent e.g. I would like to retrieve a page under Showreel called The Ruth Rendell Mysteries. My initial thought was that the code below would work, but it doesn't.

        <?php
        $page = get_page_by_title('Showreel->The Ruth Rendell Mysteries');
        ?>          
3
  • did you have a look at: codex.wordpress.org/Function_Reference/get_page_by_path
    – Michael
    Commented Jun 21, 2011 at 18:52
  • That's excellent. Thanks very much Michael, the following worked... <?php $page = get_page_by_path('Showreels/The Ruth Rendell Mysteries'); ?> <?php $my_id = $page; $post_id = get_post($my_id, ARRAY_A); $title = $post_id['post_title']; $content = $post_id['post_content']; ?> <?php echo $content ?>
    – SparrwHawk
    Commented Jun 21, 2011 at 19:35
  • Whoops, it won't let me format the code correctly. Michael, if you post your suggestion as an answer I'll tick it so you get kudos - thanks v much.
    – SparrwHawk
    Commented Jun 21, 2011 at 19:36

2 Answers 2

0

have a look at:

get_page_by_path()

codex.wordpress.org/Function_Reference/get_page_by_path

1

As you already discovered, the get_page_by_title function does not accept arguments in that fashion. However, if you look through the available functions at the Wordpress Docs site, you will find get_page_children():

http://codex.wordpress.org/Function_Reference/get_page_children

In the code examples, they retrieve the page object with the get_page_by_title function and then get all of the children of that page. From there, you could separate out the page you wanted.

To see other functions and how they are used take a few minutes to browse through the rest of the Function Reference for some good examples.

Hope this helps and good luck!

1
  • Hi, That article seems useful and I've tried to incorporate it it into my code. I've successfully made it spit out meta-data about the posts, but can't get any further than this unfortunately. I don't have much experience with WordPress coding.
    – SparrwHawk
    Commented Jun 21, 2011 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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