WordPress.org

Ready to get started?Download WordPress

Forums

add author's name into title tag in header.php (4 posts)

  1. karpadio
    Member
    Posted 8 years ago #

    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?

  2. yami
    Member
    Posted 8 years ago #

    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.

  3. Kafkaesqui

    Posted 8 years ago #

    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>

  4. karpadio
    Member
    Posted 8 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags