0

i have a question. I want to display on my homepage (template) the last postings of 5 categories as excerpts. Means excerpts from last article of cat.1, last article of cat.2 and so on, so i got then 5 articles (excerpts), each one from a differend category i.e. cat. 1-5.;

How can i make this "simply"?

EDIT: I did it so:

<?php
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => '5', );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 
    <ul class="recent-posts-2">
        <div class="cat"><a href="<?php echo get_page_link(149); ?>">my 1. category</a></div>
        <li><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2></li> 
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array( 120,120 ), array( 'class' => 'recent-thumbs' )); ?></a> 
    </ul>
<?php endforeach; ?>

<?php
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => '7', );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 
    <ul class="recent-posts-2">
        <div class="cat"><a href="<?php echo get_page_link(151); ?>">my 2. category</a></div>
        <li><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2></li> 
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array( 120,120 ), array( 'class' => 'recent-thumbs' )); ?></a> 
    </ul>
<?php endforeach; ?>

Dis example shows 2 excerpts of 2 cats, and i have to repeat it until i get my 5 articles. I thougt, maybe is there a better way.

Thanks, and sorry for my "great" englisch, -Orwell

  • What have you tried? Please edit your question to include your current code. – Chip Bennett Nov 6 '13 at 18:56
  • Thanks for the edit. Can you clarify what specifically the problem/issue is? Is it merely a question of better/more efficient coding method, or something more? – Chip Bennett Nov 6 '13 at 19:09
  • Yes, it works fine, but it seems its unnecessary code in the file, - so my question is only for more efficience and a cleaner better code if its possible. – Orwell Nov 6 '13 at 19:12
0

There are ways to write the code more efficiently, though not necessarily WordPress-specific.

  1. Only pass necessary args to your get_posts() calls

    The only critical args you need are posts_per_page and category; you can omit the rest

  2. Make an array of category IDs, and loop through the array

    Loop through an array of category IDs, so you only have to use the actual loop output once

For example:

<?php
// Add category IDs here
$category_ids = array( 5, 7 );

// Loop through category IDs
foreach ( $category_ids as $cat_id ) {

    // Category query args
    $cat_query_args = array( 'posts_per_page' => 1, 'category' => $cat_id );
    // Query posts
    $cat_query = get_posts( $cat_query_args );
    // Loop through cat query
    foreach ($cat_query as $post ) {  
        setup_postdata( $post ); 
        ?> 
        <ul class="recent-posts-2">
            <div class="cat"><a href="<?php echo get_page_link(149); ?>">my 1. category</a></div>
            <li><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2></li> 
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array( 120,120 ), array( 'class' => 'recent-thumbs' )); ?></a> 
        </ul>
    }
}
?>

You may need to add in some other things, such as a counter for class="recent-posts-1", etc.

  • Thanks alot. Im new in php and will try it out. Looks very good and saves lines. :) – Orwell Nov 6 '13 at 19:24
  • Did it so, but now i need the page links for each cat. I have for each cat a page. The <?php echo get_page_link(); ?> dont work as i need it. Have i to make a new array to assign the page links to the categorys? Thanks :) – Orwell Nov 13 '13 at 10:10
  • Please edit your question to add that additional question, along with relevant information to help answer it. How are pages associated with categories? – Chip Bennett Nov 13 '13 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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