• Resolved VierasTalo

    (@vierastalo)


    Hi, our website currently uses Hueman. I was wondering is it possible to add a Twitter handle behind the written by XX -portion at the top of a post? For example, this post: https://www.laajakuva.com/by-our-selves-2015/ currently says “KIRJOITTANUT MIIKKA MONONEN · 06.11.2015”. We would like it to say “KIRJOITTANUT MIIKKA MONONEN @TwitterHandle · 06.11.2015”. We have multiple authors so there would have to be some way to add a Twitter-handle for each writer so it would automatically be selected when that person posts and it would retroactively pop up in the older posts. Is this possible?

Viewing 7 replies - 1 through 7 (of 7 total)
  • bdbrown

    (@bdbrown)

    Hi Vieras. There are probably several ways to do this but here’s a fairly simple solution:
    1. In the user profile, add @TwitterHandle to the the user Nickname option. So you’d have “usernickname @TwitterHandle”.
    2. Change the “Display name publicly as” option to use the new nickname.

    Thread Starter VierasTalo

    (@vierastalo)

    Hi bdbrown, and thanks for your help again. However, there’s a slight issue there; I would like for the @TwitterHandle -portion to directly link to Twitter as well, to that user. Is it possible?

    bdbrown

    (@bdbrown)

    Give this a try:

    1. Add this to your child theme functions.php file (adapted from this post). This should add an extra author meta field at the bottom of the user profile:

    //========== Add Extra TWITTER Field with user profile ==============
    add_action( 'show_user_profile', 'my_extra_user_profile_fields' );
    add_action( 'edit_user_profile', 'my_extra_user_profile_fields' );
    add_action( 'personal_options_update', 'my_save_extra_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_user_profile_fields' );
    
    function my_save_extra_user_profile_fields( $user_id )
        {
        if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
        update_user_meta( $user_id, 'author_twitter', $_POST['author_twitter'] );
        }
    
    function my_extra_user_profile_fields( $user )
        { ?>
        <h3>Extra Custom Meta Fields</h3>
        <table class="form-table">
            <tr>
                <th><label for="author_twitter">Twitter User Name</label></th>
                <td>
                    <input type="text" id="author_twitter" name="author_twitter" size="20" value="<?php echo esc_attr( get_the_author_meta( 'author_twitter', $user->ID )); ?>">
                    <span class="description">Please enter your Twitter Account User name, eg: @TwitterHandle</span>
                </td>
            </tr>
        </table>
    <?php }

    Note that there is no closing php tag at the end of the functions.

    2. Copy single.php to your child theme.

    3. Change this line:

    <p class="post-byline"><?php _e('by','hueman'); ?> <?php the_author_posts_link(); ?> &middot; <?php the_time(get_option('date_format')); ?></p>

    to this:

    <p class="post-byline"><?php _e('by','hueman'); ?> <?php the_author_posts_link(); ?> &middot; <a href="<?php the_author_meta( 'author_twitter'); ?>"><?php the_author_meta('author_twitter'); ?></a> &middot; <?php the_time(get_option('date_format')); ?></p>
    Thread Starter VierasTalo

    (@vierastalo)

    Hi. This is is so close to working! I’ve now added a Twitter handle to myself, and it shows up on the page: https://www.laajakuva.com/by-our-selves-2015/ However, the link links to https://www.laajakuva.com/by-our-selves-2015/VierasTalo rather than https://twitter.com/VierasTalo which it should link to. For now I’ve left the page as is so you can see if you can decipher the problem therein. The meta box showed up on the account pages as it should’ve.

    bdbrown

    (@bdbrown)

    Oops; my fault; sorry about that.

    Change this part of the line I posted above:

    <a href="<?php the_author_meta( 'author_twitter'); ?>"><?php the_author_meta('author_twitter'); ?></a>

    to this:

    <a href="https://www.twitter.com/<?php the_author_meta( 'author_twitter'); ?>"><?php the_author_meta('author_twitter'); ?></a>
    Thread Starter VierasTalo

    (@vierastalo)

    Thank you, now it works! However, there still is one issue; Due to me adding @ at the beginning of the line, the @ now shows up everywhere, despite said authors not having a Twitter-profile. Is there any way to have the @ before the handle and not have it show up if the author hasn’t added a profile link? The code with the @ looks like this:

    <p class="post-byline"><?php _e('Kirjoittanut','hueman'); ?> <?php the_author_posts_link(); ?> · <a>">@<?php the_author_meta('author_twitter'); ?></a> · <?php the_time(get_option('date_format')); ?></p>
    For the time being I removed the @.

    Thread Starter VierasTalo

    (@vierastalo)

    Oh, wait! I “fixed” it, as in I put in my profile with @vierastalo instead of just VierasTalo and now it shows up as intended. I’m marking this as closed for now. Thanks a lot for your help again, bdbrown!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding a Twitter handle behind the Written by -portion?’ is closed to new replies.