我想在一个帖子类型中循环浏览现有的帖子,按标题显示每天一个帖子,当达到最后一个帖子时重新开始

时间:2020-12-18 作者:Matt Reeves

有没有办法每天在主页上发布一篇自定义帖子,按字母顺序循环浏览大约20篇帖子中的每一篇,然后从第一篇帖子重新开始?

我想这里找到的一个类似问题的答案可能是我需要走的方向,但我不完全确定:https://wordpress.stackexchange.com/a/325554/199183

这是我从上述答案中借用的自定义函数:

add_action( \'wp\', function () {
    if (! wp_next_scheduled ( \'mark_posts_as_featured_event\' )) {
        wp_schedule_event(time(), \'daily\', \'mark_posts_as_featured_event\');
    }
} );

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( \'sticky_posts\' );
        $old_featured_posts = get_posts( array(
            \'post_type\' => \'member\',
            \'fields\' => \'ids\',
            \'post__in\' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_post_ids as $post_id ) {
            unstick_post( $post_id );
        }
    }

    // stick new posts
    // get_random_posts
    $new_featured_post_ids = get_posts( array(
        \'post_type\' => \'member\',
        \'posts_per_page\' => 1,
        \'orderby\' => \'title\',
        \'order\' => \'DESC\',
        \'fields\' => \'ids\',
    ) );

    foreach ( $new_featured_post_ids as $post_id ) {
        stick_post( $post_id );
    } 

add_action( \'mark_posts_as_featured_event\', \'mark_posts_as_featured_event_callback\' );
我的问题是:

<?php 
$stickies = get_option( \'sticky_posts\' );
// Make sure we have stickies to avoid unexpected output
if ( $stickies ) {
    $args = [
        \'post_type\'           => \'member\',
        \'post__in\'            => $stickies,
        \'posts_per_page\'      => 1,
        \'ignore_sticky_posts\' => 1
    ];
    $the_query = new WP_Query($args);

    if ( $the_query->have_posts() ) { 
        while ( $the_query->have_posts() ) { 
            $the_query->the_post(); ?>

                   <div class="daily-member-header">
          <div class="featured-member-title">
              <div class="featured-label">Featured Member of the Day</div>
              <div class="featured-member-name"><?php the_title(); ?></div>
              <div class="featured-member-business"><?php the_field( \'business\' ); ?></div>
              <div class=arrowcircle-cont"><img class="arrowcircle" src="/wp-content/uploads/2020/12/down-arrow.png" /></div>
          </div>
       </div>
        <div class="daily-member-logo"><img src="<?php the_field( \'logo\' ); ?>" /></div>
       <div class="daily-member-content">
          <div class="daily-member-summary"><?php the_field( \'summary\'); ?></div>
          <div class="daily-member-photo"><img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), \'medium\' ); ?>" /></div>
       </div>
      <style>
         .daily-member-header{
         background:url(\'<?php the_field( \'background-img\'); ?>\');
         }
      </style>

       <?php  }    
        wp_reset_postdata();    
    }
} ?>

1 个回复
SO网友:Matt Reeves

总而言之,我正在创建一个自定义WP Schedule事件,每天检查一次,看看我的;“成员”;自定义帖子类型的帖子被标记为粘滞,如果是,则将其从粘滞帖子选项表中删除,然后随机分配另一个成员帖子作为粘滞帖子。(我在主页上通过一个短代码显示这篇随机帖子,这里没有显示短代码)。

我想我在这方面取得了一些进展。我的代码中有一些错误,我在下面强调了这些错误,例如变量名称错误和缺少开头if语句。

我不得不放弃按字母顺序显示这些帖子,因为我不知道如何编码,所以我决定按随机顺序。我在orderby rand中添加了php会话功能,这样每个网站访问者就不会看到同一篇文章两次,尽管我不能完全确定这是否可行。

以下是我在本帖帮助下的查询:https://wordpress.stackexchange.com/a/217080/199183

    <?php 


$stickies = get_option( \'sticky_posts\' );
// Make sure we have stickies to avoid unexpected output
if ( $stickies ) {
    $args = [
        \'post_type\'           => \'member\',
        \'post__in\'            => $stickies,
        \'posts_per_page\'      => 1,
        \'ignore_sticky_posts\' => 1
    ];
    $the_query = new WP_Query($args);

    if ( $the_query->have_posts() ) { 
        while ( $the_query->have_posts() ) { 
            $the_query->the_post(); ?>

                   <div class="daily-member-header">
          <div class="featured-member-title">
              <div class="featured-label">Featured Member of the Day</div>
              <div class="featured-member-name"><?php the_title(); ?></div>
              <div class="featured-member-business"><?php the_field( \'business\' ); ?></div>
              <div class=arrowcircle-cont"><img class="arrowcircle" src="/wp-content/uploads/2020/12/down-arrow.png" /></div>
          </div>
       </div>
        <div class="daily-member-logo"><img src="<?php the_field( \'logo\' ); ?>" /></div>
       <div class="daily-member-content">
          <div class="daily-member-summary"><?php the_field( \'summary\'); ?></div>
          <div class="daily-member-photo"><img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), \'medium\' ); ?>" /></div>
       </div>
      <style>
         .daily-member-header{
         background:url(\'<?php the_field( \'background-img\'); ?>\');
         }
      </style>

       <?php  }    
        wp_reset_postdata();    
    }
} ?>
以下是本帖帮助下的完整功能-https://wordpress.stackexchange.com/a/325554/199183 (对于计划功能和本页-https://love-coding.pl/en/wordpress-how-to-display-random-posts-without-duplicates/ (对于会话功能):

function prefix_start_session() {
    if( !session_id() ) {
        session_start();
    }
}
add_action( \'init\', \'prefix_start_session\' );

function get_random_post() {
    if ( !isset( $_SESSION[\'random\'] ) ) {
        $_SESSION[\'random\'] = rand();
    }
    return $_SESSION[\'random\'];
}

function wi_custom_cron_schedule( $schedules ) {
    $schedules[\'5min\'] = array(
        \'interval\' => 5, // Every 5 minutes
        \'display\'  => __( \'Every 5 Minutes\' ),
    );
    return $schedules;
}
add_filter( \'cron_schedules\', \'wi_custom_cron_schedule\' );

    if (! wp_next_scheduled ( \'mark_posts_as_featured_event\' )) {
        wp_schedule_event(time(), \'5min\', \'mark_posts_as_featured_event\');
    }

add_action( \'mark_posts_as_featured_event\', \'mark_posts_as_featured_event_callback\' );

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( \'sticky_posts\' );
    if ( ! empty($sticked_post_ids) ) { 
        $old_featured_posts = get_posts( array(
            \'post_type\' => \'member\',
            \'fields\' => \'ids\',
            \'post__in\' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_posts as $post_id ) {
            unstick_post( $post_id );
        }
    }

    // stick new posts
    // get_random_posts
    $new_featured_posts = get_posts( array(
        \'post_type\' => \'member\',
        \'posts_per_page\' => 1,
        \'orderby\' => \'rand(\' . get_random_post() . \')\',
        \'fields\' => \'ids\',
    ) );

    foreach ( $new_featured_posts as $post_id ) {
        stick_post( $post_id );
    } 
}

Errors fixed

在前面的代码中,我意识到我需要更改:

foreach ( $old_featured_post_ids as $post_id ) {
致:

foreach ( $old_featured_posts as $post_id ) {
还意识到我错过了开场白if语句。

旧版本:

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( \'sticky_posts\' );
        $old_featured_posts = get_posts( array(
            \'post_type\' => \'member\',
            \'fields\' => \'ids\',
            \'post__in\' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_post_ids as $post_id ) {
            unstick_post( $post_id );
        }
    }
新建:

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( \'sticky_posts\' );
    if ( ! empty($sticked_post_ids) ) { 
        $old_featured_posts = get_posts( array(
            \'post_type\' => \'member\',
            \'fields\' => \'ids\',
            \'post__in\' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_posts as $post_id ) {
            unstick_post( $post_id );
        }
    }
foreach ( $old_featured_post_ids as $post_id ) {
 并更改了“orderby-=>;”rand\',至“orderby”=>;\'rand(\'.get\\u random\\u post().\')\',使用新会话代码。