[Support request] Getting a custom element to show / not show in single-post view

Home Forums Support [Support request] Getting a custom element to show / not show in single-post view

Home Forums Support Getting a custom element to show / not show in single-post view

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #764437
    Greg

    Hi there — thanks to Tom’s *excellent* prior advice, I have a custom element that gets me the look and feel I want for single posts. Thank you!!

    My problem: I want to also use asides for a separate type of post (quick blurbs — no headline or author meta). The header I’ve created won’t work with those since they have, you know, neither headline nor author. πŸ™‚ All of the hooks and display rules will get me down to showing the header only on single posts, but no further. Is the right path forward to:

    * Create a new hook by post type?

    * Cerhaps a custom PHP snippet that would serve as a filter and only display the custom element on standard posts?

    * Set up the whole custom element as an if/then test in PHP so it checks the post type and only displays if it’s a standard post?

    I don’t claim to be smart enough to code any of those solutions… just looking for a direction at this point.

    * * *

    COMPLETELY UNRELATED BUT AS LONG AS I’M HERE: I’m using the PHP Snippets plugin to get around the lack of a working shortcode for displaying a post excerpt. My shortcode throws <?php the_excerpt(); ?> in between two H2 tags I have in the custom element. HOWEVER, the generated code output is:

    <H2><p>Post excerpt copy formatted like a paragraph</p></h2>

    #764482
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Are these separate posts a custom post type? If so, you should be able to create a separate Header Element and apply it to that post type only.

    That way your original Header will apply to posts, and your new Header will apply to posts inside your custom post type.

    What’s the desired output of your shortcode?

    #764490
    Greg

    Hi Tom!

    * Separate posts are all of the “aside” post type and consist of 1-2 paragraphs of body copy. No headline or meta/byline (both elements in my standard post header) would be displayed.

    * Desired output of the PHP snippet is <h2>Excerpt content goes here</h2> and I’m getting <h2><p>Excerpt content goes here</p></h2>. The entirety of the PHP snippet is the_excerpt(); (the plugin requests that users not apply PHP opening/closing tags) and I think what’s happening is the get_the-excerpt filters are applying auto-p formatting before the_excerpt can display the content. If that’s the case, it’s a WP thing and I’m not sure there’s any way around it.

    Greg

    #764831
    Tom
    Lead Developer
    Lead Developer

    1. Ah, so you want to disable your existing Header Element on all aside post formats?

    2. Hmm, I didn’t think the_excerpt() added any paragraph tags.

    Try this instead:

    <?php
    global $post;
    
    echo '<h2>' . $post->post_excerpt . '</h2>';
    ?>
    #764881
    Greg

    1. Yes! πŸ™‚

    2. No love from that solution — the excerpt doesn’t show at all. I’m willing to abandon this search for a solution and just make it work with the <p> tags in place for now — I believe you said in another thread you were working on a shortcode to handle excerpts in a future release.

    (Again, not to sound too fanboyish, but the level of support here is *astounding.*)

    #764946
    Tom
    Lead Developer
    Lead Developer

    1. Try this function:

    add_filter( 'generate_header_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id && has_post_format( 'aside' ) ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    The only thing you’d need to change is the 10, which should be the ID of your element.

    Let me know πŸ™‚

    #765000
    Greg

    Alas, no luck. Asides spit up:

    Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\bc3\wp-includes\post-template.php on line 284

    Current code being used in the PHP snippet:

    the_excerpt();
    
    add_filter( 'generate_header_element_display', function( $display, $element_id ) {
        if ( 91 === $element_id && has_post_format( 'aside' ) ) {
            $display = false;
        }
    
        return $display;
    }, 91, 2 );

    Tom, at this point — through no fault of your own, because you’ve been amazing — I am discouraged and probably need to farm this out to a pro. Can you recommend someone experienced in GeneratePress + Elementor?

    #765448
    Tom
    Lead Developer
    Lead Developer

    Strange, does that notice still show up if you remove the_excerpt();?

    If you want to hire someone, codeable.io might be worth checking out πŸ™‚

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