Archived topic
Pls help modify author
25 replies · Started by crosby87 on March 10, 2022
Hi,
It does not work anymore in any of the blog posts. It now redirects me to the home page.
Hi Crosby,
I tested the code and from my end it’s working.
Can you kindly check if the user ID you’re using is correct? Please note that the ID here if ( '2' === $author_id) should be correct for the code to work as it should.
Here is an article which may assist you with regards to this: https://wpsmackdown.com/find-wordpress-user-id/#:~:text=You%20can%20use%20the%20%E2%80%9CSearch,search%20by%20name%20or%20username.&text=The%20number%20immediately%20following%20user_id,the%20top%20of%20your%20screen.
Kindly let us know how it goes. :)
Hi Fernando,
Sorry, you are right. It seems I needed to cache my website again.
However, it still not perfect.
In the initial code provided by Jing the link worked on "normal" blog posts. Then I told her that in the blog posts where I have a unique GP Header Element, there it does not work.
Then she provided a new code. Now it only works in those blog posts where I have a unique header element, but it does not work on "normal" blog posts.
I would like it to work on BOTH. Could you please help?
See examples in private.
Kind follow-up on this. I see other posts are being answered, so I am wondering if this has been accidentally missed.
Thanks!
Hi there,
do you still have the code that Ying provided here added to your site:
https://generatepress.com/forums/topic/pls-help-modify-author/#post-2150112
Hi David,
No, I don't. I added the new one she has given:
add_filter( 'generate_page_hero_post_author', 'my_hero_author');
function my_hero_author($author) {
global $post;
$author_id = $post->post_author;
if ( '2' === $author_id) {
$author = sprintf(
'<span class="author vvcard" 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(home_url( '/rolam') ),
/* translators: author name */
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 ) )
);
return $author;
}
return $author;
}
Do I need to include both?
Yes, you will need both
Hi David,
So I included the below:
#1 code provided by Ying
add_filter( 'generate_post_author_output', function( $output ) {
$author_name = get_the_author_meta( 'display_name' );
$custum_author_link = home_url( '/rolam' );
if ( 2 === get_the_author_meta( 'ID' ) ) {
return '<a href='.$custum_author_link.'>'.$author_name.'</a>';
}
return $output ;
});
#2 code provided by Ying:
add_filter( 'generate_page_hero_post_author', 'my_hero_author');
function my_hero_author($author) {
global $post;
$author_id = $post->post_author;
if ( '2' === $author_id) {
$author = sprintf(
'<span class="author vvcard" 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(home_url( '/rolam') ),
/* translators: author name */
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 ) )
);
return $author;
}
return $author;
}
So both of them are included and now they work fine, links redirect to the about page as they should.
When I include the first, however, there seems to be a bug. The word "Author" or in my language "Szerzo" disappear from before the name of the author.
See two screenshots:
- This is without the code: https://imgur.com/a/uQ4ZKlM
- This is when I include the first code: https://imgur.com/a/hGbPREw
I tested it and it only happens when I include the first code. Second code alone does not cause this issue.
You can replace the #1 code with below code:
add_filter( 'author_link', 'my_author_link');
function my_author_link($link) {
if ( 2 === get_the_author_meta( 'ID' )) {
$link = home_url( '/rolam');
return $link;
}
return $link;
}
Works perfectly, thanks a lot
You are welcome :)