• Resolved Jason Lefkowitz

    (@jalefkowit)


    Is there a way for a theme to call into the CFIX plugin to get the featured image for a specific post, either by passing in a post ID number or a complete WP_Post object?

    The existing cfix_featured_image() and cfix_featured_image_url() functions don’t work for my needs, since they both assume they’re operating within a loop and take the context of what post they’re working with from the inside-the-loop environment, and I need to use them outside of a loop. I had thought there might be a way to pass a post ID or WP_Post object to them in the arguments array, but while you can specify a particular category in this fashion, you can’t specify a particular post. And since your plugin already has a bunch of logic built into it to figure out which category is the right one to pull an image from for a particular post, I’d rather just re-use that logic than re-implement it all myself just to get a category ID to pass to CFIX.

    If it’s impossible to have the cfix_featured_image* functions support passing in a post or post ID, an alternative would be to have a function that could accept those arguments and would return the ID of the category CFIX has determined to be the one whose image should be used for that post. It would then be possible to take that ID and pass it into the appropriate cfix_featured_image* function to get the actual image. Doing it all in one function call instead of two would be best, of course, but two would be fine if extending the cfix_featured_image* functions in this fashion would endanger compatibility.

    Thanks in advance for any help you can provide!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author CK MacLeod

    (@ck-macleod)

    Hey – that seems like a reasonable change. Unfortunately, I’m way behind in re-establishing my connections to the WordPress Repo after a recent computer unpleasantness. However, I’ve tested a change that I think might work for you.

    Forgive me if this is a little hacky, but I can’t guarantee that I’ll be able to test fully and push this out as soon as today, and you seem to be a sophisticated user – so here’s a long snipped you can paste into your current file that I’ll try to push out when I can. This change simply adds to the two functions you mention (and to the shortcodes based on them) an option to give the post ID# as ‘postid’ in the $args array.

    Instructions follow in case it’s not already obvious how to use it. If you’re doing this in a production site, you might, of course, want to save a copy of the working file for easy restoration just in case.

         /**  2018-02-23 HACK: ADD POST ID TO ARGUMENTS ARRAY **
         * GET THE FEATURED IMAGE URL
         * ***********WILL NEED TO CHECK LOGIC/UPDATING******************
         * @param array $args
         * @return string
         */
        static function get_featured_image_url( $args ) {
    		
    	global $post;
    
            $images = get_option( 'cks_cfix_featured_images' ) ;
            $use_yoast_cat = is_plugin_active('wordpress-seo/wp-seo.php') ? 
                get_option( 'cks_cfix_use_yoast_primary') : FALSE ;  
                                
            $postid = isset( $args['postid'] ) ? $args['postid'] : $post->ID ;
            $size = isset( $args['size'] ) ? $args['size'] : 'full' ;
            
            if ( isset( $args['cat_id'] ) ) {
                
                $cat_id = intval( $args['cat_id'] ) ;
    
                if ( isset( $images[$cat_id] ) ) {
    
                    $attachment = wp_get_attachment_image_src( 
                            $images[$cat_id], $size 
                            ) ;
    
                    if ( $attachment !== FALSE ) { 
                        
                        return $attachment[0];
                        
                    }
    
                }
                    
            } else if ( is_single() ) {
                    
                $id = get_post_thumbnail_id( $postid ) ;
                
                if ( $id ) {
                    
                    $attachment = wp_get_attachment_image_src( $id, $size ) ;
                    
                    if ( $attachment !== FALSE ) {
                        
                        return $attachment[0];
                        
                    }
                
                }
                
            } else if ( is_category() ) {
                
                $categories = get_the_category() ;
                
                if ( $categories ) {
                    
                    $cat = NULL;
                    
                    /* YOAST HERE */
                    
                    if ( $use_yoast_cat ) {
                
                        $yoast_cat = get_post_meta( 
                            $object_id, '_yoast_wpseo_primary_category', true 
                            ) ? get_post_meta( 
                            $object_id, '_yoast_wpseo_primary_category', true 
                            ) : '' ;
                
                        if ( $yoast_cat && 
                                in_array( $yoast_cat, $images ) &&
                                ! in_array( $yoast_cat, $excl_cat_ids )                
                        ) {   
                            
                            $attachment = wp_get_attachment_image_src( 
                                    $images[$yoast_cat], $size 
                                    ) ;
                            
                            if ( $attachment !== FALSE ) {
                                
                                return $attachment[0];
                                
                            }
                
                    
                        }
    
                    }
                    
                    foreach( $categories as $category ) {
    
                        if ( isset( $images[$category->term_id] ) ) {
    
                            $attachment = wp_get_attachment_image_src( 
                                    $images[$category->term_id], $size 
                                    ) ;
                            
                            if ( $attachment !== FALSE ) {
                                
                                return $attachment[0];
                                
                            }
    
                        }
                        
                        if ( $cat === NULL ) $cat = $category;
                        
                    }
                        
                    if ( $cat !== NULL ) {
                                    
                        $parent = intval( $cat->parent ) ;
                        
                        if ( $parent > 0 && isset( $images[$parent] ) ) {
                                            
                            $attachment = wp_get_attachment_image_src( 
                                    $images[$parent], $size 
                                    ) ;
                            
                            if ( $attachment !== FALSE ) {
                                
                                return $attachment[0];
                                
                            }
                            
                        }
                        
                    }
                    
                }
                
            }
    
            return '' ;
    
        }
    
        /**
         * SHOW THE FEATURED IMAGE
         * @param array $args
         * @return string
         */
        static function show_featured_image( $args ) {
            
    	global $post;
    		
            $images = get_option( 'cks_cfix_featured_images' ) ;
            $use_yoast_cat = is_plugin_active('wordpress-seo/wp-seo.php') ? 
                get_option( 'cks_cfix_use_yoast_primary') : FALSE ;
    		
    	$postid = isset( $args['postid'] ) ? $args['postid'] : $post->ID ;
            
            if ( isset( $args['size'] ) ) {
                
                $size = $args['size'];
                unset( $args['size'] ) ;
                
            } else {
                
                $size = 'thumbnail' ;
                
            }
            
            if ( isset( $args['cat_id'] ) ) {
                
                $cat_id = intval( $args['cat_id'] ) ;
                
                if ( isset( $images[$cat_id] ) ) {
                    
                    $img = wp_get_attachment_image( $images[$cat_id], $size ) ;
                    
                    if ( $img ) {
                        
                        return '<span class="cfix-featured-image">' . 
                            wp_get_attachment_image( $images[$cat_id], $size ) . 
                            '</span>' ;
                        
                    }
                }
            }
            
            else if ( is_single() ) {
                
                $image = get_the_post_thumbnail( $postid , $size, $args ) ;
                
                if (  $image ) return 
                    '<span class="cfix-featured-image">' . $image . '</span>' ;
                
            }
            
            else if ( is_category() ) {
                
                $categories = get_the_category() ;
                
                if ( $categories ) {
                    
                    $cat = NULL;
                    
                    if ( $use_yoast_cat ) {
                
                        $yoast_cat = get_post_meta( 
                            $object_id, '_yoast_wpseo_primary_category', true 
                            ) ? get_post_meta( 
                            $object_id, '_yoast_wpseo_primary_category', true 
                            ) : '' ;
                
                        if ( $yoast_cat && 
                                in_array( $yoast_cat, $images ) &&
                                ! in_array( $yoast_cat, $excl_cat_ids )                
                        ) {   
                            
                            $attachment = wp_get_attachment_image_src( 
                                    $images[$yoast_cat], $size 
                                    ) ;
                            
                            if ( $attachment !== FALSE ) {
                                
                                return '<span class="cfix-featured-image">' . 
                                wp_get_attachment_image( 
                                    $images[$yoast_cat], $size 
                                    ) . '</span>' ;}
                                
                        }
                
                    
                    }
                    
                    foreach( $categories as $category ) {
                        
                        if ( isset( $images[$category->term_id] ) ) {
                            
                            return '<span class="cfix-featured-image">' . 
                                wp_get_attachment_image( 
                                    $images[$category->term_id], $size 
                                    ) . '</span>' ;}
                        
                        if ( $cat === NULL ) {
                            
                            $cat = $category;
                            
                        }
                        
                    }
                    
                    if ( $cat !== NULL ) {
                    
                        $parent = intval( $cat->parent ) ;
                        
                        if ( $parent > 0 && isset( $images[$parent] ) ) {
                            
                            return '<span class="cfix-featured-image">' . 
                               wp_get_attachment_image( $images[$parent], $size ) . 
                                    '</span>' ;
                            
                        }
                    
                    }
                    
                }
                
            }
            
            return '' ;
            
       }

    If you go to your plugin editor, and find category-featured-images-extended.php, and copy the following two functions in place of cfix_featured_image() and cfix_featured_image_url(), it should work for you. Please let me know if it does.

    • This reply was modified 6 years, 9 months ago by CK MacLeod.
    • This reply was modified 6 years, 9 months ago by CK MacLeod.
    Plugin Author CK MacLeod

    (@ck-macleod)

    Oops… Sorry – just realized that the above will work only in “single post” context.. hold on. (This is why one doesn’t rush these things!)

    • This reply was modified 6 years, 9 months ago by CK MacLeod.
    Plugin Author CK MacLeod

    (@ck-macleod)

    You can try download and install the version at the following link, and maybe let me know if it works smoothly for you.

    https://ckmacleod.com/wordpress-plugins/category-featured-images-extended/download-changelog-installation/

    In short, if the post’s ID number is 123, you’d add the argument 'post_id' => 123 to an $args array or post_id="123" to the shortcode’s arguments.

    If I don’t hear back from you within a couple of days, I may just push this out into the WordPress repository, but your feedback will be welcome!

    Thread Starter Jason Lefkowitz

    (@jalefkowit)

    Testing the revised plugin in my development environment now.

    Am I right in looking at it that the new code path just returns the featured image for the specified post, if one exists? As opposed to the featured image set via the plugin for whatever category that post belongs to?

    Plugin Author CK MacLeod

    (@ck-macleod)

    No – if there’s no featured image set for the identified post, then it should return the category image (if there is one).

    • This reply was modified 6 years, 9 months ago by CK MacLeod.
    Plugin Author CK MacLeod

    (@ck-macleod)

    Any further results? I’ll test it again anyway, but I thought I had it.

    Thread Starter Jason Lefkowitz

    (@jalefkowit)

    Sorry for the delayed response, got pulled away by something else…

    I think you’re right that you’ve got it — what I was running into was I think due to an issue with the underlying database, posts with _thumbnail_id records in wp_postmeta that didn’t point back to an actual attachment in the media library. So the posts would appear to not have a Post Thumbnail set when viewed in the post editing UI, but would not make most of the “get the post thumbnail for this post, if such exists” functions in the WP API return false like they should if no post thumbnail had been set.

    I’m not sure how exactly those posts got their metadata messed up like that, but that’s a problem for me to worry about, not you ?? When I deleted the individual _thumbnail_id meta records for those posts, they started returning the category image set via CFIX like they should. So I think your new code is solid.

    Plugin Author CK MacLeod

    (@ck-macleod)

    No problem – thanks for testing!

    The specific problem you had is a variation on one that I encountered during development. I addressed it on the “Advanced” page in the plugin home pages (scroll down): https://ckmacleod.com/wordpress-plugins/category-featured-images-extended/advanced/ I speculated about the likely causes at the top of the section – “When Images Don’t Show Up, Or: Curing Bad Thumbnail Data” – and provide some programmatic fixes.

    I’d originally thought about producing a tool or option in the main interface, but no one ever mentioned it until you, so kind of fell by the development wayside. I also tried a programmatic fix within the plug-in code, but it caused problems when pushed out, and… same story.

    I spent some time in the last couple of days working on the successor plug-in, partly to incorporate your suggested addition. It adds several additional functions/enhancements that you might find interesting. I may put out a beta version if you want to volunteer again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Getting category featured image for a specific post?’ is closed to new replies.