获取一个动作钩子被激活的次数
描述
检索动作被激活的次数
用法
<?php did_action( $tag ); ?>
参数
$tag
(string) (必填) The name of the action hook.
默认值: None
返回值
(integer)
The number of times action hook $tag is fired
示例
使用 did_action() 函数确保仅在第一次运行期间添加自定义元字段,因为它可以运行多次。
function my_sticky_option()
{
global $post;
// if the post is a custom post type and only during the first execution of the action quick_edit_custom_box
if ( $post->post_type == ‘custom_post_type’ && did_action( ‘quick_edit_custom_box’ ) === 1 )
{
?>
<fieldset class=“inline-edit-col-right”>
<div class=“inline-edit-col”>
<label class=“alignleft”>
<input type=“checkbox” name=“sticky” value=“sticky” />
<span class=“checkbox-title”>
<?php _e( ‘Featured (sticky)’, ‘textdomain_string’ ); ?>
</span>
</label>
</div>
</fieldset>
<?php
} // endif;
}
// add the sticky option to the quick edit area
add_action( ‘quick_edit_custom_box’, ‘my_sticky_option’ );
注意
使用到 global: (unknown type) $wp_actions
历史
添加于 版本: 2.1
源文件
did_action() 函数的代码位于 wp-includes/plugin.php
.
/**
* Retrieve the number of times an action is fired.
*
* @since 2.1.0
*
* @global array $wp_actions Increments the amount of times action was triggered.
*
* @param string $tag The name of the action hook.
* @return int The number of times action hook $tag is fired.
*/
function did_action($tag) {
global $wp_actions;
if ( ! isset( $wp_actions[ $tag ] ) )
return 0;
return $wp_actions[$tag];
}
相关
Actions:
has_action(),
add_action(),
do_action(),
do_action_ref_array(),
did_action(),
remove_action(),
remove_all_actions()