Tagged: shortcode
-
AuthorPosts
-
February 2, 2022 at 4:31 pm #1338505
Hi there,
Hoping that you guys can help.
I created a bunch of shortcodes to create a post template and grab some elements automatically (like title ,post update, author,…) I already used this options in other sites and works like a charm, but not on this one.
The issue is that when i use them on a post and choose a category like blog the values showing are from the previous blog post from that category.Can you guys help me out? I’m stuck.
Thank you
Cheers
February 3, 2022 at 8:15 am #1338626Hey Kyle,
Thank you for the inquiry.
Are you editing the site? We are trying to make changes to the functions.php file, but we always get an error in the logoslider.php file, which we didn’t touch.
Please try to edit the sc_post_title shortcode, look for the $post->ID and replace it with get_the_ID() or the avia_get_the_ID() function. Try to do the same with the other shortcodes.
Best regards,
IsmaelFebruary 3, 2022 at 12:53 pm #1338675Hi Ismael,
Thank you so much for the quick replay.
Indeed I’m getting the same error. Meanwhile I was able to fix that.
The tip you gave (replaced by the avia_get_the_ID() function) made it all shortcodes working except one . The author. it still gets the wrong one.
function post_author_shortcode(){ global $post; ob_start(); $url= get_author_posts_url( get_the_author_meta( 'ID' )); $name= the_author(); ob_end_clean(); $output = '<a class="post-author-name" href="' . $url . '">' . $name . '</a>' ; return ($output ); } add_shortcode( 'sc_post_author', 'post_author_shortcode' );
Can you help figure the last one?
Thank you
February 4, 2022 at 6:03 am #1338812Hi,
Glad that the changes worked. Regarding the author, try to replace the the_author function with the get_the_author_meta function and use the display_name field.
// https://developer.wordpress.org/reference/functions/get_the_author_meta/
Best regards,
IsmaelFebruary 7, 2022 at 3:52 pm #1339395Hi Ismael,
I tried several options, and still grabs the wrong author.
function post_author_shortcode(){ global $post; ob_start(); $url= get_author_posts_url( get_the_author_meta( 'ID' )); $nameid= get_the_author_meta( 'ID', $author_id); $name= get_the_author_meta( 'display_name', $author_id); ob_end_clean(); $output = '<a class="post-author-name" href="' . $url . '">' . $nameid .' - '. $name . '</a>' ; return ($output); } add_shortcode( 'sc_post_author', 'post_author_shortcode' );
The result Updated on February 7, 2022 • by 23 – Chloe Scott • 7 minutes read
I checked and the author that should be showing is the ID 35 and not the 23. This is so strange. can you still help me figure this out.
Thank you
February 8, 2022 at 7:54 am #1339533Hi,
Thank you for the update.
The code above is not working because $author_id does not exist. You have to assign a value to that variable or just replace this block..
$nameid= get_the_author_meta( 'ID', $author_id); $name= get_the_author_meta( 'display_name', $author_id);
.. with:
$nameid = get_the_author_meta( 'ID' ); $name = get_the_author_meta( 'display_name' );
To get the ID of the author and assign it to $author_id, try this code.
$author_id = get_post_field( 'post_author', get_the_ID() );
Best regards,
IsmaelFebruary 8, 2022 at 6:18 pm #1339643Hi Ismael,
Thank you for the update. You were right I was doing wrong.
But with the change you send I also was not getting the right author id.So i tried the following
global $post; ob_start(); $author_id = get_post_field( 'post_author', avia_get_the_ID() ); $url= get_author_posts_url( $author_id ); $name= get_the_author_meta( 'display_name', $author_id); ob_end_clean(); $output = '<a class="post-author-name" href="' . $url . '">' . $name . '</a>' ; return ($output );
I replaced the get_the_ID() to avia_get_the_ID() and with that i was able get the correct author id of the post.
Thank you for your inputs and ways to help resolve this issue.
CheersFebruary 9, 2022 at 8:58 am #1339724 -
AuthorPosts
- The topic ‘Wrong post title when using shortcode’ is closed to new replies.