Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to use post_id >
Tagged: featured image, php, post_id, thumbnail
- This topic has 7 replies, 4 voices, and was last updated 10 years, 11 months ago by
majecdad.
-
AuthorPosts
-
April 18, 2014 at 12:21 am #101103
majecdad
MemberHate 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.
April 20, 2014 at 1:32 am #101370Davinder Singh Kainth
MemberI 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’ ) )
Sunshine PRO genesis theme
Need Genesis help? Davinder @ iGuiding Media | My Blog | Fresh Genesis ThemesApril 20, 2014 at 6:51 pm #101559majecdad
MemberThanks, but no, I'm looking to target ALL ids AFTER a specific id. e.g. post_id > 18800
Any guidance out there?
Thanks.
April 21, 2014 at 2:26 am #101595daymobrew
MemberYou 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’); } }
April 21, 2014 at 5:03 am #101604Gary Jones
Member@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' ); }
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
April 21, 2014 at 10:13 am #101667majecdad
MemberExcellent. 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.
April 21, 2014 at 10:18 am #101668Gary Jones
MemberYes, 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.
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
April 21, 2014 at 10:29 am #101673majecdad
MemberAhhh, 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.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.