did_action()获取一个动作钩子被激活的次数

目录

描述

译文

检索动作被激活的次数

原文

Retrieve the number of times an action is fired.

did_action() 描述

用法

<?php did_action$tag ); ?>

did_action() 用法

参数

$tag

(string) (必填) The name of the action hook.

默认值: None

did_action() 参数

返回值

(integer) 

The number of times action hook $tag is fired

did_action() 返回值

示例

Using did_action() function to make sure custom meta field is only added during the first run since it can run multiple times.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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' );

did_action() 示例

注意

  • 使用到 global: (unknown type) $wp_actions

did_action() 注意

历史

添加于 版本: 2.1

did_action() 历史

源文件

did_action() 函数的代码位于 wp-includes/plugin.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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];
}

did_action() 源文件

相关