我有一个解决方案.我已经尽可能多地测试了代码,我想到了尽可能多的场景.
我使用的代码与我在this answer到this question中使用的代码相同.所以我不打算再次处理该部分
工作流程第1部分
我们将利用jquery来隐藏通知栏,以及一个有两个目的的cookie,用于保存最新的帖子ID并保持通知隐藏,直到新帖子发布或到期时间到期为止
为此,我们将使用jquery中的hide()函数在用户单击隐藏按钮时隐藏通知栏.您可以根据需要自定义此按钮或使用任何其他类型的符号.
我们现在需要使用一些方法来隐藏按钮,直到发布新帖子.这将通过在单击隐藏按钮时设置cookie来完成. Cookie会在2天后过期,因此如果在这两天内没有发布新帖子,则Cookie会自动过期.要设置cookie,我们需要下载jquery-cookie插件.当仍然设置cookie时,如果发布新帖子,此插件也会强制删除cookie.
本节严重依赖于new_post_notification中设置的帖子ID.问题是,你不能直接将php变量传递给jquery.幸运的是,Wordpress有一个名为wp_localize_script的函数,我们可以使用它将帖子ID传递给jquery脚本,我们将把它作为cookie值用于它.
这是第1节的结尾,让我们得到编码
LETS CODE SECTION 1
首先,下载插件,解压缩并将jquery.cookie.js文件复制到主题的js文件夹中.接下来,在js文件夹中创建一个新文件,并将其命名为hide.notification.bar.js.打开这个新创建的文件并将以下代码粘贴到那里并保存
jQuery(document).ready(function($) {
$("#notification_hide_button").click(function(){
$(this).hide();
$(".notifications_bar").hide();
if ($.cookie( 'hide_post_cookie' ) ) {
$.cookie( 'hide_post_cookie', null )
}
var post_id = parseInt( cookie_Data.post_id, 10 );
$.cookie( 'hide_post_cookie', post_id, { expires: 2, path: '/' } );
});
});
这是用于隐藏通知栏的代码,用于设置cookie. var post_id = parseInt(cookie_Data.post_id,10);将保留帖子ID,这是最重要的信息
我们现在需要注册和排队这两个js文件,并将帖子ID设置为wp_localize_script函数.打开你的functions.php并在里面粘贴以下内容.如果您的主题中已经有一个wp_enqueue_scripts挂钩,只需从此处删除相关代码并将其粘贴到您的功能中
function enqueue_cookie_scripts() {
wp_enqueue_script( 'jquery-cookie', get_template_directory_uri() . '/js/jquery.cookie.js', array( 'jquery' ), '' , true );
wp_register_script( 'set-cookie-option', get_template_directory_uri() . '/js/hide.notification.bar.js', array( 'jquery', 'jquery-cookie'), '' , true );
$cookie_data = array(
'post_id' => get_option( 'new_post_notification' )->ID
);
wp_localize_script( 'set-cookie-option', 'cookie_Data', $cookie_data ); // this one do the magic
wp_enqueue_script( 'set-cookie-option' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_cookie_scripts' );
您还可以复制并粘贴在发布新帖子时设置new_post_notification选项的函数.有关此代码如何工作的参考,请查看here.此代码进入functions.php
add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
//Check if our post status then execute our code
if ( $new_status == 'publish' && $old_status != 'publish' ) {
if ( get_option( 'new_post_notification' ) !== false ) {
// The option already exists, so we just update it.
update_option( 'new_post_notification', $post );
} else {
add_option( 'new_post_notification', $post );
}
}
}, 10, 3 );
工作流程第2部分
我们现在已经有了jquery工作的所有内容,我们现在需要设置显示通知栏的功能,并显示隐藏按钮并删除cookie,如果cookie尚未过期则设置新帖子.
此代码已得到很好的评论,因此您现在将无法遵循它.这里最重要的部分是获取cookie的值,该值存储在全局变量中,可以使用$_COOKIE [‘hide_post_cookie’]检索.这实际上是一个帖子ID,这将根据存储在get_option(‘new_post_notification’) – > ID中的帖子进行检查
隐藏按钮将隐藏< div class =“notifications_bar”>< / div>中的任何内容,因此您将在该div中添加通知栏.根据需要自定义.
我在函数中添加了所有代码,您可以在标题中调用,如下所示
echo get_new_post_notification_bar();
第2节代码
这段代码也会进入你的functions.php
function get_new_post_notification_bar() {
// Get the new_post_notification which holds the newest post
$notification = get_option( 'new_post_notification' );
// Get the post ID saved in the cookie
$cookie_post_ID = isset( $_COOKIE['hide_post_cookie'] ) ? (int) $_COOKIE['hide_post_cookie'] : false;
$output = '';
if( false != $notification ) {
//First check if we have a cookie, if not, show the notification bar
// If a cookie is set, do not display the notification bar
if( false === $cookie_post_ID ) {
//Get the post's gmt date. This can be changed to post_date
$post_date = strtotime( $notification->post_date_gmt );
//Get the current gmt time
$todays_date = current_time( 'timestamp', true );
//Set the expiry time to two days after the posts is published
$expiry_date = strtotime( '+2 day', $post_date );
//Show the notification bar if the expiry date is not yet reached
if( $expiry_date > $todays_date ) {
$output .= '
';$output .= '';
$output .= 'Hide';
$output .= '';
}
}else{
/**
* If a cookie is set, check the cookie value against the post id set as last post
* If the two don't match, delete the cookie and show the notification bar if a new post is published
* This code only run once, that is when a cookie is still set, and new post is published within the time
* in which the cookie is still set
*/
if( (int) $notification->ID !== $cookie_post_ID ) {
?>
jQuery(document).ready(function($) {
$.removeCookie('hide_post_cookie', { path: '/' });
});
$output .= '
';$output .= '';
$output .= 'Hide';
$output .= '';
}
}
}
return $output;
}