首页 > 解决方案 > 批量添加到父母和孩子

问题描述

寻找一种仅在父页面及其所有子页面上显示特定脚本的方法。我正在尝试完成此操作,而无需进入并添加每个父/子 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

    }

});

我还是新手,所以根本不知道如何使用最后一点。

标签: phpwordpress

解决方案


根据您的需要,将此代码添加到您的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
};

解释

  1. 该代码使用WordPress 操作 wp_footer将我们的自定义代码添加到页脚

  2. is_tree()是一个自定义函数,用于检查特定页面及其子页面 ID


推荐阅读