WordPress.org

Ready to get started?Download WordPress

Forums

Last post from each category in home page (4 posts)

  1. thatsdjmk
    Member
    Posted 3 years ago #

    Hello, i would like to show at my home page one article from every category and also the title of the second article of every category. I Hope you understand what i mean. You can also see a theme that uses exatcly what i need.

    Here is the demo of Portal theme: http://www.freshthemes.com/demo/portal/

    i'm creating a new portal so i would like the news to be shown that way.

    Thank you very much and congrats about wordpress, the best blog(+more) script ever made!!

    Ps. Found several post at the forum but non was a full respond..

  2. MichaelH
    Member
    Posted 3 years ago #

    <?php
    //get all terms (e.g. categories or post tags), then display 2 posts (1st post displays the_content) in each term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'order' => 'ASC',
          'orderby' => 'title',
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 2,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          $count = 0;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
           $count ++;
           if ($count == 1) {
             the_content();
           }
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. thatsdjmk
    Member
    Posted 3 years ago #

    thank you very much. as i can see that's exactly what i was looking for.

    the problem now is how i'm gonna make it look like my theme..

  4. zandercent
    Member
    Posted 3 years ago #

    could you make this work to only display children of the current category - or a specific category?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags