Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using wordpress as my CMS. I am thinking, that the left panel has titles of my "posts" under its relevant category and the right side as the title of the post and its content.

I have tried to put the following code in a page (after enabling a php plugin for wordpress). But since I dont know php, I tried the below, but it does not display pages with category "basics".

[php]
$myposts = get_posts('numberposts=5&category=basics'); 
foreach($myposts as $post) :    
setup_postdata($post);
//add in standard WordPress post specific template tags here eg.   the_title(),     the_permalink, the_content, the_excerpt etc.
endforeach;
[/php]
share|improve this question

2 Answers 2

add in standard WordPress post specific template tags here eg.
the_title(), the_permalink, the_content, the_excerpt etc. endforeach;

Like that comments says, call the function you need to display the title.

The category argument appears to represent the category ID, not the slug. Try using category_name instead.

[php]

$myposts = new WP_Query('posts_per_page=5&category_name=basics'); 
while($myposts->have_posts()):
  $myposts->the_post();

  // your output
  the_title();

endwhile;

wp_reset_query();

[/php]
share|improve this answer

To know what to fix, you need to know what you did wrong. This si what your code does:

  • Grab all the posts in the category with the number: "basics"
  • For each post found, do:
    • Nothing...
  • Finish the for loop

You're not checking if any posts were actually found, you're not cleaning up your query data afterwards either so any loops afterwards won't workm and as One Trick Pony mentioned, you're using a slug not an ID.

So instead:

// use WP_Query isntead as it's clearer, and use category_name
$myposts = WP_Query('numberposts=5&category_name=basics');

// check if we actually have any posts
if($myposts->have_posts()){
    // we do, start the post loop
    while($myposts->have_posts()){
        $my_posts->the_post();

        //add in standard WordPress post specific template tags here eg.
        the_title();
        the_content();

    }
} else {
    // if no posts were found it's a good idea to know about it
    echo 'no posts were found in the basics category';
}

// cleanup the post data
wp_reset_postdata();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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