钩子用于邮寄和页面加载
-
-
公共或管理员方面/面对?public or admin side/facing?
- 0
- 2012-10-16
- kaiser
-
对于前端.基本上,我希望它仅在显示特定的单个自定义帖子类型时触发.For front end. Basically I want it to trigger only when a particular single custom post type is displayed.
- 0
- 2012-10-16
- Poulomi Nag
-
3 个回答
- 投票数
-
- 2012-10-16
您可以使用
wp
钩子并检查global $wp_query
对象或任何条件的对象.add_action( 'wp', 'wpse69369_special_thingy' ); function wpse69369_special_thingy() { if ( 'special_cpt' === get_post_type() AND is_singular() ) return print "Yo World!"; return printf( '<p>Nothing to see here! Check the object!<br /></p><pre>%s</pre>', var_export( $GLOBALS['wp_query'], true ) ); }
请参阅:codex.wordpress.org中的
wp
和developer.wordpress.org 中的wp
>You can use the
wp
hook and check theglobal $wp_query
object or any conditional.add_action( 'wp', 'wpse69369_special_thingy' ); function wpse69369_special_thingy() { if ( 'special_cpt' === get_post_type() AND is_singular() ) return print "Yo World!"; return printf( '<p>Nothing to see here! Check the object!<br /></p><pre>%s</pre>', var_export( $GLOBALS['wp_query'], true ) ); }
See:
wp
in codex.wordpress.org andwp
in developer.wordpress.org-
您能告诉我'wp'钩何时运行吗?can you please tell me when does 'wp' hook run?
- 0
- 2012-10-16
- Poulomi Nag
-
A)它在`after_setup_theme`和`setup_theme`之前运行,因此它只能用于插件B)在WP ::main()中,可从wp-settings.php中调用.A) It runs before `after_setup_theme` and `setup_theme`, so it's only accessible for plugins B) inside `WP :: main()`, which is called from within wp-settings.php.
- 0
- 2012-10-16
- kaiser
-
@kaiser`wp`钩子在`after_setup_theme`钩子之后和在`template_redirect`之前就不会触发,因此使主题和插件都可以访问`wp`吗?(只是为了澄清?)@kaiser Doesn't the `wp` hook fire after the `after_setup_theme` hook and right before `template_redirect` therefore making `wp` accessible by themes as well as plugins? (just to clarify?)
- 1
- 2012-10-17
- Adam
-
- 2012-10-16
使用
template_redirect
,这是触发的操作挂钩呈现模板之前;add_action('template_redirect', 'hooker'); function hooker(){ //I load just before selecting and rendering the template to screen }
Use
template_redirect
which is the action hook that fires before rendering the template;add_action('template_redirect', 'hooker'); function hooker(){ //I load just before selecting and rendering the template to screen }
-
@PoulomiNag没问题,很高兴您在上面找到了答案.尽管我认为有一点要注意的是`wp`在`after_theme_setup`挂钩之后运行,所以它不仅可以通过插件访问,而且可以安全地在主题中使用.@PoulomiNag No problem, glad you found your answer above. Though I think one small note is that `wp` runs after the `after_theme_setup` hook, so its not just accessible by plugins, making it safe to use in themes.
- 0
- 2012-10-17
- Adam
-
我刚刚检查了,是的;wp在after_theme_setup之后运行.但是我的插件需要它.所以`wp`和`template_redirect`都适合我.希望我可以在这里接受两个答案!:)I just checked and yes ; `wp` runs after `after_theme_setup`. But I need it for my plugin. So `wp` as well as `template_redirect` both work fine for me. Wish I could accept two answers here! :)
- 0
- 2012-10-17
- Poulomi Nag
-
没关系,不必接受两者,只是想阐明它们触发的顺序.知道,确保我不会发疯.祝您的插件好运...That's ok, not necessary to accept both, just wanted to clarify the order in which they fire. Making sure I'm not going crazy you know. Good luck with your plugin...
- 0
- 2012-10-17
- Adam
-
功能名称双关语+1+1 for function name pun
- 3
- 2020-02-28
- MJHd
-
- 2012-10-16
我经常使用以下内容加载页面(而不是自定义帖子)上的自定义元框.
add_action('admin_init','how_we_do_it_meta'); function how_we_do_it_meta() { if ( $_SERVER['SCRIPT_NAME'] == '/wp-admin/post.php' ) { $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID']; $template_file = get_post_meta($post_id,'_wp_page_template',TRUE); if ($template_file == 'page-how-we-do-it.php') { add_meta_box('how_we_do_it_who-meta', 'Who we work with...', 'how_we_do_it_who', 'page', 'normal', 'high'); add_action('save_post', 'save_how_we_do_it_meta'); } } }
I've quite often used the following to load in custom meta boxes on pages (rather than custom posts).
add_action('admin_init','how_we_do_it_meta'); function how_we_do_it_meta() { if ( $_SERVER['SCRIPT_NAME'] == '/wp-admin/post.php' ) { $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID']; $template_file = get_post_meta($post_id,'_wp_page_template',TRUE); if ($template_file == 'page-how-we-do-it.php') { add_meta_box('how_we_do_it_who-meta', 'Who we work with...', 'how_we_do_it_who', 'page', 'normal', 'high'); add_action('save_post', 'save_how_we_do_it_meta'); } } }
-
谢谢达伦兹.但是我需要一些挂钩才能在页面加载期间在前端工作.有任何想法吗?Thanks Darronz. But I need some hook to work at the front end during a page load. Any ideas?
- 1
- 2012-10-16
- Poulomi Nag
-
如果您将上述`更改为add_action('init',//等),那么它将在页面加载中起作用,而不仅仅是在admin部分.If you changed the above `to add_action('init', // etc)` then it'll work on the page load rather than only in the admin section.
- 0
- 2012-10-16
- darronz
-
@darronz然后您需要检查`!里面是is_admin(),因为`init`钩子在两侧都运行.@darronz And then you need to check `! is_admin()` inside, because the `init` hook runs on both sides.
- 2
- 2012-10-16
- kaiser
当加载特定帖子或页面时,我需要运行一个函数.是否有任何钩子可以让我检查页面加载期间是否正在显示帖子?