Reputation: 3484
I am really a newbie in Wordpress and php. I created a theme for my website as follows:
I think my question is clear: in wordpress admin page I have four categories: 1,2,3,4. I want last 2 posts in category 1 to be displayed in section (div) 1 (shown above), last 2 posts in category 2 to be displayed in section (div) 2, and so on.
As I said, I am novice and I faced a tons of functions in Wordpress documentation.
for example I used the code below inside index.php (of my custom theme) in section 2 which outputs: "Sorry, no posts matched your criteria."
<?php
$args = array( 'post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها');
$the_query = new WP_Query( $args );
?>
<?php
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
?>
Please give a piece of code to do this.
Upvotes: 0
Views: 46
Reputation: 12469
This should work:
$posts = get_posts ("cat=1&showposts=2");
if ($posts)
{
foreach ($posts as $post):
setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php endforeach;
}
Change cat=1
to the id of each of the four categories.
Upvotes: 2