0

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]

2 Answers 2

0

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]
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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