0

I'm working on a website where the client wants a static page displaying information about a certain category. Then under the static information she wants all the posts from that category in a 2 column list.

So I need to have an alphabetically ordered list linking to all the post in a selected category. I have the code to do this, but I need to be able to break the list up in columns. Here is the code I have displaying the post...

<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
  $cat = get_cat_ID($category);
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $post_per_page = -1; // -1 shows all posts
  $do_not_show_stickies = 1; // 0 to show stickies
  $args=array(
    'category__in' => array($cat),
    'orderby' => 'title',
    'order' => 'asc',
    'paged' => $paged,
    'posts_per_page' => $post_per_page,
    'caller_get_posts' => $do_not_show_stickies
  );
  $temp = $wp_query;  // assign orginal query to temp variable for later use   
  $wp_query = null;
  $wp_query = new WP_Query($args); 
  if( have_posts() ) : 
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <_li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to          <?php the_title_attribute(); ?>"><?php the_title(); ?></a><_/li>

I know my HTML skills aren't the best, but here is the entire template page...

https://gist.github.com/889340

1
  • 1
    Looks like the HTML in your example is a bit mangle: id="post"> is off by itself. Can you update your question to show the full code for your HTML generation? Mar 27, 2011 at 15:44

3 Answers 3

0

Updated answer:
Use two floated lists to emulate columns, same approach as previously though.

<?php
/*
Template Name: PageOfPosts
*/

get_header(); ?>

<div id="content">
    <div class="t">
        <div class="b">
            <div class="l">
                <div class="r">
                    <div class="bl">
                        <div class="br">
                            <div class="tl">
                                <div class="tr">
                                    <div class="pad">

                                        <?php while( have_posts() ) : the_post(); ?>

                                        <?php 
                                        $category = get_post_meta( get_the_ID(), 'category', true); 
                                        $img_style = 'style="width:606;height:34;position: absolute; padding-top:1px; padding-left: 2px; z-index:9999; background: no-repeat;"';
                                        ?>

                                        <div class="post" id="post-<?php the_ID(); ?>">

                                            <h1><?php the_title(); ?></h1>
                                            <img src="../wp-content/themes/DD4L/images/leafhr.png" <?php? echo $img_style;?>>
                                            <br />

                                            <div class="entry">
                                                <?php the_content(); ?>
                                                <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
                                            </div>

                                            <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>

                                        </div>

                                        <?php endwhile; ?>

                                        <br /><br />

                                        <?php if( !empty( $category ) ) : ?>

                                        <?php
                                        $args = array(
                                            'orderby' => 'title',
                                            'order' => 'asc',
                                            'nopaging' => true,
                                            'ignore_sticky_posts' => true,
                                            'tax_query' => array(
                                                array(
                                                    'taxonomy' => 'category',
                                                    'terms' => array( $category ),
                                                    'field' => 'slug'
                                                )
                                            )
                                        );
                                        $category_query = new WP_Query( $args );
                                        ?>

                                        <?php if( $category_query->have_posts() ) :  ?>

                                        <?php
                                        $total = $category_query->post_count;
                                        $quart = $total / 4;

                                        if( floor( $quart ) != $quart )
                                            $quart = ceil( $quart );
                                        $counter = 0;
                                        ?>

                                        <div class="float-container">
                                            <ul class="alignleft">
                                            <?php while( $category_query->have_posts() ) : $category_query->the_post(); $counter++; ?>

                                                <li>
                                                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                                                </li>

                                            <?php if( $quart == $counter ) : $counter = 0; ?>
                                            </ul>

                                            <ul class="alignleft">
                                            <?php endif; ?>

                                            <?php endwhile; ?>
                                            </ul>

                                            <div class="clear"></div>
                                        </div>

                                        <?php endif; ?>

                                        <?php endif; ?>

                                    </div>
                                    <div class="clear"></div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

See edit revision for previous comments and code.

17
  • @t310s: You are my hero!!! That works great, but there is just one thing. How can have the alphabetical order to be vertical instead of horizontal? Thanks a lot man, this is exactly what I needed. I've looked everywhere for this thing!
    – Daymon
    Mar 28, 2011 at 13:12
  • @Daymon - That's exactly what i was referring to when i mentioned not knowing what structure you had in mind, it will need some adjustment to have results listed vertically instead. I'll update the answer shortly, just gotta have a think about how to refactor the code.. :)
    – t31os
    Mar 28, 2011 at 13:17
  • @Daymon - Answer updated.. Opted for using the table still but using DIVs as virtual table cells. If you need help creating CSS to pretty up the results, let me know.
    – t31os
    Mar 28, 2011 at 13:41
  • @t31os - You're awesome! That listed it in vertical alphabetical order but how do i split into two columns? Could I do 2 separate lists instead of using the DIV tag? Then just float it to the left with whatever width?
    – Daymon
    Mar 28, 2011 at 13:53
  • You should have two columns already, the second half of results should list out inside a second table column, is that not happening?
    – t31os
    Mar 28, 2011 at 14:00
0

Without really looking at your code and just thinking off the top of my head you could set up a new WP-Query for each column.

In your args set 'posts_per_page' => 10, or however many post permalinks you want to show in each column. Assign a ul or div class to the columns and float them left in your css.

0

You could also use the List Category plugin which will let you insert shortcode into any page or post to show either matching posts by categories or by tags. You can specify order sorting, hide/show excerpts, and much more.

Download: http://wordpress.org/extend/plugins/list-category-posts/

List of full commands: http://foro.picandocodigo.net/discussion/251/list-category-posts-documentation/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.