php - 批量添加到父母和孩子
问题描述
寻找一种仅在父页面及其所有子页面上显示特定脚本的方法。我正在尝试完成此操作,而无需进入并添加每个父/子 URL。
这是我所拥有的:
function answerConnect_add_script_wp_footer() {
/** Tells what pg to display on AND it's child pgs **/
if (is_page ( array('foley-al','fargo-nd','advantages','project-gallery','cabinet-redooring') ) ) {
?>
<script>
console.log("I'm an inline script tag added to the footer.");
( function( a , b , c , d , e , f , g ) { c[d] = c[d] || function() { (c[d].q = c[d].q || []).push(arguments); }; c[ '_lsAlias' ] = c[ d ]; e = a.createElement(b); e.type = 'text/javascript'; e.async = true; e.src = 'https://app.chatsupport.co/api/client/get/script/LS-1f4db298'; f = function() { g = a.getElementsByTagName(b)[0]; g.parentNode.insertBefore( e , g ); }; c.addEventListener( 'load' , f ); } )( document , 'script' , window , '_ls' ); _ls( 'init' , { 'projectId' : 'LS-1f4db298' } );
</script>
<?php
}
}```
I was later told this was wrong and should be:
```add_action( ‘wp_enqueue_scripts’, function() {
if ( is_page() && in_array( $ //pageidofparent, get_post_ancestors( get_the_id()))) {
?>
<script>
console.log("I'm in your footer.");
( function( a , b , c , d , e , f , g ) { c[d] = c[d] || function() { (c[d].q = c[d].q || []).push(arguments); }; c[ '_lsAlias' ] = c[ d ]; e = a.createElement(b); e.type = 'text/javascript'; e.async = true; e.src = 'https://app.chatsupport.co/api/client/get/script/LS-1f4db298'; f = function() { g = a.getElementsByTagName(b)[0]; g.parentNode.insertBefore( e , g ); }; c.addEventListener( 'load' , f ); } )( document , 'script' , window , '_ls' ); _ls( 'init' , { 'projectId' : 'LS-1f4db298' } );
</script>
<?php
}
});
我还是新手,所以根本不知道如何使用最后一点。
解决方案
根据您的需要,将此代码添加到您的wp-content/your-theme/functions.php
:
add_action( 'wp_footer', 'my_footer_scripts' );
function my_footer_scripts(){
if (is_tree(2)) { // Your parent page ID
?>
<script>
console.log("I'm an inline script tag added to the footer.");
( function( a , b , c , d , e , f , g ) { c[d] = c[d] || function() { (c[d].q = c[d].q || []).push(arguments); }; c[ '_lsAlias' ] = c[ d ]; e = a.createElement(b); e.type = 'text/javascript'; e.async = true; e.src = 'https://app.chatsupport.co/api/client/get/script/LS-1f4db298'; f = function() { g = a.getElementsByTagName(b)[0]; g.parentNode.insertBefore( e , g ); }; c.addEventListener( 'load' , f ); } )( document , 'script' , window , '_ls' ); _ls( 'init' , { 'projectId' : 'LS-1f4db298' } );
</script>
<?PHP
}
}
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
解释
该代码使用WordPress 操作
wp_footer
将我们的自定义代码添加到页脚is_tree()
是一个自定义函数,用于检查特定页面及其子页面 ID
推荐阅读
- html - Nu Html 检查器错误“扩展为回车的数字字符引用。” 使用“”时
- graphql - 在 AWS 上使用 Cloudfoundry 部署的 Hasura GraphQL 引擎上的订阅错误
- java - F 有界多态 Java 中的返回类型
- sql - PostgreSQL 如何查询字符串数组
- asp.net-core-mvc - 从下拉列表中获取选定项目并将其传递给 onClick 事件
- javascript - React 使用仪表板构建 CMS 如何显示隐藏仪表板组件
- swift - 即使在设备旋转之后也约束/居中 CAEmitterLayer
- python - pyrequests 将 dict 值编码为 JSON 中的列表
- r - 为什么 dplyr 过滤器不接受整数数据帧?
- java - 为什么我收到 java.sql.SQLException:当 url 正确且驱动程序存在时找不到合适的驱动程序?