有很多方法可以解决这个问题。我认为,根据您的评论,最好的解决方案是使用纯挂钩。我还是不太确定你在补充什么,这里有一些想法。(您建议在单独的页面中包含page.php是行不通的,因为所有代码都是从page.php中提取出来的。
1. add custom code to the footer of a set page. (hooked wp_footer)
在子主题的函数中。php添加以下代码:
function dd_custom_function() {
if (is_page(\'services\')) { //add your slug or id or other conditional here that you want to call your code
echo \'<p>This is inserted at the bottom</p>\';
}
}
add_action( \'wp_footer\', \'dd_custom_function\');
(如果要添加到标题,请更改
wp_footer
到
wp_header
)
2. Put your code in a separate file and pull it into your footer.
添加代码文件
custom.js
到/js/中的子主题目录/(创建该目录)
将此代码添加到函数中。子主题的php:(选择wp\\u排队行之一..一行用于页眉,一行用于页脚。)
function dd_enqueue_function() {
if (is_page(\'services\')) { //add your slug or id or other conditional here that you want to call your code
wp_enqueue_script(\'creative-blog-main-script\', get_stylesheet_directory_uri() . \'/js/custom.js\', array(\'jquery\'), null, true); // Here in this example the last value has been set as true, so, it will be loaded in the footer.
wp_enqueue_script(\'creative-blog-script\', get_stylesheet_directory_uri() . \'/js/custom.js\', array(\'jquery\'), null, false); // Here in this example the last value has been set as false, so, it will be loaded in the header.
}
}
add_action(\'wp_enqueue_scripts\', \'dd_enqueue_function\');
3. See if your theme has any actions set you can a hook into.
许多主题将为您设置可以挂钩的操作。它甚至可能在页脚中。php查找
do_action
并使用这些操作放置自定义代码。如果没有,您可以在页面中添加一行。php文件:
do_action(\'dd_cool_action\');
无论您希望代码在哪里执行。
然后在您孩子的自定义函数中调用代码以执行操作:
add_action(\'dd_cool_action\', dd_custom_function\', 5);
function dd_custom_function { ?>
<div> Drew David custom code activated </div>
<?php }