I'm building a search results template and want to display the parent section of the website for each result returned. so at the bottom of each search result excerpt it'd say "Found in [section] | Reacd More"

Something like: http://www.foxglovecovert.org.uk/search/results/search&keywords=local/

  • How are you defining "section"? Category, parent page, template, something super cool that I don't even know exists? – Matthew Boynes Jan 28 '12 at 15:33
  • I'd like "blog", "gallery"(custom post format), "event"(custom post format), "about"(parent page) – Bent Jan 28 '12 at 19:59

For your custom post types and your blog, you can use get_post_type(). For your About parent page, if the pages only go one level deep, you can check $post->post_parent to see if it's the about page. Otherwise, you can use get_post_ancestors() and check that array to see if your About page is in it!

  • Thanks. I think I can write the function I need from here. – Bent Jan 28 '12 at 20:48
function my_get_highest_parent( $id ) {
    $cat = get_category( $id );
    $parent = $cat->parent;

    if( $parent == 0 ) 
        return $id;
        my_get_highest_parent( $parent );

That will get you the highest level parent (the category with parent 0) based on the category-id you feed into it

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.