dongzhao1930 2014-11-30 21:57
浏览 14
已采纳

如果WordPress中有新帖子,如何显示通知栏?

I want an alert bar at the top of my WordPress website, when there is a new post published on my blog. So the users are known that there is a new post available.

So I downloaded a WordPress plugin called: WPFront Notification Bar Only the problem is that I only can set this bar on or off. But not show this bar automatically when there is a new post and after a day it will reset the value to off and will only be switched back to on if there is a hole new post.

So I thought if I make in my header.php a notification bar like the plugin, style it to my website and add some javascript to it. Only the problem is, I don’t know how to do this and also can’t find something on the internet so the reason for this question is written above.

  • 写回答

1条回答 默认 最新

  • doq1969 2014-11-30 22:57
    关注

    I would solve that with a little code vs a plugin.

    Modify a template (of a child theme preferably) it might be page.php or header.php, depends on your theme. Add a function to functions.php to query for # of posts since whatever time you want your window to be. If posts are returned output html to create the alert bar styled to your taste; you could display the post count if you want to.

    EDIT: I modified your code below. get_post_types accepts three parameters. You provided the first, the other two are optional. The third parameter $operator defaults to 'and'. You ware asking for a list of post types that are public AND are not built-in type; that will only return you CPTs. Adding the 2nd and 3rd param below asks for posts type that are public OR not built-in.

    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    
    $output = 'names'; // default = 'names'
    $operator = 'or';  // default = 'and'
    
    $post_types = get_post_types( $args, $output, $operator );
    
    foreach ( $post_types as $post_type ) {
    
        echo '<p>' . $post_type . '</p>';
    
       $args = array( 'numberposts' => '1', 'post_type' => $post_type); // replace n with the number of posts
       $recent_posts = wp_get_recent_posts( $args );
    
       foreach( $recent_posts as $recent ){
           echo '<a href="' . get_permalink($recent["ID"]) . '" title="' . $recent["post_title"] . '" >' .   $recent["post_title"] . '</a>';
       }
    
    }
    

    The code above will return:

    Array
        (
            [post] => post
            [page] => page
            [attachment] => attachment
            [my-cpt] => my-cpt
        )
    

    Assuming you don't want to return pages or attachments, but just want the built-in 'post' and your custom post types you can skip the call to get_post_types and just create an array to suit your needs:

    $post_types = array( 'post', 'my-cpt' );
    

    If you don’t always know the name of CPTs (i.e. created by plugins), or you have many CPTs you can call get_post_types just as you were then add 'post' to the $post_types array.

    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    $post_types = get_post_types( $args );
    $post_types['post'] = 'post';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程