WordPress.org

Forums

Display random posts (9 posts)

  1. Imon Hasan
    Member
    Posted 1 year ago #

    <?php query_posts('orderby=rand'); ?>

    when i add this code in my theme . i see every post atomically duplicated and show so many post .

    pls help

  2. Gregor
    Member
    Posted 1 year ago #

    Hi imon,

    How do you want to change what's displayed?

    Do you want a certain number of posts, posts only from one category, posts with a certain tag...?

    Thanks

    VW

  3. Imon Hasan
    Member
    Posted 1 year ago #

    no i am not changing any thing .

    when i add the code then i have too many post displayed- duplicated posts.

    for example if i have 12 post . when i add this code it displayed 24 post .

    thanks

  4. Gregor
    Member
    Posted 1 year ago #

    Understood, can you post the entire code of the file containing the query_posts as described above?

  5. Imon Hasan
    Member
    Posted 1 year ago #

    Yas !!

  6. The question really is where are you putting it and as VaderW asked, what is the code. If more than 10 lines use a gist, or pastebin. :)

  7. Imon Hasan
    Member
    Posted 1 year ago #

    this is my code

    <?php if(of_get_option('frontcat_checkbox') == "1"){ ?>
    <?php if(is_front_page()) {
    	 $args = array(
    				   'cat' => ''.$os_front = of_get_option('front_cat').'',
    				   'post_type' => 'post',
    				   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    				   'posts_per_page' => ''.$os_fonts = of_get_option('frontnum_select').'');
    	query_posts($args);
    } ?>
    <?php }?>
    
                       <?php query_posts('orderby=rand'); ?>
    
    				   <?php
    
    				    if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
                    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
  8. Gregor
    Member
    Posted 1 year ago #

    Looks like when the if statements are true, you query_posts twice, duplicating the posts. To get around this move the orderby=rand into the $args array, adjust the second query_posts() to use this array in it's query, and remove the first query_posts() completely.

    So try...

    <?php if(of_get_option('frontcat_checkbox') == "1"){ ?>
    <?php if(is_front_page()) {
    // This sets the values of $args if the above both return true
    	 $args = array(
    				   'cat' => ''.$os_front = of_get_option('front_cat').'',
    				   'post_type' => 'post',
    				   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    				   'posts_per_page' => ''.$os_fonts = of_get_option('frontnum_select').'',
    				   'orderby' => 'rand');
    } ?>
    <?php }?>
    
     <?php
    // query_posts() occurs once, returning one set of posts matching the values of $args
    query_posts($args); ?>
    
    <?php
    if(have_posts()):
    // If we have the posts, display them ?>
    <?php while(have_posts()): ?>
    <?php the_post(); ?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
  9. Imon Hasan
    Member
    Posted 1 year ago #

    thanks

    but its not working result is same

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.