- This topic is empty.
-
AuthorPosts
-
July 5, 2014 at 10:21 am #174742
taxicss
ParticipantHi,
Is it possible to give a DIV a custom class depending if the post/page has a “featured image” set?
I have a custom page template which has a big banner image on top. The page title is positioned inside this banner image. Now I would like to style this banner DIV if the page doesn’t have a “featured image” set.
Thanks.
July 5, 2014 at 10:25 am #174744Paulie_D
MemberDo the pages with the featured image have a specific class…because CSS could do this?
Also JS/JQ.
July 5, 2014 at 10:29 am #174745Senff
Participant<div <?php if ( has_post_thumbnail() ) { ?> class="something" <?php } ?> >
Not tested though, but I think this should work as long as it’s within the Loop.
July 5, 2014 at 10:44 am #174747taxicss
Participant@Paulie_D No they don’t. How could I do that so every new page that’s created/will be created will have this specific class?
EDIT: ahh I didn’t think enough, I could add a specific class on the body tag of my_custom_template.php, lol. But how could CSS know if there’s a featured image?
@Senff I would try that when I get home. Currently my code is like this<div> <div><?php echo get_the_post_thumbnail( 'full'); ?></div> <h1>title</h1> </div>
July 5, 2014 at 12:39 pm #174750Paulie_D
MemberWhat I am say saying that if you apply a specific class to the body tag of pages that do NOT have this featured image then you can address the pages that don’t with…
body:not(.featured-image_class) { }
It’s kind of a doube-negative (reverse thinking) solution if you can’t add a class to the pages that DO have the featured image
July 5, 2014 at 8:00 pm #174769Senff
ParticipantI don’t think the body doesn’t get a different class depending on whether or not a post has a featured image.
There’s no need for that since you can use the conditional statement I mentioned earlier.
July 5, 2014 at 10:27 pm #174772chrisburton
Participantor you can use the NOT(
!
) operator.What this code is saying is,
if this template/page DOES NOT have a post_thumbnail/featured image, output <div class="something">
. @Senff’s code does basically the same thing however, I like to write code in terms of how I think.<?php if(!has_post_thumbnail()): ?> <div class="something"> <?php endif; ?>
July 6, 2014 at 12:28 am #174775Ilan Firsov
ParticipantOr if you really want to have a body class, take a look at class_body filter (and add_filter function )
You can add a class like this (in functions.php file):function mrb_filter_post_classes( $classes ) { global $post; if( has_post_thumbnail( $post->ID ) ) { $classes[] = "has-post-thumbnail"; } return $classes; } add_filter( 'post_class', 'mrb_filter_post_classes' );
*block code doesn’t want to work for me today…
ok, it suddenly started to work when I wrapped it in inline codeEdit: just realized that using
body_class
will not work on blog page / archive page or anything displaying multiple posts on the same page. Just replacebody_class
withpost_class
and you should be good to go. I updated the snippet aboveJuly 6, 2014 at 12:40 am #174777taxicss
ParticipantOk I got it. I think the solution of @Senff or @chrisburton best fit my requirements.
Thanks to all the suggestion though.
Performance | Best practices question:
Since the featured image is used as a banner (aesthetics), is it better if I would just apply it as background-image in CSS? I’m not sure if it’s possible to put PHP code in CSS. Thoughts?
July 6, 2014 at 4:20 am #174785chrisburton
Participant@taxicss I think we need more information on the whole scope of what you’re trying to accomplish to be able to answer your question. All we know at this point is that you needed to add a class when there are no featured images. That doesn’t really give us any perspective on what you’re doing to answer your question on whether that is the best approach in regards to performance or best practices. Can you be a bit more clear?
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.