• Resolved theApe

    (@theape)


    I’ve got a custom post type which uses two batches of template file to display content in different ways depending on a (?) variable in the URL being viewed. What I need to do is disable the add_theme_support( ‘title-tag’ ); when a certain template file is being used, so I can insert a custom title for that post.

    The template file header.php will work as normal and pull the title from the core. But custom-header.php will not have the title in the wp_head(); and I’ll manually insert one into the <head>. Removing wp_head(); from the template isn’t an option and not something I really want to do.

    I can accomplish what I’m after if the custom post type was a page in the function.php with… if ( !is_page_template('custom-header.php') ) { add_theme_support( 'title-tag' ); }

    But it’s a post so this doesn’t work on this occasion and I can’t find an alternative with wp_title() being deprecated in the near future.

    The websiteurl.com/post shows the happy WordPress <title>Post Name</title>. And what I want is websiteurl.com/post?custom which uses the custom-header.php template file already to show <title>Manually Inserted</title> and not <title>Post Name</title>.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter theApe

    (@theape)

    I found this, but I don’t think it’ll help in my case as WP is only seeing the template files being used when the (?) variable isn’t being used. Regardless of whether it is or not.

    add_filter( 'template_include', 'var_template_include', 1000 );
    function var_template_include( $t ){
      $GLOBALS['current_theme_template'] = basename($t);
      return $t;
    }
    function get_current_template( $echo = false ) {
      if( !isset( $GLOBALS['current_theme_template'] ) ) return false;
      if( $echo ) echo $GLOBALS['current_theme_template']; else  return $GLOBALS['current_theme_template'];
    }

    With that I could then us Comparison Operators to hide or show the title theme support. This won’t work with my variables and I’m not sure if it’s even possible, but I thought I would leave this hear as it might help someone else with a similar problem.

    Thread Starter theApe

    (@theape)

    After a bit more research I figured it out… and it was staring me in the face.

    I put this in the top of my custom-header.php file and it does the job.

    function new_title_tag() {
        return 'Manually inserted new title';
    }
    add_filter('pre_get_document_title', 'new_title_tag');

    I can now customise the title depending on what template file is being used.

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

The topic ‘Modify title-tag based on specific post template file’ is closed to new replies.