WordPress.org

Ready to get started?Download WordPress

Forums

PHP help for displaying featured images on pages (4 posts)

  1. maryefern
    Member
    Posted 1 year ago #

    I want to show featured image thumbnails on pages (to appear before the page title and content), but only if there is a featured image defined for that page (otherwise, I don't want any image to appear above the page title).

    With the code below, I get the desired result. However, it kills the featured image thumbnails on my archive pages and on my posts...

    add_action( 'genesis_before_post_title', 'genesis_post_info' );
    add_action('genesis_post_title','generate_post_image', 5);
    function generate_post_image() {
    
    	if ( is_page() || ! genesis_get_option( 'content_archive_thumbnail' ) )
    	return;	
    
    	if ( is_page() && has_post_thumbnail() ) {
    printf( $image );
    	} else { return;
    	}
    
    	if ( $image = genesis_get_image( array( 'format' => 'url', 'size' => genesis_get_option( 'image_size' ) ) ) ) {
    		printf( '<a href="%s" rel="bookmark"><img class="post-image" src="%s" alt="%s" /></a>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
    	}

    If I remove this portion...

    if ( is_page() && has_post_thumbnail() ) {
    printf( $image );
    	} else { return;
    	}

    ...then my archives and posts look perfect, but I am back to where I started with no featured images on the pages.

    I am using the Genesis framework from Studiopress.

    What am I doing wrong? My website is: http://www.balletuni.com

  2. Have you tried asking in the Studiopress forums? It's really hard for volunteers to provide support for commercial products behind a paywall. For one most don't have access to the code and we also want to respect the commercial theme authors business model of offering paid support.

  3. maryefern
    Member
    Posted 1 year ago #

    Hi Chris,

    Thanks for your reply!

    Yes, I tried submitting my problem to the official Studiopress Support but they told me that, since it would require a modification of my theme, they cannot help me. They sent me instead to the community forums at studiopress.com/forums, but a response is not guaranteed and so far I have not had any.

    I understand your difficulty as well... I guess I just feel like someone who understands php might be able to figure out a way for me to get this done. I have almost zero knowledge of php, so that is why I am struggling. Of course, if it really does require more knowledge of Genesis, then I will just keep trying on the Studiopress community forums...

  4. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    So, there's nothing really Genesis Theme specific here. The key is this part of the callback:

    if ( is_page() || ! genesis_get_option( 'content_archive_thumbnail' ) )
    	return;	
    
    if ( is_page() && has_post_thumbnail() ) {
    	printf( $image );
    } else {
    	return;
    }

    Your second conditional is really unnecessary. Try adding the has_post_thumbnail() to the first conditional. For example, the following conditional will *not* print the image if the following are true:
    - The current post does NOT have a post thumbail, OR
    - The current page is an archive index AND the specified option is NOT enabled

    if ( ! has_post_thumbail() || ( is_archive() && ! genesis_get_option( 'content_archive_thumbnail' ) ) ) {
    	return;
    }

    If there are other conditionals you want to account for, you'll have to let us know what they are.

    P.S. if you're not especially familiar with PHP, I strongly recommend using opening/closing braces for all conditional clauses. It will make things much easier for you to follow.

Topic Closed

This topic has been closed to new replies.

About this Topic