WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show author's first name and first letter/initial of last name. (7 posts)

  1. Kevin Kwok
    Member
    Posted 3 years ago #

    Within my index.php I have a line of code that shows a summary of a post and the author's first name. I would like to display the first name and last initial.

    example: "Barack O."

    line of code I have now: echo the_author_meta('first_name');

  2. Kevin Kwok
    Member
    Posted 3 years ago #

    I tried this :

    echo substr($the_author_lastname->last_name, 0, 1);

    and

    echo substr(the_author_lastname(),0,1);

    didnt do anything =(

  3. Mark / t31os
    Moderator
    Posted 3 years ago #

    Try this..

    $sep = ' '; // Seperator between first name and last initial (space currently)
    $ln_init = the_author_meta('last_name');
    echo the_author_meta('first_name') . $sep . $ln_init{0};
  4. Kevin Kwok
    Member
    Posted 3 years ago #

    hm tried that it seemed to to just display the the name like this

    ObamaBarack

    thanks though.

    This is a snippet of my code :

    foreach($posts as $post){
        setup_postdata($post);
        childtheme_post_header();
    
    if(get_post_meta($post->ID, 'full-image', $single = true))
    	echo '<img class="full-image" src="'.get_post_meta($post->ID, 'full-image', $single = true).'"/>';
    else
        echo '<img class="full-image" src="'.bloginfo('url').'" />';
        echo the_author_meta('first_name');
        echo ('&nbsp;');
        echo the_author_meta('last_name');
    }

    but my latest fix is:

    if(strlen($the_author_lastname->lastint) >1){
    echo substr(last_lastint(),0,1);
    }
    else{
    last_lastint();
    }
  5. Kevin Kwok
    Member
    Posted 3 years ago #

    I'm so sure I've seen this before, I think if I am unable to figure it out maybe I can edit each profile and add a nickname to be "Barack O." and just tell wordpress to display nickname, but I hope it does not come to that.

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    this worked in my setup (in the normal loop):
    <?php echo $authordata->first_name.' '.substr($authordata->last_name,0,1).'.'; ?>

    alternatively, using the_author_meta() and get_the_author_meta() :
    <?php the_author_meta('first_name'); ?> <?php echo substr(get_the_author_meta('last_name'),0,1); ?>.

    (ps: might need some adjustments to keep the output nice if some of the fields are empty)

  7. Kevin Kwok
    Member
    Posted 3 years ago #

    ALCHYMYTH
    youre a genius it worked. I used your alternate method it was just missing a space and the period after the last initial so I changed it a little to this:

    <?php echo the_author_meta('first_name'); ?>
    <?php echo ('&nbsp;'); ?>
    <?php echo substr(get_the_author_meta('last_name'),0,1).'.'; ?>

    Thanks you. Thank you. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic