[Resolved] Custom author page

Home Forums Support [Resolved] Custom author page

Home Forums Support Custom author page

Viewing 11 posts - 16 through 26 (of 26 total)
  • Author
    Posts
  • #1168212
    Tom
    Lead Developer
    Lead Developer

    You could try this instead:

    add_filter( 'generate_page_hero_post_author', function() {
        global $post;
        $author_id = $post->post_author;
        $url = get_author_posts_url( $author_id );
    
        if ( 1 === $author_id ) {
            $url = 'YOUR CUSTOM URL';
        }
    
        if ( 2 === $author_id ) {
            $url = 'YOUR CUSTOM URL';
        }
    
        return sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><a class="url fn n" href="%1$s" title="%2$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%3$s</span></a></span>',
            esc_url( $url ),
            esc_attr( sprintf( __( 'View all posts by %s', 'gp-premium' ), get_the_author_meta( 'display_name', $author_id ) ) ),
            esc_html( get_the_author_meta( 'display_name', $author_id ) )
        );
    } );
    #1169784
    Dana

    Hey Tom. Sadly non of these options are working 100%.

    Option 1 below does work if I use the search results page and click on the author but does not work from my post pages.

    add_filter( 'author_link', function( $link, $author_id ) {
        if ( 1 === $author_id ) {
            return 'CUSTOM URL';
        }
    
        if ( 2 === $author_id ) {
            return 'CUSTOM URL';
        }
    
        return $link;
    }, 10, 2 );

    Option 2 below is the same as option 1 functionality; works from search results page but not post pages.

    add_action( 'wp', function() {
        add_filter( 'author_link', function( $link, $author_id ) {
            if ( 1 === $author_id ) {
                return 'CUSTOM URL';
            }
    
            if ( 2 === $author_id ) {
                return 'CUSTOM URL';
            }
    
            return $link;
        }, 10, 2 );
    }, 50 );

    Option 3 below doesn’t work from search results page or my post pages.

    add_filter( 'generate_page_hero_post_author', function() {
        global $post;
        $author_id = $post->post_author;
        $url = get_author_posts_url( $author_id );
    
        if ( 1 == $author_id ) {
            $url = 'CUSTOM URL';
        }
    
        if ( 2 == $author_id ) {
            $url = 'CUSTOM URL';
        }
    
        return sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><a class="url fn n" href="%1$s" title="%2$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%3$s</span></a></span>',
            esc_url( $url ),
            esc_attr( sprintf( __( 'View all posts by %s', 'gp-premium' ), get_the_author_meta( 'display_name', $author_id ) ) ),
            esc_html( get_the_author_meta( 'display_name', $author_id ) )
        );
    } );

    Options 1 & 2 are close but there still seems to be an issue with the Elements Post Hero Header I’m using with the {{author}} template tag as it still points to //mywebsite.com/author/author-name from my posts pages. From the search results page, hovering/clicking on the author name points to //mywebsite.com/custom-author-page.

    #1170564
    Tom
    Lead Developer
    Lead Developer

    You need to have the first function and the last function.

    The first function will do it for your archives, while the last function will do it for the page heroes.

    The first function should do it everywhere, but if it’s not working for page heroes, that last function is necessary.

    #1171048
    Dana

    I get it now Tom. Thanks. I work on my site in the evenings and the brain is not firing on on cylinders after working all day.

    The first function works for archives no problem.

    However the third function was still returning the wrong URL. I tested the function by replacing the third parameter (author name) with the URL parameter and it printed out //mywebsite.com/author/author-name/ where the author name would normally show on the hero page and was still //mywebsite.com/author/author-name if I hovered over the the full url. This helped me troubleshoot if the function was actually working.

    So I changed the PHP operator from ‘===’ (identical) to ‘==’ (equal) and it worked giving me //mywebsite.com/custom-author-page when I hovered and clicked the author name.

    Thanks for your time and patience on this one while I brush up on my PHP chops.

    #1171825
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad you got it working! 🙂

    #1768348
    Victor

    Hey Tom,

    Does this still work? I tried it but it doesn’t seem to be working like it worked for the archive pages. I created a custom page at domain.com/authorname but when I go to domain.com/author/authorname, it is a different page.

    #1768628
    David
    Staff
    Customer Support

    Hi there,

    that code simply replaces the Link in the archives so it will point to the ‘url’ added to the filter. It doesn’t redirect or replace the default author archive.

    #1769403
    Victor

    So do you think a 301 redirect is the best way to replace default author archives with the custom page I have created?

    Thanks!

    #1770066
    David
    Staff
    Customer Support
    #1770444
    Victor

    David sir,

    Can you give me the code for this?

    So normal archives are located at domain.com/authors/johndoe. I want this to be replaced by domain.com/john-doe. I am specifying this so that you can change the code appropriately in the case of author archives similar to category ones. There is no dash in the normal author archives url because it renders it based on the username of the author.

    Thanks in advance.

    #1771512
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Is john-doe a static page? Or are you just trying to remove the author part of the URL?

    If it’s a static page, setting up 301 redirects is the way to go. Not sure there’s a magic function for this.

Viewing 11 posts - 16 through 26 (of 26 total)
  • You must be logged in to reply to this topic.