I've got this code here, and I want it to exclude this div from being parsed if the condition is false.
<?php if ( ! is_single() ) { ?>
<div class="cat-container">
<a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
</div>
<?php } ?>
However, It does not work as intended. It's either true or false for all results depending on if ! is included or not. I feel like I'm missing something really simple.
Basically, I don't want the div to be sent to the loop If it is a page, not a post(Since pages don't have categories.)
I've tried is_single, Is_singluar, and ('post') in there as well, to no avail.
Thanks for any insight
I've got this code here, and I want it to exclude this div from being parsed if the condition is false.
<?php if ( ! is_single() ) { ?>
<div class="cat-container">
<a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
</div>
<?php } ?>
However, It does not work as intended. It's either true or false for all results depending on if ! is included or not. I feel like I'm missing something really simple.
Basically, I don't want the div to be sent to the loop If it is a page, not a post(Since pages don't have categories.)
I've tried is_single, Is_singluar, and ('post') in there as well, to no avail.
Thanks for any insight
Share Improve this question asked Feb 1, 2020 at 4:37 DevonDevon 356 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2is_single()
and is_singular()
are not the correct functions to use here.
Your comment mentions that this is being added to search.php
, but is_single()
and is_singular()
will always be false on search.php
, because those functions are checking if the current page is a single post or page, but search.php
is not. It's is a list of multiple results. is_single()
is not checking the current item in a loop, it's checking, essentially, what is represented by the current URL.
This is why it's displaying for ! is_single()
, because is_single()
is false
, and ! false
is true
.
If you need to determine the post type of a post within a loop, you can use get_post_type()
:
<?php if ( 'post' === get_post_type() ) { ?>
<div class="cat-container">
<a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
</div>
<?php } ?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744784593a4593546.html
is_single()
, but your explanation says you DO want it loading on single posts. Try this insteadif( is_single( 'post' ) ) {
You want to exclude the DIV if it's false, but you're actually including it ONLY if it's false. – Tony Djukic Commented Feb 1, 2020 at 4:48is_single()
you want to instead check if it's a post for each of the returned search results, so instead tryif ( get_post_type( get_the_ID() ) == 'post' ) { //if is true }
. Want me to put it into an answer? Also, I assume this is executing within thewhile( have_posts() ) : the_post();
? – Tony Djukic Commented Feb 1, 2020 at 5:11