[英]wordpress is_front_page() is not working (static front page)
我正在为WordPress网站使用WP-Alert插件。 问题是,即使将其设置为仅在主页上显示,它也会显示在所有页面上。 (is_home)。 但是,我的主页是静态主页,因此我将其设置为is_front_page,并且仍显示在所有页面上。 如果您能让我知道我是否错过了什么?
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$display = true;
$homeOption = get_option('alert_show_on_home');
$pageOption = get_option('alert_show_on_page');
$postOption = get_option('alert_show_on_post');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
$display = true ;
}elseif($pageOption == 'yes' && is_page()){
$display = true ;
}elseif($postOption == 'yes' && is_single()){
$display = true ;
}else{
$display = false ;
}
if($display){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
我将此行添加到了上面的代码中,但它仍然不会仅显示在静态主页上。 } elseif($ homeOption =='yes'&& is_front_page()){$ display = true; }
在此先感谢大家:)
在当前页面设置$wp_query
对象之前,WordPress会加载functions.php
。 is_front_page
是$wp_query->is_front_page()
的包装,如果尚未设置查询,则总是返回false。
有关此主题的更多信息,请参见以下问题: https : //wordpress.stackexchange.com/questions/41805/is-front-page-only-works-in-theme-file-and-does-not-work-in-函数-php
要在您的functions.php
使用它,必须将其包装在实例化$ wp_query对象之后的操作中。
add_action('wp', function () {
if (!is_front_page()) {
add_action('wp_print_styles', function () {
wp_deregister_style('wpos-slick-style');
wp_deregister_style('wpsisac-public-style');
}, 100);
}
});
现在显示在您的静态主页中吗?
尝试这个
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$homeOption = get_option('alert_show_on_home');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
希望这对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.