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 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题