Gimhoy's Blog

[文章目录]

您曾经浏览过

WordPress中is_home()/is_singular()等函数失效的解决办法

本文最后更新于2013年8月17日,已超过三年没有更新,如果文章内容失效,请反馈给我们,谢谢!

昨晚在折腾博客的时候,给文章页加了个随机文章列表,然后is_singular()的判断就失效了,导致一些js加载不了。开始还以为是加js的时候出了问题,后来才知道是因为query_post()这个函数的原因。具体的原理不是很懂,解决方法倒很简单:在引用query_posts()函数的结尾endwhile后面加上wp_reset_query()。比如我的随机文章小工具改后的效果:

<h3>随机文章</h3>
			<ul class="ulstyle">
				<?php $posts = query_posts('orderby=rand&showposts=5'); ?>  
				<?php while(have_posts()) : the_post(); ?> 
					<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a><span class="sidebaraction"></span></li> 
				<?php endwhile; wp_reset_query(); ?> 
			</ul>

 

附:WordPress中一些常见的判断函数

is_home() : 是否为主页

is_single() : 是否为内容页(Post)

is_page() : 是否为内容页(Page)

is_category() : 是否为Category/Archive页

is_tag() : 是否为Tag存档页

is_date() : 是否为指定日期存档页

is_year() : 是否为指定年份存档页

is_month() : 是否为指定月份存档页

is_day() : 是否为指定日存档页

is_time() : 是否为指定时间存档页

is_archive() : 是否为存档页

is_search() : 是否为搜索结果页

is_404() : 是否为 “HTTP 404: Not Found” 错误页

is_paged() : 主页/Category/Archive页是否以多页显示

is_singular() : 相当于is_single()||is_page()||is_attachment()



免费获得每月10G空间+10G免费流量
  • Comment (8)
  • Trackback (1)
  • sinsky Google Chrome Windows 2015/01/30 01:52 @Ta

    您曾经浏览过:
    undefined
    undefined
    undefined
    undefined

    左面坏掉了。

    #25
  • hopesap Mozilla Firefox Windows 2014/12/29 14:57 @Ta

    我用你的方法仍然没有解决is_archive() 无效的问题呢..

    #24
  • kk Internet Explorer Windows 2014/06/10 20:06 @Ta

    签到成功!签到时间:19:59:44,每日打卡,生活更精彩哦~

    #23
  • 麦田一根葱 Google Chrome Windows 2013/08/21 17:58 @Ta

    😥 我也在为这个烦恼,总算找到原因了

    #22
    • Gimhoy Google Chrome Windows 2013/08/21 20:53 @Ta

      感谢我吧 哈哈哈~

  • 梦月酱 Google Chrome Windows 2013/08/20 10:16 @Ta

    话说 左边那个错位了。

    #21
    • Gimhoy Google Chrome Windows 2013/08/20 20:35 @Ta

      右边吧? 下滑太快的话是会错位。。似乎是左右js有点冲突,有空再改改吧

  • 梦月酱 Google Chrome Windows 2013/08/20 10:15 @Ta

    每日打卡,打卡时间:上午10:15:30

    #20
Leave a Reply

*

*