• 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!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter arcab4

    (@arcab4)

    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?

    Thread Starter arcab4

    (@arcab4)

    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?

    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:

    https://codex.www.ads-software.com/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.

    Thread Starter arcab4

    (@arcab4)

    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.

    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.

    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?

    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.

    Thanks for your thoughts, Kafkaesqui

    Thanks Kafkaesqui.

    Just what I was looking for!

    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;
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Automatically create columns of categories?’ is closed to new replies.