• Resolved Sam

    (@wamshatley)


    Hi there,

    Is there an obvious way I’m missing to conditionally add a title to the top of the WPSP loop only if posts are found?

    I’m displaying a post loop across all category pages with a dynamic tax_term passed through based on the current page, and sometimes the category doesn’t have any posts. In those cases, I’d prefer to completely hide the output. If there are posts though, I’d like to prefix the section with a title.

    There don’t seem to be any hooks I’m able to use inside the main query/loop to add a title – the nearest I could find is ‘wpsp_inside_wrapper’, but it’s before the query is made and outside the $query->have_posts(), so won’t work for me.

    If not, would there be any chance of a pull request being accepted to move the query up a little higher next to the inside wrapper and a new conditional hook for something like wpsp_conditional_title added if $query->have_posts() is true or something like that?

    Thanks in advance for your help – plugin’s great alongside GP!

    • This topic was modified 3 years, 7 months ago by Sam.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Elvin

    (@ejcabquina)

    Hi there,

    Not exactly sure which specifically do you need the hook.

    If you need a hook inside the loop, you can try wpsp_before_header but this one goes directly inside the <article>.

    Thread Starter Sam

    (@wamshatley)

    Thanks for the reply. I’m looking to add a title to the section of the page before the loop even starts, but show it only where the query returns results.

    That way, if there are no results, nothing at all is displayed, and if there are results then a title like “Latest Related Posts” is displayed before the loop.

    Plugin Support Elvin

    (@ejcabquina)

    Not exactly sure why the hook is not appearing on your end when there’s no result.

    I’ve done a test to confirm if that is the case and I didn’t get the same thing you did.

    See this page – https://dev-generate-press.pantheonsite.io/?page_id=33

    Code used:

    add_action('wpsp_inside_wrapper', function(){ 
    	echo '<h1 style="display:block; width: 100%; margin-left: 2em;">test</h1>'; 
    });

    You can see that there’s no returned return but the hooked element “test” is still showing up.

    And in the actual plugin codes, there’s no condition to stop the execution of hooks when there’s no results – https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/wp-show-posts.php

    Thread Starter Sam

    (@wamshatley)

    The hook’s working as you describe/as designed, but I’m trying to hide the title when there are no results (because it doesn’t fit in the design I’m working on to say “Latest Posts” but then show none, for example), so I’d like to wrap a hook in a conditional, i.e.:

    // Start the query.
    $query = new WP_Query( apply_filters( 'wpsp_query_args', $args, $settings ) );
    
    if ( $query->have_posts() ) {
    	do_action( 'wpsp_inside_wrapper_has_posts', $settings );
    }

    So that the hook only executes if the query returns posts. Alternatively, it’d be probably cleaner if the hook also made the query object available so it can be filtered if implemented, i.e.:

    do_action( 'wpsp_inside_wrapper', $settings, $query );

    Rather than adding in a separate conditional hook, but this would involve moving the query to before the hook executes.

    Failing that, it’d be great if there was actually a setting in the UI for adding a title to the block and conditionally showing it if results.

    Sorry for the explanation again – I just wondered how likely a pull request for adding/changing a hook would be.

    Thanks,
    Sam

    • This reply was modified 3 years, 7 months ago by Sam.
    Plugin Support Elvin

    (@ejcabquina)

    You can do the condition on the actual hook instead.

    Ponder on this.

    add_action('wpsp_inside_wrapper', function(){ 
    	if ( !get_the_ID() ){
    		echo '<h1 style="display:block; width: 100%; margin-left: 2em;">test</h1>'; 
    	}
    });

    The idea is, we check if any post IDs exists. If there isn’t any, then that’s basically the same thing as having no results.

    The if ( !get_the_ID() ) checks of there are NO post IDs found. If there are posts ID found, it does nothing. If there are NO post IDs found, it echoes the string.

    Thread Starter Sam

    (@wamshatley)

    Hey, thanks for the reply.

    I just tried this, but the get_the_ID() or get_queried_object_id() at wpsp_inside_wrapper is at a point where it hasn’t started a WPSP query loop, so it’s checking the ID of the last post in the page’s main loop, which isn’t quite what I’m looking for, as I need to check for no results in the WPSP query.

    I guess this would work if

    $query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );

    was called above the hook, right? But again I think it comes down to reordering the position of the query in the plugin.

    Thanks for your help with this – maybe I’m overcomplicating this, just to get a title showing if there are results!

    Plugin Support Elvin

    (@ejcabquina)

    Ah right, in that case, the next best thing would be to add a new hook (ex: do_action( 'wpsp_before_loop', $settings, $query ); as you’ve suggested ) after this line https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/wp-show-posts.php#L383

    Just make sure you remember to re-add it back every time the plugin updates as it’ll likely get wiped.

    I’m logging it as a feature request. It’s up to Tom to consider it. ??

    Thread Starter Sam

    (@wamshatley)

    Thanks, I figured it might be coming to that – a relatively harmless & simple feature, so hopefully it might get considered!

    Plugin Support Elvin

    (@ejcabquina)

    I’ve logged it on the official github page. ??
    https://github.com/tomusborne/wp-show-posts/issues/40

    For now, we’ll have to settle with the workaround.

    No problem. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conditionally adding a title above the post loop if $query->has_posts()’ is closed to new replies.