php - How to set up a If is_singular statement?

I've got this code here, and I want it to exclude this div from being parsed if the condition is false.<?php if

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
  • Is it just that the logic is wrong? You're saying if it's NOT is_single(), but your explanation says you DO want it loading on single posts. Try this instead if( 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:48
  • If I reverse it, it disables the div even if the post type is single. It's all true or all false. – Devon Commented Feb 1, 2020 at 4:53
  • 1 Are you calling it within a template? We're missing something simple here... ...not sure what. – Tony Djukic Commented Feb 1, 2020 at 4:57
  • 2 Ok, that's the issue - the search.php is neither a page nor a post so your logic instead has to execute on each of the returned items. Instead of is_single() you want to instead check if it's a post for each of the returned search results, so instead try if ( 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 the while( have_posts() ) : the_post();? – Tony Djukic Commented Feb 1, 2020 at 5:11
  • 1 That did the trick! Thank you :) – Devon Commented Feb 1, 2020 at 5:14
 |  Show 1 more comment

1 Answer 1

Reset to default 2

is_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

相关推荐

  • php - How to set up a If is_singular statement?

    I've got this code here, and I want it to exclude this div from being parsed if the condition is false.<?php if

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信