• Upgrading an old WordPress 3.7.17 site template and there is some questionable code that I’m not clear on entirely and wondering if someone can make a bit more sense of it and help to simplify it for 4.7.1?

    <title><?php
    	// Print the <title> tag based on what is being viewed.
    	global $page, $paged;
    
    	// Add the blog name.
    	bloginfo( 'name' );
    
        if ( is_page() && $post->post_parent && !is_home()) {
            if (!is_front_page()) {
                if ($urlSplits[2] != '' && $urlSplits[3] != '') {
                    $grandparent_get = get_post($post->post_parent);
                    $grandparent = $grandparent_get->post_parent;
                    echo ' | ' . get_the_title($grandparent) . ' &rsaquo; ';
                } else {
                    echo ' | ';
                }
                    echo get_the_title($post->post_parent) . ' &rsaquo; ';
                }
                the_title();
            } else {
                if (is_home()) {
                    the_title();
                } else if (!is_front_page() && !is_home() && !is_404()) {
                    echo ' | ';
                    the_title();
                } else if (is_404()) {
                    echo ' | Page Not Found';
                }
            }
    
    	// Add the blog description for the home/front page.
    	$site_description = get_bloginfo( 'description', 'display' );
    	if ( $site_description && ( is_home() || is_front_page() ) )
    		echo " | $site_description";
    
    	?></title>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The grandparent title bit? I don’t know what $urlSplits are, but it clearly influences which title gets applied in some cases. Generally speaking they are trying to use an ancestor’s title in the case of subordinate posts. No idea why. Maybe a more logical approach is immediately check the post’s parent ID and if it’s non-zero, get the parent post. Reapply the same logic until a zero parent is found, indicating the oldest ancestor. The process could be wrapped up in a little re-entrant function to further clean up the template.

    Thread Starter wyclef

    (@wyclef)

    Hmmm… is there a boilerplate title tag example you could point me to that might help me rebuild this? This is what I’m thinking. I think their home page title is different? They are doing something with !is_front_page that I am missing? It seems as though for is_home they are adding the site description? Ugh.

         <?php
    function getPageTitle() {
        global $post;  
      
        if (($post->post_parent != 0) && !is_search()) {
            $title = ' — '.get_the_title($post->post_parent).' ? '.get_the_title(); 
        } else if (is_single()) {
            $title = get_the_title($post->ID);
        } else if (is_404()) {
            $title = ' | Page Not Found';
        } else {
            $title = ' | '.get_the_title();
        }
        return $title;
    }   ?>
    <title><? bloginfo('name'); ?> <? echo getPageTitle(); ?></title>
    • This reply was modified 8 years, 2 months ago by wyclef.
    • This reply was modified 8 years, 2 months ago by wyclef.
    • This reply was modified 8 years, 2 months ago by wyclef.
    • This reply was modified 8 years, 2 months ago by wyclef.
    • This reply was modified 8 years, 2 months ago by wyclef.
    Moderator bcworkz

    (@bcworkz)

    You could just grab the title code from the header.php file of earlier twenty* themes that ship with the WP download. It’s going to only use the current page title, it’s not going to care what ancestors there are.

    The most recent themes however simply add _wp_render_title_tag() to the wp_head action. As you can see by following the link, it just wraps the result of wp_get_document_title() in title tags.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Updating old PHP for WP Title Tag’ is closed to new replies.