我正在查找传递给筛选函数的参数。我在哪里可以在法典中找到这样的信息?
http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content 没有提供太多信息
我想知道这个帖子是不是别人的孩子
我正在查找传递给筛选函数的参数。我在哪里可以在法典中找到这样的信息?
http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content 没有提供太多信息
我想知道这个帖子是不是别人的孩子
我认为没有任何额外的参数传递给the_content
, 但全局变量如下$post 可访问。
所以像这样的方法是可行的:
add_filter( \'the_content\', \'check_for_post_parent\' );
function check_for_post_parent($content) {
global $post;
if ($parent_id == $post->post_parent) {
//do what you want to $content here,
//now that you know $parent_id
//...
}
return $content;
}
在里面wp-includes/post-template.php
您将找到应用过滤器的位置:
/**
* Display the post content.
*
* @since 0.71
*
* @param string $more_link_text Optional. Content for when there is more text.
* @param string $stripteaser Optional. Teaser content before the more text.
*/
function the_content($more_link_text = null, $stripteaser = 0) {
$content = get_the_content($more_link_text, $stripteaser);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
echo $content;
}
唯一的参数是内容文本。但您始终可以使用全局变量$post来获取有关当前使用的post的更多信息。在主题函数中尝试以下代码段。php:
/*
* Priority 100 to let other filters do their work first.
*/
add_filter( \'the_content\', \'debug_post_info\', 100 );
/**
* Print information about the current post.
*
* @param string $content
* @return string
*/
function debug_post_info( $content )
{
return $content . \'<hr><pre>\' . var_export( $GLOBALS[\'post\'], TRUE ) . \'</pre>\';
}
在子页面上,您可以获得一些不错的数据:stdClass::__set_state(array(
\'ID\' => 2168,
\'post_author\' => \'2\',
\'post_date\' => \'2007-09-04 09:52:18\',
\'post_date_gmt\' => \'2007-09-03 23:52:18\',
\'post_content\' => \'This page has a parent.\',
\'post_title\' => \'Child page 2\',
\'post_excerpt\' => \'\',
\'post_status\' => \'publish\',
\'comment_status\' => \'open\',
\'ping_status\' => \'open\',
\'post_password\' => \'\',
\'post_name\' => \'child-page-2\',
\'to_ping\' => \'\',
\'pinged\' => \'\',
\'post_modified\' => \'2007-09-04 09:52:18\',
\'post_modified_gmt\' => \'2007-09-03 23:52:18\',
\'post_content_filtered\' => \'\',
\'post_parent\' => 2167,
\'guid\' => \'http://wpthemetestdata.wordpress.com/parent-page/child-page-1/child-page-2/\',
\'menu_order\' => 0,
\'post_type\' => \'page\',
\'post_mime_type\' => \'\',
\'comment_count\' => \'0\',
\'ancestors\' =>
array (
0 => 2167,
1 => 2166,
),
\'filter\' => \'raw\',
))
\'post_parent\' => 2167
是父帖子的ID。在没有父级的页面上,参数设置为0
.我是updating the codex page example for action hooks, 在游戏中完成一些可重用的功能(最初是针对这里的一些Q@WA)。但后来我遇到了一个以前没有意识到的问题:在挂接到一个函数以修改变量的输出后,我再也无法决定是要回显输出还是只返回它。The Problem: 我可以修改传递给do_action 用回调函数钩住。使用变量修改/添加的所有内容仅在回调函数中可用,但在do_action 在原始函数内部调用。很高兴:我将其修改为一个工作示例,因此您可以将其复制/粘贴