$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only

matteradmin7PV0评论

I wrote below code but facing issue to run it only on archive, categories, author, date pages to improve page speed.

add_action( 'wp_footer', function () { ?>

<script>

if( is_archive || is_category() || is_author() || is_date()) {

  // targets spans containing text
  let CalendarPrevBtn = document.getElementsByClassName("wp-calendar-nav-prev");
  let CalendarNextBtn = document.getElementsByClassName("wp-calendar-nav-next");

  // stores spans text
  let PrevBtnText = CalendarPrevBtn[0].textContent;
  let NextBtnText = CalendarNextBtn[0].textContent;

  // deciding if btn needs to be hidden
  Array.from(CalendarPrevBtn).forEach((x) => {
    if (!PrevBtnText.trim()) {
      x.style.display = "none";
    } else {
      // x.style.display = "block";
    }
  });

  Array.from(CalendarNextBtn).forEach((x) => {
    if (!NextBtnText.trim()) {
      x.style.display = "none";
    } else {
      // x.style.display = "block";
    }
  });

}
</script>

<?php } );

When I run this code it says:

Uncaught ReferenceError: is_single is not defined

I wrote below code but facing issue to run it only on archive, categories, author, date pages to improve page speed.

add_action( 'wp_footer', function () { ?>

<script>

if( is_archive || is_category() || is_author() || is_date()) {

  // targets spans containing text
  let CalendarPrevBtn = document.getElementsByClassName("wp-calendar-nav-prev");
  let CalendarNextBtn = document.getElementsByClassName("wp-calendar-nav-next");

  // stores spans text
  let PrevBtnText = CalendarPrevBtn[0].textContent;
  let NextBtnText = CalendarNextBtn[0].textContent;

  // deciding if btn needs to be hidden
  Array.from(CalendarPrevBtn).forEach((x) => {
    if (!PrevBtnText.trim()) {
      x.style.display = "none";
    } else {
      // x.style.display = "block";
    }
  });

  Array.from(CalendarNextBtn).forEach((x) => {
    if (!NextBtnText.trim()) {
      x.style.display = "none";
    } else {
      // x.style.display = "block";
    }
  });

}
</script>

<?php } );

When I run this code it says:

Uncaught ReferenceError: is_single is not defined

Share Improve this question asked Mar 20, 2023 at 12:51 SunnySunny 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

None of those functions are JavaScript functions. They're PHP functions. You need to run them outside the <script> tag in the PHP:

add_action(
    'wp_footer',
    function () {
        if ( is_archive() || is_category() || is_author() || is_date() ) {
            ?>
            <script>
                // etc.
            </script>
            <?php
        }
    }
);

You were also missing the () on is_archive.

Post a comment

comment list (0)

  1. No comments so far