ThemeShaper Forums » Thematic

[closed]

Customize Page Posttitle to include Ancestor/Parent Page Info

(1 post)
  • Started 3 years ago by wlanni
  • This topic is not resolved
  1. wlanni
    Member

    Hello!

    I'm building out a luxury bed site.

    Basically, what I needed to do was on specific bed model pages using a specific template I created, I needed to include the bed brand name before the model name. On the parent brand page, I didn't want the brand name to repeat itself. And then on other pages using the same template (like accessories pages, etc) I didn't want them to try to grab their parent page title.

    I found a really great function that uses get_post_ancestors to check if a page is a child of another page. I combined that then with rewriting thematic_postheader_posttitle.

    Was there a better/more efficient way of writing this so that I ONLY had to write the if statements for my custom page templates, vs having to include ALL the cases for everything else (404, is_single, is_page, etc)?

    Also, I tried originally to write this as a function called childtheme_override_postheader_posttitle but I couldn't figure out how to get that to work, so I gave up.

    Here's what I did:
    First function pulled from here: Conditional Tags > WordPress Codex

    //function to check if page is child of another page
    function is_tree($pid) { // $pid = The ID of the page we’re looking for pages underneath
    	global $post; // load details about this page
    	$anc = get_post_ancestors( $post->ID );
    	foreach($anc as $ancestor) {
    		if(is_page() && $ancestor == $pid) {
    			return true;
    		}
    	}
    	if(is_page()&&(is_page($pid)))
    		return true; // we’re at the page or at a sub page
    	else
    		return false; // we’re elsewhere
    };

    Then I wrote in some if statements to check for the page template, and to rewrite posttitles on children of specific pages:

    //update product page titles to include brand name
    function product_page_has_brandname()  {
    	global $post;
    //Had to check if a page is_page, and then also make sure it isn't my specific template, to write the normal <h1> title
         if (is_single() || is_page() && !is_page_template('products.php')) {
    	        $posttitle = '<h1 class="entry-title">' . get_the_title() . "</h1>\n";
    
    //check if page is using my template, and then is part of one of the brand trees
    	    } elseif (is_page_template('products.php') && is_tree(73) ) {
    	    	if (is_page(73)) {
    	    		$posttitle = '<h1 class="entry-title"><img src="';
    	    		$posttitle .= get_stylesheet_directory_uri();
    	    		$posttitle .= '/images/logo-hastens-bk.png" alt="' . get_the_title(73) . '" width="166" height="51" /></h1>';
    	    	} else {
    	    		$posttitle = '<h1 class="entry-title"><span class="page-ancestor">' . get_the_title(73) . '</span> ' . get_the_title() . "</h1>\n";
    	    	}
    	     } elseif (is_page_template('products.php') && is_tree(71)) {
    	    	if (is_page(71)) {
    	    		$posttitle = '<h1 class="entry-title"><img src="'.get_stylesheet_directory_uri() . '/images/logo-vispring-bk.png" alt="' . get_the_title(71) . '" width="174" height="43" /></h1>';
    	    	} else {
    		    	$posttitle = '<h1 class="entry-title"><span class="page-ancestor">' . get_the_title(71) . '</span> ' . get_the_title() . "</h1>\n";
    	    	}
    	    } elseif (is_404()) {
    	        $posttitle = '<h1 class="entry-title">' . __('Not Found', 'thematic') . "</h1>\n";
    	    } else {
    	        $posttitle = '<h2 class="entry-title"><a href="';
    	        $posttitle .= apply_filters('the_permalink', get_permalink());
    	        $posttitle .= '" title="';
    	        $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    	        $posttitle .= '" rel="bookmark">';
    	        $posttitle .= get_the_title();
    	        $posttitle .= "</a></h2>\n";
    	   }
       return $posttitle;
    } // end postheader_posttitle
    add_filter('thematic_postheader_posttitle','product_page_has_brandname');
    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.