admin管理员组文章数量:1026989
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:
Share Improve this question asked Mar 20, 2023 at 12:51 SunnySunny 31 bronze badgeUncaught ReferenceError: is_single is not defined
1 Answer
Reset to default 0None 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
.
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:
Share Improve this question asked Mar 20, 2023 at 12:51 SunnySunny 31 bronze badgeUncaught ReferenceError: is_single is not defined
1 Answer
Reset to default 0None 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
.
本文标签:
版权声明:本文标题:php - Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737415712a1472131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论