How to use post_id >

Community Forums Forums Design Tips and Tricks How to use post_id >

This topic is: not resolved

This topic contains 7 replies, has 4 voices, and was last updated by  majecdad 6 months ago.

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

    majecdad
    Participant
    Post count: 8

    Hate to drop in just for help… but I am. :)

    I added some code to put the Featured image in the Post content, like this:

    /* Code to Display Featured Image on top of the post */
    add_action( ‘genesis_before_entry_content’, ‘featured_post_image’, 8 );
    function featured_post_image() {
    if ( ! is_singular( ‘post’ ) ) return;
    the_post_thumbnail(‘post-image’);
    }

    But I want to ONLY do this for posts GOING FORWARD. I have 1600 old posts where an image was added manually. If I place this globally, I’ll have two images on those 1600 posts. No bueno.

    Can’t I do something to ONLY have a post with “post_ID > 18800″ use this code?

    Thank you much in advance for any help. Have a great day.

    #101370

    Davinder Singh Kainth
    Participant
    Post count: 1291

    I understand, you want to target specific post ID

    Use following for post and pages (where 315 is post / page id)

    if (is_post(array('317')))
    if (is_page(array('317')))

    in place of

    if ( ! is_singular( ‘post’ ) )


    Need website customization services or other Genesis / WordPress help ?
    Davinder @ Design Studio iGuiding Media | My Blog | Fresh Genesis Themes Collection

    #101559

    majecdad
    Participant
    Post count: 8

    Thanks, but no, I’m looking to target ALL ids AFTER a specific id. e.g. post_id > 18800

    Any guidance out there?

    Thanks.

    #101595

    daymobrew
    Member
    Post count: 10

    You could try using the get_the_ID() function.

    /* Code to Display Featured Image on top of the post */
    add_action( ‘genesis_before_entry_content’, ‘featured_post_image’, 8 );
    function featured_post_image() {
      if ( ! is_singular( ‘post’ ) ) return;
      if (18800 > get_the_ID()) {
        the_post_thumbnail(‘post-image’);
      }
    }
    #101604

    Gary Jones
    Moderator
    Post count: 693

    @daymobrew – almost, but your logic is inverted.

    add_action( 'genesis_before_entry_content', 'featured_post_image', 8 );
    /**
     * Code to Display Featured Image on top of the post, since specific post ID.
     *
     * @return null Return early if not a singular post, or post ID is too low.
     */
    function featured_post_image() {
    	if ( ! is_singular( 'post' ) || get_the_ID() <= 18800 ) {
    		return;
    	}
    
    	the_post_thumbnail( 'post-image' );
    }

    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ

    #101667

    majecdad
    Participant
    Post count: 8

    Excellent. The || get_the_id was where I couldn’t figure structure.

    Only question remaining is if I want the code to only apply where the post ID is GREATER than 18800, should the code be:

    get_the_id() >= 18800 (instead of <=)?

    Thank you.

    #101668

    Gary Jones
    Moderator
    Post count: 693

    Yes, if the rest of the logic was like that, but it’s not.

    It’s usually better practice to return early for any unfavourable conditions, so that what’s left only applies to favourable conditions.

    So my code basically says “If it’s not a single post, or the ID is less than or equal to 18800, then return and do nothing. For anything else (that is, posts with ID greater than 18800), go ahead and do something (here, show a post thumbnail).”

    The benefit is that you don’t end up nesting and indenting code for each condition or wrapping the whole of the contents of the function in a giant condition.

    Return early when you can for cleaner code.


    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ

    #101673

    majecdad
    Participant
    Post count: 8

    Ahhh, I see. I love that I learn something every day. Being self-taught, and doing ‘cut-and-paste’ code, even after several years, there is still so much to learn. :)

    Thank you all who replied, especially Gary, for not just the code, but as importantly the explanation of why it does what it does. I really appreciate it.

    Have a great day.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.