Many people want to show wordpress show latest post from each category posts from there selected categories or all categories on home page. Here we given code snippet for this.
I this article I will show how easily you can achieve this.
wordpress show latest post from each category
Please following code in your home page which is your theme’s index.php file.
01 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span>--> |
02 | $categories =get_categories( $all_categories ); |
03 | foreach ( $categories as $category ) { |
05 | 'showposts' =--> 1, |
06 | 'category__in' => array ( $category ->term_id), |
07 | 'caller_get_posts' =>1 |
09 | $posts =get_posts( $post_args ); |
12 | Category: sprintf( __( " href=" ' . get_category_link( $category->term_id ) . ' ">name ) . '" ' . '>' . $category ->name.'</pre> |
13 | <pre> '; foreach ( $posts as $post ) {</pre> |
15 | setup_postdata( $post ); ?></pre> |
18 | php the_title_attribute(); ?>" href="<!--?php the_permalink() ?-->" rel="bookmark"> |
19 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span> the_title(); ?--> |
21 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span> the_excerpt(); ?--> |
25 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span>--> |
if you want to show the posts from specific articles then use following code.
01 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span>--> |
02 | $selected_categories = array ( |
03 | 'include' =--> '1,4,9' , |
07 | $categories =get_categories( $selected_categories ); foreach ( $categories as $category ) { |
10 | 'category__in' => array ( $category ->term_id), |
11 | 'caller_get_posts' =>1 |
13 | $posts =get_posts( $post_args ); |
16 | Category: sprintf( __( " href=" ' . get_category_link( $category->term_id ) . ' ">name ) . '" ' . '>' . $category ->name.'</pre> |
17 | <pre> '; foreach ( $posts as $post ) {</pre> |
19 | setup_postdata( $post ); ?></pre> |
22 | php the_title_attribute(); ?>" href="<!--?php the_permalink() ?-->" rel="bookmark"> |
23 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span> the_title(); ?--> |
25 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span> the_excerpt(); ?--> |
29 | <!--?<span class ="hiddenSpellError" pre="" data-mce-bogus="1">php</span>--> |
Very helpful article. I have been looking for a way to do this all my life. Thanks for the codes.
What about if I choose certain categories but I don’t want one of the child-categories of one of these chosen categories?
How do I exclude this category?
I tried to add ‘exclude’ => ’35’, But that does not seem to work.
Any thoughts?