• I am trying to understand how to add a class to php so I can then style it with css.

    Here is the code I am working on. I would like to style the post name and the thumb.

    Thanks for any help as usual…

    function get_related_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    
        $output = '<ul>';
        foreach ( $authors_posts as $authors_post ) {
            $output .= get_the_post_thumbnail($authors_post->ID);
            $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
        }
        $output .= '</ul>';
    
        return $output;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    To style thumbnails, see Function_Reference/get_the_post_thumbnail#Styling_Post_Thumbnails.

    You can add a class to the post name by altering the <li> tag to something like <li class="post_name"> or to the <a> tag to something like <a class="post_link" href="' . get_permalink( //...

    Thread Starter Bubbajuju

    (@bubbajuju)

    Thanks bcworz,

    I keep getting syntax errors with everything I try. I will go and check out your link and I will try altering the
    <li> tag.

    Thanks again.

    Thread Starter Bubbajuju

    (@bubbajuju)

    Woot! Thanks for your help, it works.
    This is what I ended up with:

    function get_related_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    
        $output = '<ul>';
        foreach ( $authors_posts as $authors_post ) {
            $output .= get_the_post_thumbnail($authors_post->ID, array('class' => 'relatedauthorthumb'));
            $output .= '<li class="sidebarheadline"><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
        }
        $output .= '</ul>';
    
        return $output;
    }

    Now any ideas on how to add a href to the thumbnails to make them clickable and take you to the post?
    Thanks!!!

    Moderator bcworkz

    (@bcworkz)

    Yes, move the <li> and <a> tags and their contents so they enclose the thumbnail code as well, like this:

    $output .= '<li class="sidebarheadline"><a href="' . get_permalink( $authors_post->ID ) . '">' . get_the_post_thumbnail($authors_post->ID, array('class' => 'relatedauthorthumb'));
    $output .= apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';

    This changes what’s contained in every <li> block so you may need to tweak your CSS to get a reasonable layout.

    Thread Starter Bubbajuju

    (@bubbajuju)

    Awesome bcworkz, I’ve been slammed and haven’t had a chance to implement this yet but I wanted you to know how thankful I am for your help. I will report back asap when I’m able to follow your instructions and pull it off!
    Thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Classes or Id's to Related Author Posts Code’ is closed to new replies.