检查是否在特定页面中调用了the_excerpt()函数

时间:2010-06-11 12:10:58

标签: wordpress

如何检查存档页面中特定页面示例中是否调用了the_excerpt()函数。

我想知道是否使用the_excerpt()函数来显示内容,或者是否使用the_content()来显示档案/类别页面中的内容。

如何检查这个。有没有条件标签

1 个答案:

答案 0 :(得分:1)

没有条件标签,但有一个过滤器。如果您挂钩'get_the_excerpt'过滤器,您的函数将会运行。所以,作为一个例子:

if(is_archive() || is_category()){
  add_filter( 'get_the_excerpt', 'my_custom_excerpt_filter' );
}

function my_custom_excerpt_filter( $excerpt ){
  if( !defined( 'USED_EXCERPT' ) ){
    define( 'USED_EXCERPT', true );
  }
}

然后,为了检查是否使用了摘录,在循环的第一次迭代之后的任何时间,使用:

if(defined('USED_EXCERPT') && USED_EXCERPT){
  //If the excerpt was used, this if statement returns true.
}

这不是一种非常优雅或有用的方法,但这就是你要做的。如果您需要知道模板在加载模板之前是否会使用摘录,您必须先提前挂钩,使用fopen()读取模板文件并搜索the_excerpt()