WordPress.org

Ready to get started?Download WordPress

Forums

Show Author Related Posts if they have post (6 posts)

  1. raybeam
    Member
    Posted 1 year ago #

    Hi,

    I'm trying to create a function that will show related author posts only if the author has the posts. It works fine for users who have more than one post, but when users have only one post, it displays some default css styling that looks awkward without the accompanying thumbnails that link to the related posts.

    This is what I did in my functions.php:

    /********************************************************************/
    /* Function for displaying related post thumbnails */
    /*********************************************************************/
    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' => 7 ) );
    
        $output = '<div class="tiles-block"><h4 class="other-props" >Other properties by this developer</h4>';
    	foreach ( $authors_posts as $authors_post ) {
    	  $output .= '<a href="' . get_permalink( $authors_post->ID ) . '">' . get_the_post_thumbnail($authors_post->ID) . '</a>';
    	}
    	$output .= '</div>';
    
        return $output;
    }
    
    and this is how I display them through my single.php:
    <div class="row">

    <div class="col-md-12">

    <?php echo get_related_author_posts(); ?>

    </div> <!-- col-md-12 -->

    </div> <!-- end row -->

    Obviously I need some kind of if statement, just not sure how....

    Any help please? :)

  2. PaulMudo
    Blocked
    Posted 1 year ago #

    This is a CSS issue. Need an example URL to see the problem.
    Just review using Firebug or so, and find out the differences of HTML/CSS.

  3. raybeam
    Member
    Posted 1 year ago #

    My friend, I think I didn't explain this clearly or something. It's not the css. I don't want the block that the posts are wrapped in to show if the author has no posts.

    I believe this is about if statements. example : if author_has_posts : display the posts...

    problem is, I don't if WordPress has a "author_has_posts" statement..

    Thanks for the reply!

  4. PaulMudo
    Blocked
    Posted 1 year ago #

    You can get author post number using this:

    $author_post_number = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->posts." WHERE post_author = ". intval($authordata->ID) );
    
    if( $author_post_number == 1){
        your code here....
    }

    Don't forget to add this $wpdb; in globals:

    function get_related_author_posts() {
        global $authordata, $post, $wpdb;
    ....
  5. raybeam
    Member
    Posted 1 year ago #

    Thanks man,

    I'm not exactly a programmer, so I don't full understand where I would put that in relation to my code. I usually just utilise php functions from other people to do the tasks I want.

    So would it look like this?:

    [ Moderator note: please wrap code in backticks or use the code button. ]

    /********************************************************************/
    /* Function for displaying related post thumbnails */
    /*********************************************************************/
    function get_related_author_posts() {
        global $authordata, $post, $wpdb;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 7 ) );
    
    $author_post_number = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->posts." WHERE post_author = ". intval($authordata->ID) );
    
    if( $author_post_number == 1){
    
        $output = '<div class="tiles-block"><h4 class="other-props" >Other properties by this developer</h4>';
    	foreach ( $authors_posts as $authors_post ) {
    	  $output .= '<a>ID ) . '">' . get_the_post_thumbnail($authors_post->ID) . '</a>';
    	}
    	$output .= '</div>';
    
        return $output;
    }
    }
  6. PaulMudo
    Blocked
    Posted 1 year ago #

    This code will output User post thumbnails but only for user who has only one post. What exactly you need, let me know and I can say what to add in if{ } else { } statement?

    /********************************************************************/
    /* Function for displaying related post thumbnails */
    /*********************************************************************/
    function get_related_author_posts() {
        global $authordata, $post, $wpdb;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 7 ) );
    	$author_post_number = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->posts." WHERE post_author = ". intval($authordata->ID) );
    
    	if( $author_post_number == 1){
    		$output = '<div class="tiles-block"><h4 class="other-props" >Other properties by this developer</h4>';
    		foreach ( $authors_posts as $authors_post ) {
    		  $output .= '<a href="' . get_permalink( $authors_post->ID ) . '">' . get_the_post_thumbnail($authors_post->ID) . '</a>';
    		}
    		$output .= '</div>';
    		return $output;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic