WordPress.org

Support

Support » How-To and Troubleshooting » Question for PRO, about the loop?

Question for PRO, about the loop?

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

    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

    Moderator keesiemeijer

    @keesiemeijer

    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; ?>

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

    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

    Moderator keesiemeijer

    @keesiemeijer

    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']);
        }
      }

    Thanks i will test it.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Question for PRO, about the loop?’ is closed to new replies.
Skip to toolbar