Site logo

Archived topic

Customize post output based on meta field

6 replies · Started by Arthur on November 30, 2020

Viewing posts 1–7 of 7

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?

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;
}

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 :)

Perfect thank you Tom!

You're welcome :)

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

Sorry never mind, found the option in Customizer.

This archived topic is closed to new replies.