[Resolved] Remove link for the author archive page on author name (specific posts)

Home Forums Support Remove link for the author archive page on author name (specific posts)

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1392174
    Joey

    I use this code to add guest authors:

    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
     
    function guest_author_name( $name ) {
    global $post;
     
    $author = get_post_meta( $post->ID, 'guest-author', true );
     
    if ( $author )
    $name = $author;
     
    return $name;
    }

    Then in custom fields I add a “guest-author” field. This way I don’t have to create a contributor account for writers who only posts one time.

    The problem is that their name at the top of the page links to the real author (the generic “guest author” account) page and puts the wrong name at the top (the most recent post). I don’t mind having a generic author page for all the guest writers but it can’t have the wrong name. For example: https://simipress.com/author/simieditions/

    Is there a way to modify the above snippet so that these guest authors don’t have the default link to the author page on their name?

    #1393100
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You may be able to use this filter:

    add_filter( 'author_link', function( $link ) {
        $author = get_post_meta( get_the_ID(), 'guest-author', true );
    
        if ( $author ) {
            $link = 'custom link';
        }
    
        return $link;
    } );
    #1393401
    Joey

    Thank you Tom, for the quick reply. I got the error:

    Your PHP code changes were rolled back due to an error on line 107 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘;’

    Line 107 is this:
    $author = get_post_meta( get_the_ID(), ‘guest-author’, true );

    #1394266
    Tom
    Lead Developer
    Lead Developer

    Sorry about that – can you try the updated code?

    #1394584
    Joey

    Weird, it works well on Firefox, but for Chrome or mobile it takes me to custom20%link as a separate page saying “This site can’t be reached.” Any idea why?

    If this is too much trouble, another solution might be to just link guest authors to a static page (my contributor page).

    #1395764
    Tom
    Lead Developer
    Lead Developer

    Did you update custom link with the full URL where the link should go?

    #1396365
    Joey

    Oh, sorry I’m being stupid. That works. If I want to remove the hyperlink completely (nothing happens when they click on the author name) for guest authors, how would I modify the code? I know I can use a hashtag or just leave it blank, but then it reloads the page. Thank you.

    #1397422
    Tom
    Lead Developer
    Lead Developer

    In that case, you could try this:

    add_filter( 'generate_post_author_output', function( $output ) {
        $author = get_post_meta( get_the_ID(), 'guest-author', true );
    
        if ( $author ) {
            return sprintf( '<span class="byline">%1$s<span class="author vcard" %3$s><span class="author-name" itemprop="name">%2$s</span</span></span> ',
                apply_filters( 'generate_inside_post_meta_item_output', '', 'author' ),
                esc_html( get_the_author() ),
                generate_get_microdata( 'post-author' )
            );
        }
    
        return $output;
    } );
    #1397708
    Joey

    Very cool, it works. Thank you very much. I need to learn PHP.

    #1397946
    Tom
    Lead Developer
    Lead Developer
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.