网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > WordPress
User Name
Password
 
FAQ Members List Calendar Cheap Glasses Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1   IP: 114.238.82.223
Old 2013-09-18, 10:25 PM
topvip topvip is offline
超级版主
 
Join Date: 2006-01-04
Posts: 1206
topvip 正向着好的方向发展
Default wordpress判断是否首页的if(is_home())不起作用?

利用is_home()判断是否为首页,如果不起作用,大致有如下两个常见原因:

第一种:

  当你的首页不是默认的index.php的时候,而是在后台指定了一个page页面。这种情况下is_home()会失效,也就是说这样子的情况下就不能再用is_home()来判断。

  is_front_page()是判断当前页是不是指定的首页,我们在上面描述的情况下需要的就是这个函数。


Code:
<?php if (is_home() || is_front_page()) { ?>
我只会在首页显示
<?php } ?>
  ps:我在使用多站点wordpress进行二次开发时,需要所有的站点均指定一个page作为首页来显示;而且,该page作为首页显示时,页头还要显示一个banner图片。这就需要对所有theme主题的page.php文件内使用上述代码以判断是否首页。

第二种:

  如果is_home()之前有个 query_posts(),则会让它本身判断失效。原因是 is_home() 函数在首页的时候会返回一个 true 来判断,而 query_posts()会阻断这一判断。

  解决方案是在 is_home()之前加一个 wp_reset_query()。

Code:
<?php wp_reset_query(); if ( is_home() ) { ?> 
我只会在首页显示
<?php } ?>
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 12:25 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.