我正在使用一个WP - Alert插件用于wordpress网站。问题是它显示在所有页面上,即使它设置为仅在主页上显示。(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 = 真 ;}
提前感谢伙计们:)
WordPress在当前
页面设置$wp_query
对象之前加载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-functions-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
}
}
}
?>
希望这对你有用