Support » Themes and Templates » add author’s name into title tag in header.php

  • i’m trying to tweak the title tag in header.php to the following structure:

    post author: post title >> blog name

    the system appears to ignore me.

    any suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’ve found it much less frustrating to ignore the title() function, instead of trying to fix it; it’s not tremendously flexible. You could try something like:

    <title>
    <?php if (is_single() ) {
    placeholder_author_function; echo ": "; single_post_title(); echo "&raquo;"; bloginfo('name');
    } else { wp_title(,true); }
    ?>
    </title>

    … and add in additional is_* functions for any other cases where you’re dissatisfied with the default title. (This is how I’ve done mine.)

    However, I’m not sure how to get at the identity of the post’s author outside the Loop.

    Three forum threads for the same Q. Hmm…

    As one of your forum posts noted, you can’t access post data outside The Loop with the regular template tags. I’d normally suggest using single_post_title(), but since you want title *and* author, we’ll do a little PHP.

    First we need to “query” the post content:

    $post = $wp_query->post;

    Easy enough. Now you can now access the post’s title like so:

    $post->post_title

    Author info is little harder to come by, since only user ID is contained in the post table. You can use a little plugin of mine to pull it in, or use a single line that queries the database for it:

    $author = $wpdb->get_var("SELECT user_nickname FROM $wpdb->users WHERE ID = '$post->post_author'");

    This gets nickname for the author ID in $post->post_author. If you need a full name (i.e. first and last), you’ll need to select them from the row:

    $author = $wpdb->get_row("SELECT user_firstname, user_lastname FROM $wpdb->users WHERE ID = '$post->post_author'");

    then call the elements like so:

    $author->user_firstname
    $author->user_lastname

    Now to pull it all together in a useful script (assuming nickname):

    <title>
    <?php if(is_single()) {
    $post = $wp_query->post;
    $author = $wpdb->get_var("SELECT user_nickname FROM $wpdb->users WHERE ID = '$post->post_author'");
    echo "$author: $post->post_title >> ";
    } ?>
    <?php bloginfo('name'); ?>
    </title>

    Thread Starter karpadio

    (@karpadio)

    this works just as i desire. thank you very much.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add author’s name into title tag in header.php’ is closed to new replies.