hi i am new to wordpress. so i am getting lot of problems
I am trying to display posts of specific category using the following code

$posts_array = get_posts( $args );
foreach($posts_array as $posting)
echo $posting;

But i am getting errors instead. Please anyone help me in getting the solution. Thanks in advance

Recommended Answers

All 2 Replies


Do you want to show them outside of the wordpress universe?, or are trying to create your own plugins?

Using wordpress docs as referenced here, we can easily device simple codes to retrieve and sort blog posts.

Example codes below is outside the wordpress. If you will be using it creating a plugin, the approach and coding techniques is slightly different.


global $post;

$cat = $_GET['cat'];
$how_many_post = 5; //change the number if you want more

$args = array( 'posts_per_page' => $how_many_post, 'category' => $cat);
$posts_array = get_posts( $args );

foreach($posts_array as $posting) : setup_postdata( $post );
  echo '<a href="'. the_permalink() .'">'. the_title() .'</a>';

it should work, but I have never had the chance to test it...



change this

foreach($posts_array as $posting) : setup_postdata( $post );

to this

foreach($posts_array as $post) : setup_postdata( $post );

Sorry for the typos.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.