- This topic is empty.
-
AuthorPosts
-
March 25, 2017 at 8:15 am #253153KonspaulParticipant
This site https://jaanavuola.com/portfolio/ is very dependent of a featured image set on posts and pages. The featured image is set in function.php file with this code
// Add featured image on single post add_action( 'genesis_before_content', 'jaana_featured_image', 1 ); function jaana_featured_image() { $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
')"><?php }Everything is fine as long as the post/page HAS a featured image. But if it doesn’t then the page looks like this https://jaanavuola.com/oma-tili/ It takes the big space for the featured image and leaves it as an empty gray space.
I’m asking your help to make a PHP if statement something like this:
if
/* The div which is showing a featured image */ .featured-images-in-posts
is
/* CSS generated code when the PHP is not finding a featured image */ background-image: url((unknown));
make the
.featured-images-in-posts { display: none }
Is this possible?
March 25, 2017 at 10:20 am #253158AtelierbramParticipantWould it be possible for you to edit the
single.php
file, or the one with the.featured-images-in-posts
div, and wrap that div within a conditional tag (checking if the thumbnail is set), something like:<?php if(has_post_thumbnail()): ?><?php endif; ?>
Where the featured-image-in-posts div will be before the endif (can’t format this over here, sorry)
March 26, 2017 at 4:03 am #253173KonspaulParticipantThanks for your help.
This was actually solved simply by adding an if statement to the php like so:
<?php // Add featured image on single post add_action( 'genesis_before_content', 'jaana_featured_image', 1 ); function jaana_featured_image() { $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); if ($thumb): ?>
')"><?php else: ?>March 26, 2017 at 9:48 am #253178AtelierbramParticipantUsing frameworks for WordPress makes these things a bit harder, for me in my mind that is, because it adds this extra layer of abstraction. So ISO editing a template file, one is filtering functions in
functions.php
, … a matter of preference?First checking if something is “set” with a conditional statement before applying some code seems to be the sensible way to prevent unnecessary output.
March 29, 2017 at 10:39 am #253244KonspaulParticipantYes. WordPress adds extra “layer”. For me this was very hard in the beginning when I just started developing WordPress themes after regular HTML/CSS pages where things are very straight forward.
I prefer keep all my small edits in function.php file. Makes things more easy than separate code in template files. Template files works better on other scenarios.
March 30, 2017 at 3:29 am #253277AtelierbramParticipantMisunderstanding; I referred to the Genesis framework (but there are more of them) you are using: do you really need it?
March 30, 2017 at 4:36 am #253284KonspaulParticipantGenesis? Yes. It is best thing in WordPress. Very solid framework and child theme editing is extremely easy.
March 30, 2017 at 5:03 am #253291AtelierbramParticipantHave different experiences myself, and would never recommend it to beginners in HTML/CSS/PHP. For I think then they will be learning how to use “the framework” while skipping on learning the basics, and getting in trouble later on …
But as they say: to each their own ;\
May 30, 2017 at 10:33 pm #255370braddaltonParticipantYou could also use genesis_get_image as this is the function for featured images in Genesis.
Then you can add a if statement, ternary or filter to control the output when the featured image is not added.
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.