WordPress.org

Forums

Problems with has_post_thumbnail (4 posts)

  1. grumo64
    Member
    Posted 4 years ago #

    I want to display my last post by category but only if the post has thumbnail.

    I need something like this :

    $categories = get_categories('orderby=id&child_of=10');
    foreach($categories as $cat) {
      $postslist = get_posts('category=10&numberposts=1&order=date');
      foreach ($postslist as $post) {
        if (has_post_thumbnail($post->ID)){
          ... the last post thumbnail
        }else{
          ... find the next post id with thumbnail
        }
      }
    }

    Maybe the "get_adjacent_post" could help ?
    Thanks for your help

  2. grumo64
    Member
    Posted 4 years ago #

    Please any help could help me ...

  3. ascottmccauley
    Member
    Posted 4 years ago #

    Probably a little too drunk to be posting code right now, but maybe this will hep you out:

    //Previous Post in Category
    function get_prev_cat_post($categoryID) {
        global $wpdb, $post;
    
        // get post date of current post
        $currentPostDate = $post->post_date;
    	$newPostInfo = $wpdb->get_row(
    		"SELECT * FROM $wpdb->posts
    		LEFT JOIN $wpdb->term_relationships ON
    		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    		LEFT JOIN $wpdb->term_taxonomy ON
    		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->posts.post_type = 'post'
    		AND $wpdb->term_taxonomy.taxonomy = 'category'
    		AND $wpdb->term_taxonomy.term_id = $categoryID
    		AND $wpdb->posts.post_date < '$currentPostDate'
    		ORDER BY post_date DESC
    		LIMIT 1");     
    
    	if ($newPostInfo) {
    		return $newPostInfo;
    	}
    }
    
    //Next Post in Category
    function get_next_cat_post($categoryID) {
        global $wpdb, $post;
    
        // get post date of current post
        $currentPostDate = $post->post_date;
    	$newPostInfo = $wpdb->get_row(
    		"SELECT * FROM $wpdb->posts
    		LEFT JOIN $wpdb->term_relationships ON
    		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    		LEFT JOIN $wpdb->term_taxonomy ON
    		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->posts.post_type = 'post'
    		AND $wpdb->term_taxonomy.taxonomy = 'category'
    		AND $wpdb->term_taxonomy.term_id = $categoryID
    		AND $wpdb->posts.post_date > '$currentPostDate'
    		ORDER BY post_date ASC
    		LIMIT 1");
    
    	if ($newPostInfo) {
    		return $newPostInfo;
    	}
    }
    
    //Previous & Next Post Navigation with images.
    function get_navHTML() {
    	$navHTML = '';
    	if(function_exists('get_prev_cat_post') && function_exists('get_next_cat_post')) {
    		$featuredID = get_category_by_slug('featured')->term_id;
    		$slideshowID = get_category_by_slug('slideshow')->term_id;
    		$portfolioID = get_category_by_slug('portfolio')->term_id;
    		$excludedCats = array($featuredID, $slideshowID, $portfolioID);
    		foreach (get_the_category() as $category) {
    			if (!in_array($category->cat_ID,$excludedCats)) {
    				$mainCatID = $category->cat_ID;
    			}
    		}
    		$mainCat = get_cat_name($mainCatID);
    
    		$prev = get_prev_cat_post($mainCatID);
    		$prevID = $prev->ID;
    		$prevTitle = $prev->post_title;
    		$prevURL = get_permalink($prevID);
    		$image = get_post_meta($prevID, 'photoQImageSizes', true);
    		if($image){
    			$prevThumb = $image['tiny']['imgTag'];
    		}elseif(has_post_thumbnail($prevID)) {
    			$prevThumb = get_the_post_thumbnail($prevID,'thumbnail');
    		}
    		$next = get_next_cat_post($mainCatID);
    		$nextID = $next->ID;
    		$nextTitle = $next->post_title;
    		$nextURL = get_permalink($nextID);
    		$image = get_post_meta($nextID, 'photoQImageSizes', true);
    		if($image){
    			$nextThumb = $image['tiny']['imgTag'];
    		}elseif(has_post_thumbnail($nextID)) {
    			$nextThumb = get_the_post_thumbnail($nextID,'thumbnail');
    		}
    		if($prev || $next){
    			$navHTML .= '
    			<nav class="nav-single">
    				<h6><a href="'. get_category_link($mainCatID) .'" title="'. $mainCat .'">More from <em>'. $mainCat . '</em></a></h6>
    				<ul>';
    					if($prev){
    						$navHTML .= '
    						<li class="older">
    							<a href="' . $prevURL . '" title="' . $prevTitle . '">
    								<span class="nav-image">' . $prevThumb . '</span>
    							</a>
    						</li>';
    					}
    					if($next){
    						$navHTML .= '
    						<li class="newer">
    							<a href="' . $nextURL . '" title="' . $nextTitle . '">
    								<span class="nav-image">' . $nextThumb . '</span>
    							</a>
    						</li>';
    					}
    				$navHTML .= '</ul>
    			</nav>';
    		}
    	}
    	return $navHTML;
    }
  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic