• I’ve tried various hooks like wp_title and pre_get_document_title but I just can’t get it to work.

    I’m trying to modify my post type archive template page title like this

    		   add_filter('pre_get_document_title', 'new_filter_title');
    
    		  function new_filter_title($title)
    		  
    		  {
    			if ( is_page('1637') && $id = get_queried_object_id() )
    			{
    		  
    			  $areas = get_query_var('area');
    			  $area = get_term_by('slug', $areas, 'area');
    			  $models = get_query_var('serie');
    			  $model = get_term_by('slug', $models, 'serie');
    		  
    			  $title = '';
    		  
    			  if($model && $model) $title .= $model->name . ' Used';
    			  else $title .= 'Used';
    		  
    			  $title .= ' Cars For Sale';
    		  
    			  if($area && $area) $title .= ' In ' . $area->name;
    
    			  $title .= ' on ' . get_bloginfo('name');
    		  
    		  
    		  
    			  return $title;
    			}
    		  
    			return $title;
    		  } 

    What am I missing?

    I’d like to add that, I also have an SEO plugin installed (Yoast)

    • This topic was modified 7 years, 4 months ago by Nkululeko.
    • This topic was modified 7 years, 4 months ago by Nkululeko.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I believe Yoast overrides the usual themed page title, and thus your code is also overridden. IIRC, you can turn this feature off in Yoast settings? Then your code ought to work, but it really depends on your theme. You have the correct code for most modern themes that rely upon built in theme support for titles. Your theme could be the unusual theme that does titles differently.

    Just a hunch, you might try passing a large priority argument so your callback executes last.
    add_filter('pre_get_document_title', 'new_filter_title', 9999 );

    For specifics on working with titles when Yoast is active, I suggest you ask in their dedicated support forum.

    Thread Starter Nkululeko

    (@oieniuz)

    You’re absolutely right. The theme titles are done differently. this is the code they use to display the title.

    }
    
    	$alignment                           = get_post_meta($post_id, 'alignment', true);
        $title_style_h1                      = array();
        $title_style_subtitle                = array();
        $title_box_bg_color                  = get_post_meta( $post_id, 'title_box_bg_color', true );
        $title_box_font_color                = get_post_meta( $post_id, 'title_box_font_color', true );
        $title_box_line_color                = get_post_meta( $post_id, 'title_box_line_color', true );
        $title_box_custom_bg_image           = get_post_meta( $post_id, 'title_box_custom_bg_image', true );
        $title_tag                           = (empty(get_post_meta( $post_id, 'title_tag', true ))) ? 'h2' : get_post_meta( $post_id, 'title_tag', true );
        $sub_title                           = get_post_meta( $post_id, 'sub_title', true );
        $breadcrumbs                         = get_post_meta( $post_id, 'breadcrumbs', true );
        $breadcrumbs_font_color              = get_post_meta( $post_id, 'breadcrumbs_font_color', true );
        $title_box_subtitle_font_color       = get_post_meta( $post_id, 'title_box_subtitle_font_color', true );
    	$sub_title_instead                   = get_post_meta($post_id, 'sub_title_instead', true);
    
    	if( empty($alignment) || is_post_type_archive(stm_listings_post_type()) ) {
    		$alignment = 'left';
    	}

    I think this the actual code that displays the title.

    $title_tag = (empty(get_post_meta( $post_id, 'title_tag', true ))) ? 'h2' : get_post_meta( $post_id, 'title_tag', true );

    Is there a possibilty to hook into it?

    Another solution I might be thinking of, is instead of trying to hook into the pages title, how about I echo (as an H1 title) the yoast meta-title (I’ve set using the same code with wpseo_title)

    I’ve tried it with this code

    echo get_post_meta($post->ID, '_yoast_wpseo_title', true);

    But I think the _yoast_wpseo_title key returns the title of post-type and not the custom title I’ve just made

    I’ve also tried it with this code…

    <h1 class="archive-title">  --------------> this is Line 3
    <?php
    if ( is_tax() ) :                                                                   
        $taxonomy = get_queried_object()->taxonomy;
        $term_id = get_queried_object()->term_id;
        $meta   = get_option( 'wpseo_taxonomy_meta' );
        $title  = $meta[$taxonomy][$term_id]['wpseo_title'];
        //printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_taxonomy_meta' ), 1 ) );
        if ( isset($meta) && !empty($title) ) :         
            echo apply_filters( 'the_title', $title );
        else :
            single_term_title();    
        endif;
    endif;                              
    ?>
    </h1> 

    This keeps returning a Parse error: syntax error, unexpected '<', expecting end of file in .......................stm_classic_filter.php on line 3 no matter what I do

    What would you suggest?

    Moderator bcworkz

    (@bcworkz)

    The title_tag code from your theme appears to be only the size header tag to use (h2, h3, etc.), not the actual title. You can use what ever source for title you want, yoast, your own version, the standard WP version from get_documant_title() or wp_title(), etc. No matter which you use, you are still faced with overriding the default placed by your theme. If it’s not evident on the theme’s header template or one of the related template parts, the title could be generated on the “wp_head” action. To see all functions called with this action, dump out the global $wp_filter array. You only need the part keyed “wp_head”. There will be quite a few listed. Any prefixed with wp_ are of course WP core. You’re looking for theme functions, hopefully they are consistently prefixed and easy to see.

    Then it’s a matter of finding the function declaration in theme source code and determining which is responsible for the title. Once you find the right function, you can remove it from the action stack and replace it with your own. Of course you can only remove after it is added. Hooking “after_theme_setup” should be late enough to remove the theme’s hook.

    If you still have trouble determining how to override, I suggest you ask the theme devs through their support channel.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘how to hook into a page’s title?’ is closed to new replies.