Show wordpress posts from one category only

I am trying to display posts of a certain category to a page showing a thumbnail then linking through to the full posts. This is going to be fore a case study section.

I have the links showing up and carrying me through however it displays ALL posts instead of the specific category like I want.

Read More

Any ideas? I’m not familiar with PHP and only just stated using wordpress

my code:

<?php // PAGE LINK/TITLE

if (is_page()) {
  $cat=get_cat_ID($post->post_title); //use page title to get a category ID
  $posts = get_posts ("cat=$cat&showposts=10");
  if ($posts) {
    foreach ($posts as $post):
      setup_postdata($post); 

 if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail();
} 

?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page

?>

<?php endforeach;
  }
}
?>

Related posts

2 comments

  1. <?php 
     $catPost = get_posts(get_cat_ID("NameOfTheCategory")); //change this
       foreach ($catPost as $post) : setup_postdata($post); ?>
           <div>
                 <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
                 <p><?php the_content(); ?></p>
           </div>
    <?php  endforeach;?>
  2. There is no specific category set, only the amount of posts to be displayed, in this case 10.

     <?php // PAGE LINK/TITLE
    
    
    if (is_page()) {
      $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    
      $posts = get_posts ("category_name=service&posts_per_page=10"); //CHANGE CODE AND ADD THIS LINE***************************
    
      if ($posts) {
        foreach ($posts as $post):
          setup_postdata($post); 
    
                      if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
    } 
    
    
        ?>
    

    This sets the specific category and the number of posts (use category slug to pull it in)

    category_name=service&posts_per_page=10

Comments are closed.