Forums

Question for PRO, about the loop? (8 posts)

  1. taghaboy
    Member
    Posted 1 year ago #

    hello,
    is there a way to control the loop,
    to show a post randomly,
    but only one time in a day?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    If you want to see one post at random, and change it every day, you could insert an option with the date last shown and the post ID for that date. In your template, check the option date against the current date. If it is different, select a new post and update the option. Then, show the post whose ID is in the option.

    If this is not what you want to do, please explain more.

  3. taghaboy
    Member
    Posted 1 year ago #

    First of all, thanks for your reply.
    i think you are near to my idea, can you pls to provide any code to touch your example?
    thanks

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with something like this:

    <?php
      $now = date('Y-m-d');
      $checkdate = get_option( 'random-post-date');
    
      if($checkdate == ''){
        $args = array( 'numberposts' => 1, 'orderby' => 'rand' );
      } else {
        if($checkdate['date'] != $now) {
          $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => array($checkdate['ID']));
        } else {
          $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'p' => $checkdate['ID']);
        }
      }
    
      $random_post = get_posts( $args );
      $date_options['ID'] = $random_post[0]->ID;
      $date_options['date'] = $now;
      if($checkdate == '' || $checkdate['date'] != $now){
        update_option( 'random-post-date', $date_options );
      }
      foreach( $random_post as $post ) : setup_postdata($post); ?>
        <!-- your loop here -->
      <?php endforeach; ?>
  5. taghaboy
    Member
    Posted 1 year ago #

    thanks for your reply,
    i'll test it now.

  6. taghaboy
    Member
    Posted 1 year ago #

    Hi again,

    the code work, thanks, i have just one last question :
    how can i use your code to show posts from specific category, like category_name=XY ?

    Thanks

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    change this:

    if($checkdate == ''){
        $args = array( 'numberposts' => 1, 'orderby' => 'rand' );
      } else {
        if($checkdate['date'] != $now) {
          $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => array($checkdate['ID']));
        } else {
          $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'p' => $checkdate['ID']);
        }
      }

    to this (not tested):

    if($checkdate == ''){
        $args = array( 'numberposts' => 1, 'category_name' => 'XY', 'orderby' => 'rand' );
      } else {
        if($checkdate['date'] != $now) {
          $args = array( 'numberposts' => 1 , 'category_name' => 'XY', 'orderby' => 'rand', 'post__not_in' => array($checkdate['ID']));
        } else {
          $args = array( 'numberposts' => 1, , 'category_name' => 'XY', 'p' => $checkdate['ID']);
        }
      }
  8. taghaboy
    Member
    Posted 1 year ago #

    Thanks i will test it.

Topic Closed

This topic has been closed to new replies.

About this Topic