我正在写一个插件,我需要显示有关帖子的管理员通知。我调用了提供消息的函数。
add_action('admin_notices', array($this, 'postNotices'));
这是我的功能:
public function postNotices() {
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_post = new WP_Query($args);
if ($query_post->have_posts()) {
while ($query_post->have_posts()) {
$query_post->the_post();
// $my_meta = 'Im getting post meta here'; ?>
<div>Some notification</div>
<?php wp_reset_query();
}
}
}
通知显示工作正常,但是当我尝试创建新帖子时,它会在新帖子页面中显示最后的帖子数据,例如帖子标题。我认为这意味着wp_reset_query()
功能无法与admin_notices
钩子一起使用。有没有人建议?
任何建议都将受到高度赞赏。
经过艰苦的研究,我找到了答案,我们走了,
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_posts = get_posts($args);
foreach ($query_posts as $post) {
?>
<div>Some notification</div>
<?php
}
无需添加功能,例如wp_reset_query()
。找到了答案,但不知道逻辑是什么。如果有人知道,请在这里评论。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句