• Resolved psntr

    (@psntr)


    Dear wordpress community,

    I’m digging a bit through WordPress for few weeks now, trying to build my own theme. While trying to solve this issue, I bump on severals solution that might work, but I want to do it in the cleanest way possible.

    Basically I modifying my index.php, and now it has 3 columns and right after it a 4 columns, what I try to achieve seems quite easy… But, how to assign each columns a specific category and displaying only 1 recent post?

    <div class="my-first-column">
    query_posts(array( 'posts_per_page' => 1, 'category' => 1))
    // loop
    while(have_posts()) {
      the_post();
      the_title(); // your custom-post-type post's title
      the_content(); // // your custom-post-type post's content
    }
    wp_reset_query() // reset the query
    </div>
    
    <div class="my-second-column">
    query_posts(array( 'posts_per_page' => 1, 'category' => 2))
    // loop
    while(have_posts()) {
      the_post();
      the_title(); // your custom-post-type post's title
      the_content(); // // your custom-post-type post's content
    }
    wp_reset_query() // reset the query
    </div>
    
    <div class="my-third-column">
    query_posts(array( 'posts_per_page' => 1, 'category' => 3))
    // loop
    while(have_posts()) {
      the_post();
      the_title(); // your custom-post-type post's title
      the_content(); // // your custom-post-type post's content
    }
    wp_reset_query() // reset the query
    </div>

    and so on and so on. But then it seems a bit repetitive and maybe there would been a more reliable solution for that…

    Thank you for your time.

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php
    
    $cat_args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );
    $categories = get_categories($cat_args);
    $i = 0;
    
    foreach ($categories as $category) {
        $args = array(
            'showposts' => 1,
            'category__in' => array($category->term_id),
            'caller_get_posts' => 1
        );
        $posts = get_posts($args);
        if ($posts) {
            foreach ($posts as $post) {
                ?>
                <div class="my-<?php echo $i; ?>-column"> // to change classes
                    <?php
                    setup_postdata($post);
                    the_title(); // your custom-post-type post's title
                    the_content(); // // your custom-post-type post's content
                    ?>
                </div>
            <?php
            } // foreach - $posts
        } // if
        $i++;
    } // foreach
    ?>
    Thread Starter psntr

    (@psntr)

    Thank that’s solution that is clever, I did it and it worked.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assigning a specific class per category and show its last post’ is closed to new replies.