0

I want to add an image to a parent page and all it's children including 2nd level grandchildren. I don't want the image on any other page.

here's what I have so far:

if(is_page() || is_single()) {
global $post;
if ($post->post_parent == '27' || $post->ID == '27') { 
    $ntest='<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="'.get_stylesheet_directory_uri().'/images/nodal-clear-logo.png"></div>';
}else{ 
    $ntest='';
}

This works for 1st level and the parent, but not grandchildren. I thought about adding the 2nd level page ids, but thought there has be be a better way.

I feel like I'm missing something easy.

I'm writing this in a function that will add it to the content with the_content (full content below):

function rt_before_after($content) {
    if(is_page() || is_single()) {
      global $post;
      if ($post->post_parent == '27' || $post->ID == '27') { 
        $ntest='<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="'.get_stylesheet_directory_uri().'/images/nodal-clear-logo.png"></div>';
      }else{ 
        $ntest='';
      }
        $beforecontent = $ntest.'<h1 class="title">'.get_the_title().'</h1>';

        $fullcontent = $beforecontent . $content;
    } else {
        $fullcontent = $content;
    }

    return $fullcontent;
}
add_filter('the_content', 'rt_before_after');
  • Can you ellaborate on why you want this? It sounds like you're trying to recreate custom post type/taxonomy archives using pages and page templates – Tom J Nowell Aug 29 '18 at 18:38
  • It's all regular pages. On my site menu, I have 5 top-level menu items. On menu item 2 and all the pages in that parent tree I want to add an icon to those pages. It's basically that on theses pages we want to let people know that there's a slight difference to the feel of the site. – rudtek Aug 29 '18 at 19:05
3

You can use get_ancestors() to check if the post have an ancestor parent id or grandparent id etc..

But you would have to check every post for this...

function rt_before_after($content) {
    if(is_page() || is_single()) {
      global $post;
      $ancestors = get_ancestors( $post->ID, 'page' );
      if ( in_array( 27, $ancestors ) || $post->ID == '27') {
        $ntest='<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="'.get_stylesheet_directory_uri().'/images/nodal-clear-logo.png"></div>';
      }else{
        $ntest='';
      }
        $beforecontent = $ntest.'<h1 class="title">'.get_the_title().'</h1>';

        $fullcontent = $beforecontent . $content;
    } else {
        $fullcontent = $content;
    }

    return $fullcontent;
}
add_filter('the_content', 'rt_before_after');

You can go the other way around from the parent to children with get_pages()

And with this approach you can move it to seperate function and call it once and even cache it if you want. I would go for the second approach.

function rt_before_after($content) {
    if(is_page() || is_single()) {
      global $post;

      // get all the child pages and grandchild etc..
      $pages = get_pages( ['child_of'=>27] );
      $pages_ids = array_map( function( $item ) {
        return $item->ID;
      }, $pages );

      if ( in_array( $post->ID, $pages_ids ) || $post->ID == '27') {
        $ntest='<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="'.get_stylesheet_directory_uri().'/images/nodal-clear-logo.png"></div>';
      }else{
        $ntest='';
      }
        $beforecontent = $ntest.'<h1 class="title">'.get_the_title().'</h1>';

        $fullcontent = $beforecontent . $content;
    } else {
        $fullcontent = $content;
    }

    return $fullcontent;
}
add_filter('the_content', 'rt_before_after');

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.