javascript - 如何在 wordpress 中使用 out 插件创建浏览器推送导航
问题描述
我想将所有最新帖子显示为来自我的新 wordpress 网站的通知。我有一个用于浏览器通知的 JavaScript 代码,但我无法从每个最新帖子中动态调用描述,请帮助我根据需要修改此代码。
<script type="text/javascript" charset="UTF-8">
var articles = [
<?php $the_query = new WP_Query( 'posts_per_page=20' );
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
["<?php the_title(); ?>","<?php the_permalink() ?>"],<?php endwhile; wp_reset_postdata(); ?>
["100% FREE Offers Available on KiduDeals.com","<?php echo get_home_url(); ?>"]
];
setTimeout(function(){
var x = Math.floor((Math.random() * 10) + 1);
var title=articles[x][0];
var desc='<?php the_expert(50); ?>';
var url=articles[x][1];
notifyBrowser(title,desc,url);
}, 50000);
document.addEventListener('DOMContentLoaded', function (){
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}
});
function notifyBrowser(title,desc,url){
if (!Notification) {
console.log('Desktop notifications not available in your browser..');
return;
}
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}
else {
var notification = new Notification(title, {
icon:'<?php echo get_bloginfo('template_url');?>/assets/images/favicon.png',
body: desc,
});
// Remove the notification from Notification Center when clicked.
notification.onclick = function () {
window.open(url);
};
// Callback function when the notification is closed.
notification.onclose = function () {
console.log('Notification closed');
};
}};
</script>
解决方案
推荐阅读
- angular - 路由器插座内未知的角度组件
- python - 为什么处理许多 CSV 文件的代码在运行时会变慢?
- scroll - 如何在 Vue 3 Composition Api 中滚动时向导航栏添加和删除类
- android - 命令:flutter build apk -vv error FileSystemException: Cannot open file app.so errno = 2
- android-studio - 如何使 TextView 对象从变为不可见的 TextView 对象继承约束?
- javascript - 一些图片加载延迟 20 秒后无任何原因
- javascript - HTML 复制到剪贴板按钮不起作用
- kotlin - 收集内部流时取消协程
- c++ - 树突变和共享指针问题的访问者模式
- java - 有什么方法可以防止 Spring boot 在连接到 Kafka 时为 client-id 添加后缀?