doubaran2438 2017-05-17 14:58
浏览 41
已采纳

WordPress忽略is_page()条件

<?php if( is_home()  || is_page(10)): ?> 
    <div class="text-center col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php elseif(is_single() && !is_page(10)): ?>
    <div class="text-center col-xl-9 col-lg-9 col-md-9 col-xs-12">
<?php endif; ?>

All I'm trying to do is change the classes assigned to this <div> if you are on a single post, which works fine, but I can't get it under control when viewing a contact page, which it seems to recognize as a single post. This is fine, but I want to exclude one page from this effect.

I've tried is_page('Contact'), is_page('contact'), and is_page(10) None of them seem to return true in any case, I even deleted all other conditionals and just had it print something if its the contact page, but it still doesn't return true and has no effect.

I've done a lot of searching and all I can find says I'm using it correctly.. I'm at a loss as to why it wouldn't return true?

Am I using php if statements incorrectly and it's been an 'oops, it worked' scenario thus far? The url to the page ends with ?page_id=10, the page is titled 'Contact'

I could create a page template, but it seems more than what I need here, the contact page is painfully simple, and all I really need to do is center the four lines of text appropriately using bootstrap. Works fine across all other pages of the website, and changes the <div> appropriately if you're on the homepage or viewing a single post.

As requested, the full index.php code is below. Thanks to anyone who is willing to look at this. It is nothing massive, I am relatively new so please excuse my excessive commenting - I'm still learning. Full index.php: https://pastebin.com/nnG0ny99

  • 写回答

3条回答 默认 最新

  • douhuang3833 2017-05-17 15:05
    关注

    You can try using:

    global $post;
    
    <?php if( is_home()  || $post->ID == 10): ?> 
    <div class="text-center col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <?php elseif(is_singular() && $post->ID != 10): ?>
    <div class="text-center col-xl-9 col-lg-9 col-md-9 col-xs-12">
    <?php endif; ?>
    

    That way you are getting the actual post id rather than the page id.

    Edit: try to change is_single() to is_singular() as saNs suggested in his answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?