Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found this snippet in a different thread and it mostly does what I need, but I'm having trouble wrapping the individual pages this outputs with tags. All I can seem to add is a line break. I don't have a lot of experience with php so unfortunately I wasn't able to customize it to my needs. In the end I just need the firsts and second level page titles to be displayed and they need to be wrapped in individual h2's. Thanks in advance.

    //This is the loop that pulls banner titles
function print_page_parents($reverse = true){
    global $post;

    //create array of pages (i.e. current, parent, grandparent)
    $page = array($post->ID);
    $page_ancestors = get_ancestors($post->ID, 'page');
    $pages = array_merge($page, $page_ancestors);

    if($reverse) {
        //reverse array (i.e. grandparent, parent, current)
        $pages = array_reverse($pages);
    }

    for($i=0; $i<count($pages); $i++) {
        $ban_titles.= get_the_title($pages[$i]);
        if($i != count($pages) - 1){
            $ban_titles.= " <br /> ";
        }
    }

    echo $ban_titles;
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

To wrap your titles in h2 just change echo $ban_titles; to echo '<h2>'. $ban_titles . '</h2>';.

If you want to limit number of titles displayed to 2 change for($i=0; $i<count($pages); $i++) to for($i=0; $i<2; $i++).

Hope it helps, PHP is not so scary after all :)

[edit]

How about that?

    //This is the loop that pulls banner titles
function print_page_parents($reverse = true){
    global $post;

    //create array of pages (i.e. current, parent, grandparent)
    $page = array($post->ID);
    $page_ancestors = get_ancestors($post->ID, 'page');
    $pages = array_merge($page, $page_ancestors);

    if($reverse) {
        //reverse array (i.e. grandparent, parent, current)
        $pages = array_reverse($pages);
    }

    $i = 0;
    if(count($pages)==1) {

    echo '<h2>'. get_the_title($pages[$i]) .'</h2>';

    } else {

    echo '<h2>'. get_the_title($pages[$i]) .'</h2>';
    echo '<h2>'. get_the_title($pages[$i+1]) .'</h2>';

    }

 }
share|improve this answer
    
Hi Bart, thanks for the quick response. I implemented your solutions but unfortunately they didn't solve this issue completely. The h2 tags were displaying but it wrapped both the 1st and 2nd level outputs. I'm trying to wrap h2's around each level individually. I've been trying to work in a foreach loop but have been unsuccessful. Now for the amount of page titles being displayed only shows 2 page titles as needed but when I'm on a top level page it displays the top level page title twice. Thanks for giving it a shot. :) –  Clint Hubbard Jr Feb 1 '13 at 4:40
    
I'm thinking in order to wrap each page title individually I will need to change some of the code here: ' if($i != count($pages) - 1){ $ban_titles.= " <br /> "; } ' since I'm able to add html tags after $ban_titles but not before for some reason. –  Clint Hubbard Jr Feb 1 '13 at 4:46
    
Sorry, you are right I was rushing it a bit and it's almost 7 am where I live. Check the edit, I hope it finally works for you :) Have a good night (or a day)! :) –  Bart Karp Feb 1 '13 at 5:56
    
Hey Bart, you rock man! That last edit works perfectly! I can't thank you enough. I learned quite a bit from this one. Cheers! –  Clint Hubbard Jr Feb 1 '13 at 6:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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