Forums

Automatically create columns of categories? (11 posts)

  1. arcab4
    Member
    Posted 4 years ago #

    Hello,

    i figured out how to list post titles of a specific category via this:

    <?php query_posts('cat=12'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <span class="listings"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>"><?php the_title(); ?></a></span>
    <?php endwhile;?>

    so this will list all the posts in one column. i can't seem to figure out how to put the posts in columns if there are too many.

    for example, like if i have 20 posts in one column, and what i want is 10posts in each column.

    any help would be appreciated and happy thanksgiving!

  2. arcab4
    Member
    Posted 4 years ago #

    update - actually when i post that code above, it spits out the title and then the content? how do i get it to only show the title tags?

  3. arcab4
    Member
    Posted 4 years ago #

    update 2: if i add another statement below it, it stops showing the content and shows only the title in category 53.

    <?php query_posts('cat=53'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <span class="listings"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>"><?php the_title(); ?></a></span>
    <?php endwhile;?>
    
    <?php query_posts('cat=12'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <span class="listings"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>"><?php the_title(); ?></a></span>
    <?php endwhile;?>

    odd?

  4. Kafkaesqui
    Moderator
    Posted 4 years ago #

    As I often point out, query_posts() is a powerful tool, but it is not one designed for multiple loop management. For that, see the other options available:

    http://codex.wordpress.org/The_Loop#Multiple_Loops

    In any case, here is one way you can lay out a single posts query into 2 (or more) columns:

    <?php query_posts('cat=12'); ?>
    <div class="column-1">
    <?php while (have_posts()) : the_post(); ?>
    <?php if( $post == $posts[10] ) : ?>
    </div><div class="column-2">
    <?php endif; ?>
    <span class="listings"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?>"><?php the_title(); ?></a></span>
    <?php endwhile;?>
    </div>

    Note how I test the $post object against $posts[10] -- in programmer's math 10 here would the 11th post ($posts[0] is the 1st) -- to decide when to end the first column div and open the second one.

  5. arcab4
    Member
    Posted 4 years ago #

    thanks for the information. that helped alot. i ran it and my results showed up in div column-2. column-1 was blank. i currently have as a test 6 posts so i changed $post[10] to a smaller digit like $post[2].

    i'll continue to see if i can figure it out but if you have any suggestions, i would appreciate it.

  6. Kafkaesqui
    Moderator
    Posted 4 years ago #

    If you always want 2 'even' columns no matter the # of posts, you can first evaluate $posts to find out how many posts reside in it, then roll out your 2nd column from that:

    <?php
    $split = ceil( count($posts) / 2 );
    if( $post == $posts[$split] ) :
    ?>
    </div><div class="column-2">

    count($posts) just counts the number of objects (posts) in $posts, so $split is passed the (rounded up) value of this divided by 2.

  7. travelvice
    Member
    Posted 4 years ago #

    Howdy,

    I like the direction this took, and would love to see an adaptation of it in this way:

    On the index page, an equal two-column listing of all top-level categories (that aren't empty, or have children that aren't empty).

    Any thoughts?

  8. Kafkaesqui
    Moderator
    Posted 4 years ago #

    travelvice, my only thought this early in is that your request has less to do with setting ones posts into a columnar layout and more about how one would (or could!) go about designing a very unusual custom query of posts.

    I think it'd be best to start a new thread on it, though no guarantees a good solution will come about -- other than the standard one of using separate queries for each category.

  9. travelvice
    Member
    Posted 4 years ago #

    Thanks for your thoughts, Kafkaesqui

  10. tommy2toes
    Member
    Posted 4 years ago #

    Thanks Kafkaesqui.

    Just what I was looking for!

  11. JohnSheridan
    Member
    Posted 3 years ago #

    I was looking to do the same thing, and implemented something even simpler. I just put a new div around each post. The overall content div is the higher level container. Works a treat:

    <!-- Display the Title as a link to the Post's permalink. -->
    <div class="embedpost">
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php the_tags(); ?></p>
     <!-- Display the Time. -->
     <small><?php the_time('F jS, Y'); ?></small>
    </div>

    and the css:

    .embedpost {
    width:48%;
    float:left;
    padding:0 0 0 10px;
    margin:0 0 20px 0;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic