WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add class to post_class (2 posts)

  1. duncmorley
    Member
    Posted 3 years ago #

    I am looking to write a function that detects if a feature image has been added to a post, and to add a class if it has. I have written this so far.

    // Add a class to post_class if feature image is detected
    function check_image( $class ) {
    	if ( has_post_thumbnail() ) {
    		$class[] = 'feature-image';
    	}
    	return $class;
    	}
    add_filter('post_class', 'check_image');
    // end ---

    This actually works but I am getting an error "Missing argument 1 for check_image()". Any ideas?? I am quite new to writing WordPress functions so go easy :-)

  2. duncmorley
    Member
    Posted 3 years ago #

    Fixed it. The code that works is below:

    // Add a class to post_class if feature image is detected
    function check_image( $class = '' ) {
    	if ( has_post_thumbnail() ) {
    		$class[] = 'feature-image';
    	}
    	return $class;
    	}
    add_filter('post_class', 'check_image');
    // end ---

Topic Closed

This topic has been closed to new replies.

About this Topic