Support » Fixing WordPress » How to display custom post type title before the loop on the page as an H1 headi

  • Resolved Nkululeko

    (@oieniuz)


    So I have modified my Custom Post Type $title to display certain terms. See the code

    add_filter('wpseo_title', 'vehicle_listing_title');
    
    function vehicle_listing_title( $title ) 
    {
      if ( is_post_type_archive('vehicles') && $id = get_queried_object_id() ) 
      {
       
         
        $locations = get_query_var('location');
        $location = get_term_by('slug', $locations, 'vehicle_location');
        $models = get_query_var('model');
        $model = get_term_by('slug', $models, 'vehicle_model'); 
        
        $title = '';
    
        if($model && $model) $title .= $model->name . ' used';
        else $title .= 'Used';
    
        $title .= ' cars for sale';
    
        if($location && $location) $title .= ' in ' . $location->name;
    
        $title .= ' on ' . get_bloginfo('name');
    
        return $title;
      }
    
      return $title;
    }

    The code functions perfectly, but now I want to echo the exact title to show as an h1 heading in the beginning of the loop. how do I do this? I’ve tried it get_post_meta(), I dont think its the right function for this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could try adding the same function to the archive title filter:

    add_filter( 'get_the_archive_title', 'vehicle_listing_title' );

    Or modifying the template to call your custom function where you want it to appear.

    Thread Starter Nkululeko

    (@oieniuz)

    The issue is… my Custom post type does not have the title displayed. I don’t think get_the_archive_title will hook when there is no title. I basically want to echo the vehicle_listing_title as an H1 heading, or create one to match

    • This reply was modified 6 years, 12 months ago by Nkululeko.
    Thread Starter Nkululeko

    (@oieniuz)

    Don’t mind my last reply. Your solution worked perfectly. Thanks alot man.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display custom post type title before the loop on the page as an H1 headi’ is closed to new replies.