admin管理员组文章数量:1390957
We need to add two snippets of code, one right below the opening body tag, and the other right before the closing body tag. What is the best way to do this? I checked out the wp_enqueue_script, but it appears the content would be in the head section.
We need to add two snippets of code, one right below the opening body tag, and the other right before the closing body tag. What is the best way to do this? I checked out the wp_enqueue_script, but it appears the content would be in the head section.
Share Improve this question asked Jul 23, 2012 at 4:51 linnselinnse 1611 gold badge1 silver badge5 bronze badges5 Answers
Reset to default 13Did you even open header.php
and take a peek? You'll see genesis_before()
called right after the opening <body>
tag - follow the white rabbit and you get:
function genesis_before() { do_action('genesis_before'); }
And likewise for the footer. So...
add_action( 'genesis_before', 'im_a_lazy_copy_paster' );
add_action( 'genesis_after', 'im_a_lazy_copy_paster' );
function im_a_lazy_copy_paster() {
if ( current_filter() == 'genesis_before' )
echo '<script>party.start();</script>';
else
echo '<script>if ( cops.called() ) party.split();</script>';
}
You can use a plugin such as this to show all of the Genesis hooks: http://wordpress/plugins/genesis-visual-hook-guide/. This will allow you to quickly find the right hook to modify to do this. Then modify the hook in the functions file. You can also use a plugin which allows you to easily modify hooks through the wp-admin: http://www.studiopress/plugins/simple-hooks
You were on the right track. wp_enqueue_script
takes a parameter called in_footer which defines whether your script should be loaded before page content or at the end of the page body.
$in_footer: (boolean) (optional) Normally scripts are placed in the
<head>
section. If this parameter is true the script is placed at the bottom of the<body>
. This requires the theme to have thewp_footer()
hook in the appropriate place. Note that you have to enqueue your script beforewp_head
is run, even if it will be placed in the footer. Default:false
Here is the reference on codex: http://codex.wordpress/Function_Reference/wp_enqueue_script
After the opening body tag.
add_action( 'genesis_before', 'my_genesis_script' ); function my_genesis_script() { if ( current_filter() == 'genesis_before' ) echo '<script>parties.over();</script>'; }
Structural Action Hooks
genesis_before: This hook executes immediately after the opening tag in the document source.
- Before the closing body tag:
You can add the script to the Genesis > Theme Settings > Header and Footer scripts and enter your script you would like output to wp_footer().
The wp_footer() hook executes immediately before the closing tag in the document source.
An update for GenesisWP 3.3.1
Go to header.php
?>
</head>
<?php
genesis_markup(
[
'open' => '<body %s>',
'context' => 'body',
]
);
if ( function_exists( 'wp_body_open' ) ) {
wp_body_open();
}
echo '<script>parties.over();</script>';
/**
* Fires immediately after the `wp_body_open` action hook.
*
* @since 1.0.0
*/
do_action( 'genesis_before' )
本文标签: How to add code just below opening body tag in Genesis framework
版权声明:本文标题:How to add code just below opening body tag in Genesis framework 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744589081a2614372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论