Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress – modify title tag.

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #242421
    gulliver
    Participant

    I’ve previously used conditionals to modify the title tag (in the header), but since enabling title-tag support have no idea how to do some of the stuff I’d done when using wp_title().

    Specifically, I’m trying to add page numbers to multi-page posts and prepend some pages with the parent/grandparent name.

    #242592
    Ilan Firsov
    Participant

    Not really sure but from a little search (https://developer.wordpress.org/reference/hooks/wp_title/)[wp_title filter] should still work, maybe try that.

    <?php
    add_filter( 'wp_title', 'my_new_title', 10, 3 );
    function my_new_title( $title, $sep, $seplocation ) {
        return "123 {$sep} {$title}";
    }
    
    #242593
    gulliver
    Participant

    Thanks.
    Toward the bottom of that page is a note that wp_title is being deprecated and to approach things differently.

    As a non-coder, I’d just about managed to grasp stuff like

    if (is_404()) echo ‘file not available’;

    … but how to use the revised format of

    if (is_404()) {$title[‘title’] = ‘file not available’;}

    is still largely a mystery to me and don’t know how to appropriately revise previously-used code.

    #242594
    Ilan Firsov
    Participant

    This looks to be working:

    <?php
    add_filter( 'pre_get_document_title', 'my_404_title' );
    function my_404_title( $title ) {
        if( is_404() ) {
            return 'new 404 page title';
        }
    
        return $title;
    }
    
    #242596
    gulliver
    Participant

    Thanks. Appreciated. :-)
    Yes, that does work.
    I’ve read about pre_get_document_title but not yet been able to understand it enough to be able to use it.

    For example, my old header (which used wp_title) had a conditional to add page numbers to multi-page posts/pages.

    // Add a page number if necessary:
    if ($paged >= 2 || $page >= 2 ) echo ' ' . sprintf( __('(page %s)'), max($paged, $page) );
    

    I still don’t know how to modify things to include this with title-tag. And I’ve never been able to prepend some pages with the parent/grandparent name.

    #242598
    Ilan Firsov
    Participant

    Can you post the whole old function? I can’t really tell from where you are getting the $paged and $page variables and I don’t think they are available as global variables.

    #242599
    gulliver
    Participant

    Thanks, Ilan.

    Rather than a function, I’d been using conditionals within the title element of the header.php – an edited example of which is below.

    <title><?php
    
    // Set <title> tag based on what is being viewed.
    
    global $page, $paged;
    
    // Add the site name and separator.
    bloginfo('name'); echo ' | ';
    
    // Add the site description for the front page.
    $site_description = get_bloginfo('description', 'display');
    if ($site_description && (is_front_page())) echo "$site_description";
    
    // Show page title on posts page.
    if (is_home()) wp_title('');
    
    // Title for 404.
    if (is_404()) echo 'file not available';
    
    // Add a page number if necessary:
    if ($paged >= 2 || $page >= 2 ) echo ' ' . sprintf( __('(page %s)'), max($paged, $page) );
    
    // Show page title.
    if (is_page()) wp_title('');
    
    //else bloginfo('name');
    
    ?></title>
    
    
    #242600
    Ilan Firsov
    Participant

    I think you may be able to use the same logic in the filter you just need to return the new title at the end instead of echoing it
    Try this

    <?php
    add_filter( 'pre_get_document_title', 'my_custom_page_title' );
    function my_custom_page_title( $title ) {
        global $page, $paged;
    
        // Add the site name and separator.
        $new_title = get_bloginfo('name') . ' | ';
    
        // Add the site description for the front page.
        $site_description = get_bloginfo('description', 'display');
        if ($site_description && (is_front_page())) $new_title .= $site_description;
    
        // Show page title on posts page.
        if (is_home()) $new_title .= $title;
    
        // Title for 404.
        if (is_404()) $new_title .= 'file not available';
    
        // Add a page number if necessary:
        if ($paged >= 2 || $page >= 2 ) $new_title .= ' ' . sprintf( __('(page %s)'), max($paged, $page) );
    
        // Show page title.
        if (is_page()) $new_title .= $title;
    
    
        return $new_title;
    }
    
    #242601
    gulliver
    Participant

    Thanks.
    I can’t get it to work.

    General-template.php in core includes the line below, which seems to add the numbering to posts – but not pages.

    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    $title['page'] = sprintf( __( 'page %s' ), max( $paged, $page ) );
    }
    
Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.