• Hello,
    I have this code (found here) that give a list of posts by the author.

    $current_author = get_query_var('author');
    $author_posts=  get_posts( 'author='.$user->id.'&posts_per_page=-1' );
    if($author_posts){
    echo "Mijn dagboek berichten";
    echo '<ul>';
    foreach ($author_posts as $author_post)  {
     echo '<li><a href="'.get_permalink($author_post->ID).'">'.$author_post->post_title.'</a></li>';
    }
    echo '</ul>';
    }

    The code works fine but I need a list of Thumbnails of the posts, (linked to them).
    I tried many solutions but I am not well skilled for that.
    Anyone can help me?
    thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Please follow the documentation, it provides all the information you need.
    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

    Thread Starter gennaro80

    (@gennaro80)

    Hello Navneil,
    thank you very much for your support and for the time you dedicate to me.

    I already had a look to this documentation and this seems to be the code I need:

    $posts = get_posts( array( 'posts_per_page' => 20 ) );
    foreach ( $posts as $_post ) {
        if ( has_post_thumbnail( $_post->ID ) ) {
            echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
            echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
            echo '</a>';
        }
    }

    But it shows last posts by all users. I need only to display posts by a specific author that I identified with Username ($username). This code will be in the Author page.
    Can you help me to modify it?
    thank you very much

    I was not able to understand you properly so I wrote few code that might help you out. Do let me know.

    //Get the post of the user ($user->id)
    $posts = get_posts( array( 'author='.$user->id.'&posts_per_page' => 20) );
    foreach ( $posts as $_post ) {
        if ( has_post_thumbnail( $_post->ID ) ) {
            echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
            echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
            echo '</a>';
        }
    }
    //Get the post of the current logged in User
    $posts = get_posts( array( 'author=' . get_current_user_id() .'&posts_per_page' => 20) );
    foreach ( $posts as $_post ) {
        if ( has_post_thumbnail( $_post->ID ) ) {
            echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
            echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
            echo '</a>';
        }
    }
    Thread Starter gennaro80

    (@gennaro80)

    I am sorry if I was not clear.
    Thank you for your help!
    I will try to explain better:
    I have a certain numbers of authors on my blog and, for each one of them, I have a personal page with all info (name, location description, etc.) I want add also a thumbnail gallery of their posts.
    It means that on the page of author id=n, I want display only the posts by the author “n” as thumbnails.
    The first code you posted display all the posts by different authors, my scope is to display only the posts by the author identified with Username ($username) on his personal profile page.
    I hope now is clear.
    Thanks

    Okay when you said id=n, what does n mean? the username or user id?

    Thread Starter gennaro80

    (@gennaro80)

    username

    I’d like to bump up this topic, since I’m looking exactly the same as @gennaro80

    How to modify that code:

    $current_author = get_query_var('author');
    $author_posts=  get_posts( 'author='.$user->id.'&posts_per_page=-1' );
    if($author_posts){
    echo "Mijn dagboek berichten";
    echo '<ul>';
    foreach ($author_posts as $author_post)  {
     echo '<li><a href="'.get_permalink($author_post->ID).'">'.$author_post->post_title.'</a></li>';
    }
    echo '</ul>';
    }

    …so it displays only the posts of a specific user, it may be pointed e.g. with specific user_id, user_login etc.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘List of Post thumbnails by author’ is closed to new replies.