WordPress.org

Support

Support » Themes and Templates » [Resolved] How to have random posts only on index page (not category, author…)?

[Resolved] 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]http://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 http://phut59.com/category/tu-sieu-de/) . It seemed the new function I added not work.

    I wonder if someone can help.

    Thanks in advance 🙂

Viewing 1 replies (of 1 total)
  • Oh I’ve just found the solution, I added the new function bfa_loop_norandom to the get_whitelist.php, and that help! 😀

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] How to have random posts only on index page (not category, author…)?’ is closed to new replies.