Forums

How to display multiple or different category in one page (6 posts)

  1. andythology
    Member
    Posted 3 years ago #

    I would like to build a portal with wordpress and i have been reading and yet to find out how can i display different category in one page, ie the main page.
    for instance, the site is similar of what i am trying to achieve: http://www.chc.org.sg/citynews/

    thank you very much.

  2. mores
    Member
    Posted 3 years ago #

    Get the theme Revolution Pro 30 :)

    What you want is a loop through all available Categories, then display X number of posts in each of that category. The magic word is "multiple loops", and I have recently gotten to grips with it :)
    (for my site and another one )

    This is my code:

    <?php
    $flip=0;
    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    foreach ($categories as $category) { ?>
    <div id="col_<?php echo $flip; ?>">
    	<div id="col_headline"><a href="<?php echo get_category_link($category->id); ?>">Kategorie: <?php echo $category->name; ?></a></div>
    	<?php if (in_array($category->id,$mycatids)) {
    			query_posts('category_name='.$category->name.'&showposts=5&offset=1');
    		} else {
    			query_posts('category_name='.$category->name.'&showposts=5');
    		} ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<div id="col_post" class="accordion_toggle_<?php echo $flip; ?>"><?php the_title(); ?></div>
    		<div id="col_header_pic" class="accordion_content_<?php echo $flip; ?>"><a href="<?php the_permalink(); ?>"><img src="<?php easypermgals_header_pure(); ?>" width=315 border=0></a></div>
    
    	<?php endwhile; else: ?><p>Nix!</p><?php endif; ?>
    <?php $flip++; ?>
    </div>
    <?php }	?>

    The "flip" variable is so that I can have individually numbered DIVs that are called col_1, col_2, and so on. I can then style and position those with CSS.
    Hope it helps.

  3. tprod
    Member
    Posted 3 years ago #

    Hi and thanks for this solution,

    However, I have a problem when I copy this script with my wordpress version (2.6.x). Indeed, I create a file named 'home.php' and this is the script I use. Tables have changed. Can anybody have an update of the script ?

    Best regards

  4. mores
    Member
    Posted 3 years ago #

    "update"?

    What is the error message, what happens when you run the page, what do tables have to do with anything? This code works on 2.6.2!

  5. tprod
    Member
    Posted 3 years ago #

    I have a blank page. I'm sure that the file home.php is included but no results. Table names are in my version :

    wp_terms
    wp_term_relationships
    wp_term_taxonomy

    Even if I do a print_r($categories) there's no content.

    Do you have an idea ? Do you know what I'm doing wrong ?

    Here is an print screen -> The problem

  6. mores
    Member
    Posted 3 years ago #

    Ah ... I forgot something :)

    The script won't work, since there is a check if the featured article's category is being listed, which is not what you need. Corrected code:

    <?php
    $flip=0;
    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    foreach ($categories as $category) { ?>
    <div id="col_<?php echo $flip; ?>">
    	<div id="col_headline"><a href="<?php echo get_category_link($category->id); ?>">Kategorie: <?php echo $category->name; ?></a></div>
    	<?php query_posts('category_name='.$category->name.'&showposts=5'); } ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<div id="col_post" class="accordion_toggle_<?php echo $flip; ?>"><?php the_title(); ?></div>
    		<div id="col_header_pic" class="accordion_content_<?php echo $flip; ?>"><a href="<?php the_permalink(); ?>"><img src="<?php easypermgals_header_pure(); ?>" width=315 border=0></a></div>
    
    	<?php endwhile; else: ?><p>Nix!</p><?php endif; ?>
    <?php $flip++; ?>
    </div>
    <?php }	?>

Topic Closed

This topic has been closed to new replies.

About this Topic