Site logo

Archived topic

Replicating WP Show Posts Image dimensions

5 replies · Started by Nathan on March 16, 2022

Viewing posts 1–6 of 6

On my site I use both WP Show Posts together with GeneratePress and am wondering what the best approach for the below is.

I am trying to make the category, tags, and search results display posts with a similar look to WP Show Posts.

I am currently using the below snippet to override the default category archive template with a static page. The static page displays a WP Show Posts shortcode. This is proving to be tiresome with each additional category and doesn't impact the tags or search pages. This was also setup from a prior version of GP and so seems a little dated.

add_filter('request', function( array $query_vars ) {
    if ( is_admin() ) {
        return $query_vars;
    }

    if ( isset( $query_vars['category_name'] ) ) {
        $pagename = $query_vars['category_name'];

        $query_vars = array( 'pagename' => "$pagename" );
    }

    return $query_vars;
} );

So I am looking for an alternative to this.

With the latest updates to GP I can use a Element Block to create the Content Template. However I am running into the problem of when images have been uploaded at different aspect ratio's they don't adjust like they do with WP Show Posts.

For my WP Show Posts list I have the dimensions for images set at 800px x 450px which is a 16:9 aspect ratio. The images seem to be cropped/scaled to meet the 16:9 aspect ratio on WP Show Posts and show images of an intrinsic value of the 800px x 450px giving all images the same height and width across the columns. I am wondering how to achieve the same within the Elements (or Customizer if that's easier). Currently (as can be seen on the tag archive), images have the same width in the columns, but different heights due to differing aspect ratio for the intrinsic image.

I've added links to pages in the Private Information.

I did try this approach : https://wpshowposts.com/support/topic/use-wp-show-posts-to-replace-blog-archive-page/

However I couldn't get the tags working in the code below and wouldn't know how to get it working on the search page, the category worked though :

if(is_archive() || is_home() ){
	add_filter( 'generate_has_default_loop', '__return_false' );
}

add_action( 'generate_before_main_content', function(){
	if( is_tag() ){
		$tag = get_queried_object();
		$tag_slug = $tag->slug;
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 2933, 'tax_term="' . $tag_slug . '"' 
		);
	}
	if( is_category() ){
		$cat = get_category( get_query_var( 'cat' ) );
		$cat_slug = $cat->slug;
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 2938, 'tax_term="' . $cat_slug . '"' 
		);
	}
});

Any help on this would be greatly appreciated.

Hi Nathan,

With regards to the Content Template, we can try some custom CSS to make them behave as it would in WPSP.

If we refer to the third link you provided where images aren’t the same height, a CSS like this could fix it:

img.attachment-image_656x369.size-image_656x369.wp-post-image {
    height: 150px;
    width: 100%;
    object-fit: cover;
}

Here is an article with regards to adding CSS: https://docs.generatepress.com/article/adding-css/#additional-css

Adding it through additional CSS should work.

With regards to your PHP code, here is a PHP you may replace it with:

if(is_archive() || is_home() ){
	add_filter( 'generate_has_default_loop', '__return_false' );
}

add_action( 'generate_before_main_content', function(){
	if( is_tag() ) {
		$tag = get_queried_object();
		$tag_slug = $tag->slug;
		$args = array(
            'taxonomy' => 'post_tag',
            'tax_term' => $tag_slug
            );
   		 
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 1362, $args);
	}
	if( is_category() ){
		$cat = get_category( get_query_var( 'cat' ) );
		$cat_slug = $cat->slug;
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 1362, 'tax_term="' . $cat_slug . '"' 
		);
	}
});

add_filter( 'generate_has_default_loop', function( $has_loop ) {
    if ( is_category() || is_tag() ) {
        return false;
    }

    return $has_loop;
} ); 

You’ll need to set your WPSP as such where the Taxonomy is left blank: https://share.getcloudapp.com/6quZLmLJ

Moreover, please replace 1362 with the WPSP ID of your list. :)

Kindly let us know how it goes. Hope this helps! :)

Hi Fernando

Thanks for your reply.

RE the first code (the CSS). If CSS will be the best solution I've opted to use this instead, unless you can think of any reason I should use height instead of aspect ratio

   aspect-ratio: 16 / 9;
    object-fit: cover;
    width: 100%;

Fortunately though the second code provided works perfect and has added WP Show posts on both the Category and Tags archive, which means the above CSS problem only really matters on the search results. Which is probably used less than the archive page.

I see.

If you wish to display your WPSP list in the search page as well, you can change you’re code to:

add_action( 'generate_before_main_content', function(){
	if ( is_search() ) {
        wpsp_display( 1362 );
    }
	if( is_tag() ) {
		$tag = get_queried_object();
		$tag_slug = $tag->slug;
		$args = array(
            'taxonomy' => 'post_tag',
            'tax_term' => $tag_slug
            );
   		 
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 1362, $args);
	}
	if( is_category() ){
		$cat = get_category( get_query_var( 'cat' ) );
		$cat_slug = $cat->slug;
		if ( function_exists( 'wpsp_display' ) ) 
		   wpsp_display( 1362, 'tax_term="' . $cat_slug . '"' 
		);
	}
});

add_filter( 'generate_has_default_loop', function( $has_loop ) {
    if ( is_category() || is_tag() || is_search() ) {
        return false;
    }

    return $has_loop;
} ); 

This code might not be necessary depending on how your using it:

if(is_archive() || is_home() ){
	add_filter( 'generate_has_default_loop', '__return_false' );
}

Kindly let us know how it goes. :)

Hi Fernando

Thanks for the extra snippet, I settled on just going the CSS route for now but will likely use the snippet in future builds (until WP Show Posts is merged with GB anyway).

All good! You’re welcome Nathan! Feel free to reach out anytime if assistance with anything else is needed. :)

This archived topic is closed to new replies.