Tagged: 

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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

    #1338626

    Hey 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,
    Ismael

    #1338675

    Hi 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

    #1338812

    Hi,

    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,
    Ismael

    #1339395

    Hi 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

    #1339533

    Hi,

    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,
    Ismael

    #1339643

    Hi 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.
    Cheers

    #1339724

    Hi,

    No problem. Glad to know that it is now working correctly. Please do not hesitate to open another thread if you need anything else.

    Have a nice day.

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Wrong post title when using shortcode’ is closed to new replies.