1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to make WP show Post ID as <title> tag of post?

Discussion in 'Blogging' started by djp371, Jun 1, 2014.

  1. djp371

    djp371 Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2009
    Messages:
    1,472
    Likes Received:
    283
    Occupation:
    Internet marketing
    Location:
    United States
    Home Page:
    Hi Guys,

    Need your help, How to make Wordpress show Post ID as <title> tag of post, if the Title on post EDIT page is not filled?

    I have post located here domain.com/category/14412 - right now <title> looks like this <title> Domain.com | </title>

    I need make <title> tag show ID of post like this - <title> Domain.com | 14412</title>

    Any ideas?

    Thank You in advance!
     
  2. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,063
    Likes Received:
    2,872
    Gender:
    Male
    Is this only for the single page/post?

    Edit:

    See if this works:

    Code:
    
    function default_if_empty($title, $id) {
        if (!is_single() AND !is_page()) {
            return $title;
        }
        $temp = trim($title);
        if(empty($temp))
        {
            return get_bloginfo('url').' | '.$id;
        }
        return $title;
    }
    add_filter('the_title', 'default_if_empty', 10, 2);
    
    
     
    Last edited: Jun 1, 2014
  3. djp371

    djp371 Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2009
    Messages:
    1,472
    Likes Received:
    283
    Occupation:
    Internet marketing
    Location:
    United States
    Home Page:
    Only for post page :)

    Where should I add this one ? :)
     
  4. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,063
    Likes Received:
    2,872
    Gender:
    Male
    In that case, add the following to your functions.php

    Code:
    function default_if_empty($title, $id) {
        if (!is_single()) {
            return $title;
        }
        $temp = trim($title);
        if(empty($temp))
        {
            return get_bloginfo('url').' | '.$id;
        }
        return $title;
    }
    add_filter('the_title', 'default_if_empty', 10, 2);
    
    


     
    • Thanks Thanks x 1
  5. djp371

    djp371 Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2009
    Messages:
    1,472
    Likes Received:
    283
    Occupation:
    Internet marketing
    Location:
    United States
    Home Page:
    Thanks, only issue is that it added Title to the post BODY ( I need to avoid it, doesn't look good @design), but not this title into <title> tag.

    I need get this <title> tag on single post page: <title>BrandName | 14412</title>

    If this helps, i am using PageLines framework.


     
  6. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,063
    Likes Received:
    2,872
    Gender:
    Male
    My sincere apologies I meant to filter wp_title not the_title lol (I think I should leave this habit of taking MJ) :p
    Enter the following ode instead of the previous one

    Code:
    
    function default_if_empty($title) {
        if (!is_single()) {
            return  $title;
            
        }
        global $post;
        $temp = $title;
        if(empty($temp))
        {
            $title =  get_bloginfo('url').' | '.$post->ID;
            
        }
        return $title;
        
    }
    add_filter('wp_title', 'default_if_empty' , 9999,1);
    
     
  7. IMCapitalist

    IMCapitalist Jr. VIP Jr. VIP Premium Member

    Joined:
    Jan 21, 2014
    Messages:
    1,090
    Likes Received:
    60
    Location:
    Google Universe
  8. djp371

    djp371 Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2009
    Messages:
    1,472
    Likes Received:
    283
    Occupation:
    Internet marketing
    Location:
    United States
    Home Page:
    Doesn't work buddy :) FOR MJ - https://www.youtube.com/watch?v=TjlQ_pTPmPo

    @IMCapitalist - What works for you? Do you use pagelines framework?