How to have random posts only on index page (not category, author…)?
-
[ Moderator note: bbcode removed. It doesn’t work in these forums. ]
At first, thanks for great theme ??
I’m working on an interesting online image-dictionary project ([url]https://phut59.com[/url]), and I want to have random word on the index page. After a few searches I did it by adding a php code in function bfa_loop() in template_tags.php to query random posts.
function bfa_loop( $postformat = 'postformat' ) { query_posts($query_string . '&orderby=rand'); <!-- here is the code i added --> if( have_posts() ) : ?> <div class="post-list"> <?php while (have_posts()) : the_post(); ?> <?php bfa_get_template_part( $postformat, get_post_format() ); ?> <?php endwhile; ?> </div> ...
And it worked, I have random posts on the index page, but it does also to the category and author page too. After that, I figured out the category and author page use the same main template index.php. So I guess that to have normal posts in category (or author page) I must create a new main template “category.php” then make a another loop function like this in template_tags.php
function bfa_loop_norandom( $postformat = 'postformat' ) { if( have_posts() ) : ?> <div class="post-list"> <?php while (have_posts()) : the_post(); ?> <?php bfa_get_template_part( $postformat, get_post_format() ); ?> <?php endwhile; ?> </div> ...
Then call it in the category.php main template.
<!--<div id="container">--> <?php get_header(); ?> <div id="main" class="row"> <div id="content" class="cf col8"> <?php bfa_loop_norandom( 'postformat' ); ?> </div> <div id="widgetarea-one" class="col4"> <?php dynamic_sidebar( 'Widget Area ONE' ); ?> </div> </div> <?php get_footer(); ?> <!--</div>-->
[You need to use the code buttons right above the posting box – not make your own – your code may have been damaged as is]
But it did not work, there was nothing on category page (for example https://phut59.com/category/tu-sieu-de/) . It seemed the new function I added not work.
I wonder if someone can help.
Thanks in advance ??
- The topic ‘How to have random posts only on index page (not category, author…)?’ is closed to new replies.