skip to Main Content

I am building a block theme for WordPress.

I wrote a shortcode function that is supposed to output the message "By [author’s display name with author’s link]".
More specifically, I am trying to use it in the search result loop (main loop for the search result template).

function auteur_par(){
    $auteur = get_the_author();
    $auteur_nom = get_the_author_meta('display_name', $auteur);
    $auteur_url = get_the_author_meta('user_url', $auteur);
    return '<p>Par <a href="' . $auteur_url . '" target="_self" class="wp-block-post-author-name__link">' . $auteur_nom . '</a></p>';
}
add_shortcode('auteur_par', 'auteur_par');

I also tried using get_the_author_meta directly:

function auteur_par(){
    $auteur_nom = get_the_author_meta('display_name');
    $auteur_url = get_the_author_meta('user_url');
    return '<p>Par <a href="' . $auteur_url . '" target="_self" class="wp-block-post-author-name__link">' . $auteur_nom . '</a></p>';
}
add_shortcode('auteur_par', 'auteur_par');

Both don’t work work when I use it as a shortcode within the "Modèle de publication" block (I can’t find what the english name is for this block, it’s the block under the query loop block in the outline). On the page, the paragraph is there, but the variables are empty.
I made a similar function using get_the_modified_date() and that one works perfectly. I don’t understand why it works for the modification date but not for the author.

I tried the solutions referenced in this post and this post but it doesn’t work.

2

Answers


  1. You need to get the Author ID like in your 2nd linked thread, then from that author ID you will be able to get the WP_User related object and get any user data from the author.

    Try the following:

    function auteur_par_shortcode(){
        $author_id   = get_post_field ('post_author', get_the_ID()); // Get author ID
        $author_user = new WP_User($author_id); // Get Author WP_User object
       
        return sprintf('<p>%s <a href="%s" target="_self" class="wp-block-post-author-name__link">%s</a></p>',
            esc_html__('Par'), $author_user->user_url, $author_user->display_name);
    }
    add_shortcode('auteur_par', 'auteur_par_shortcode');
    
    
    

    It should work now.

    Usage: [auteur_par]

    Login or Signup to reply.
  2. You can use the below shortcode to retrieve and display the author data within the WordPress loop. Add the following code to your theme’s functions.php file:

    
    function post_author_data_shortcode_sp( $atts ) {
        // Get the global post object
        global $post;
    
        // Ensure we are inside the loop and the $post object is available
        if ( ! $post ) {
            return ''; // Exit if not in the loop
        }
        // Get the author's ID
        $author_id = $post->post_author;   
    
        // Get author information
        $author_name = get_the_author_meta( 'display_name', $author_id );
        $author_url = get_author_posts_url( $author_id );
        $author_description = get_the_author_meta( 'description', $author_id );
        $author_avatar = get_avatar( $author_id, 64 ); // Get author avatar (64px size)
    
        // Construct the output HTML
        $output = '<div class="post-author-info">';
        $output .= '<h4>Author: <a href="' . esc_url( $author_url ) . '">' . esc_html( $author_name ) . '</a></h4>';
        $output .= '<p>' . esc_html( $author_description ) . '</p>';
        $output .= '<div class="author-avatar">' . $author_avatar . '</div>';
        $output .= '</div>';
    
        return $output;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search