【问题标题】:Randomize Wordpress Publish Time Or Random Posts Once A Day每天一次随机化 Wordpress 发布时间或随机帖子
【发布时间】:2013-01-11 13:47:56
【问题描述】:

我确实为此做了搜索作业,但我仍然没有解决方案。

我有一个基于 wordpress 引擎的视频管网站,它显示 12 个视频缩略图(帖子)/页面。 但它出现在发布时间顺序中(与 wordpress 一样),但在这个站点中它不是那么好。我希望帖子每天随机一次(随机发布日期)。

我可以整合

query_posts($query_string . '&orderby=rand');

但这只是解决方案的一半,因为这会在每个页面加载和每个页面上随机发布帖子,因此有可能在第 1 页和第 2 页上出现相同的视频。

所以我需要一个解决方案,让整个网站上的帖子一次随机化,每天一次。

最好的方法是以某种方式将每个帖子的发布时间的发布日期打乱。

我不敢相信没有插件可以做到这一点:) 感谢转发。

【问题讨论】:

    标签: wordpress posts


    【解决方案1】:

    所以这里有一个随机发布日期的小脚本。

    $date_format = 'Y-m-d H:i:s';
    $date1       = date( $date_format, strtotime("-1 year", time()) );
    $date2       = date( $date_format, time() );
    $post_type   = 'post'; // post, page, product or some other custom post type
    
    // Get all the posts
    $posts = get_posts( array( 
            'numberposts' => -1, // get ALL posts
            'post_status' => 'any', // published, pending, draft, future, trash
            'post_type'   => $post_type 
      ));
    
    foreach( $posts as $post ) {
    
      //* Generate a random post dates
      $random_date = mt_rand( strtotime( $date1 ), strtotime($date2) );
    
      //* Format the date that WordPress likes
      $post_date = date( $date_format, $random_date );
    
      // We only want to update the post date
      $update = array(
            'ID'            => $post->ID,
            'post_date'     => $post_date,
            'post_date_gmt' => null,
      );
    
      //* Update the post
      wp_update_post( $update );
    
    }
    

    Aldo 这个帖子已经很老了,我希望这对将来寻找这个答案的人有所帮助。

    【讨论】:

      【解决方案2】:

      由于您假设您的查询是随机的,因此 Wordpress 无法避免在不同页面中显示相同的内容。

      【讨论】:

      • 是的,我知道,这就是为什么所谓的(我将其命名为 :) 发布日期洗牌器将是一种很好的技术,这是一个将每个帖子的发布日期更改为相对随机日期的脚本,因此每次运行低谷时帖子都会混合
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多