[Resolved] Customize post output based on meta field

Home Forums Support [Resolved] Customize post output based on meta field

Home Forums Support Customize post output based on meta field

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1558956
    Arthur

    I am loving the minimalist approach of GP!

    I am trying to set a custom thumbnail size and hide the excerpt for posts, depending on a custom meta field (i.e. ‘meta-box-dropdown’ = ‘Tier 1’ or ‘meta-box-dropdown’ = ‘Tier 2’). I am trying to do so within functions.php using the code below. This however does not filter the posts successfully.

    add_filter( 'generate_blog_image_attributes', function( $atts ) {
        if ( get_post_meta($postid, 'meta-box-dropdown', true) )  {
            $atts[ 'width' ] = 1900;
            $atts[ 'height' ] = 300;
            $atts[ 'crop' ] = false;
        }
    
        // Return our options
        return $atts;
    } );
    

    Any suggestions on how to approach?

    #1559242
    Arthur

    As a side note, I have three different loops on my front page. I would like the output of each loop to be different from the main loop. I was able to set up different loops but unsure how to manage the thumbnail sizes and fields within each loop.

    
    /* CUSTOM LOOP FOR HERO POSTS - TIER 1*/
    
    $args = array(
        'meta_key' => 'meta-box-dropdown',
        'posts_per_page' => 1,
        'meta_value' => 'Tier 1',
    );
    
    $tier1_query = new WP_Query($args);
    
    if ( generate_has_default_loop() ) {
    
    	if ( have_posts() ) :
    
    		while ( $tier1_query->have_posts() ) :
    
    			$tier1_query->the_post();
    
    			get_template_part( 'content','tier1');
    
    			//print get_post_meta( $post->ID, 'meta-box-dropdown', true );
    			 
    		endwhile;
    
    		/**
    		 * generate_after_loop hook.
    		 *
    		 * @since 2.3
    		 */
    		do_action( 'generate_after_loop', 'index' );
    
    	else :
    
    		generate_do_template_part( 'none' );
    
    	endif;
    }
    #1561156
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    1. It’s better to define a WordPress-created image size. Try this:

    add_filter( 'generate_page_header_default_size', function( $size ) {
        if ( get_post_meta( get_the_ID(), 'meta-box-dropdown', true) ) {
            return 'my-custom-size';
        }
    
        return $size;
    } );

    More info here: https://docs.generatepress.com/article/generate_page_header_default_size/

    2. Using a method similar to the above should work as long as the meta keys/values are different depending on the loop.

    Let me know ๐Ÿ™‚

    #1566245
    Arthur

    Perfect thank you Tom!

    #1566493
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

    #1570959
    Arthur

    How would I make this image size apply to the post only on my homepage (index) page and not the post page?

    #1570972
    Arthur

    Sorry never mind, found the option in Customizer.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.