Forums

Dynamically changing the page title? (4 posts)

  1. nexusmedia
    Member
    Posted 8 months ago #

    How do I dynamically set the page title? I would like to specify the page title before wp_head is passed, so that the <title> tag will output whatever I want. I've searched for a while but can't find a simple solution.

    I don't want to display or return wp_title - I want to dynamically SET it before it's output to the page from within a custom content type, ie.

    set_wpl_title('My Custom Title');

    And when wp_head is called, all the other stuff is included plus

    <title>My Custom Title</title>

    Any simple way of achieving this?

  2. mnolf
    Member
    Posted 7 months ago #

    i'd like to do exactly the same thing.
    did you have any luck so far?

  3. nexusmedia
    Member
    Posted 7 months ago #

    Not really, but I created a bit of a hack to achieve what I was trying to accomplish:

    So wherever I wanted the customize the page title, I would set a global variable containing the required title and then call get_header():

    global $title;
    $title = 'My Custom Title';
    get_header(); ?>

    And then the header.php page looks like this:

    <? global $title; ?>
    <? if ($title): ?>
    <title><?=$title?></title>
    <? else: ?>
    <title>
    <? wp_title('');?>
    </title>
    <? endif; ?>

    This could be shorthanded with a ternary syntax:

    <? global $title; ?>
    <? ($title) ? $title : wp_title('') ;?>

    So not the cleanest solution, but it got the job done.

  4. mnolf
    Member
    Posted 7 months ago #

    for some reason, the short version doesn't work for me. the long one, however, works perfectly.
    thanks very much!

    (side note: when using the "all in one SEO" plugin, it needs to be deactivated on the concerned pages)

Reply

You must log in to post.

About this Topic

Tags