I am new with word press and I want to show the post of a specific category on my page. The name of the category which i want to show is home slider. Below is the code which I have used to display all the posts title, image and content.

<div class="row">
                <?php $myposts = get_posts( 'numberposts=6&offset=$debut' ); 
                foreach( $myposts as $post) : setup_postdata( $post ) ?>

                    <div class="col-sm-4 col-lg-4 col-md-4">
                            <div class="thumbnail">
                            <?php the_post_thumbnail( 'thumbnail'); ?>
                            <div class="caption">
                            <h4 class="pull-right">$94.99</h4>
                            <a> <?php the_title(); ?> </a>
                          <!--I have used this substr here to set the limit of the text, and if do not want to set the limit simply use this line of code <?php //the_content(); ?>-->
                          <?php echo substr(get_the_excerpt(), 0,30); ?>...
                          <a href="#" >read more</a>
                            <div class="ratings">
                                <p class="pull-right">18 reviews</p>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star-empty"></span>
                    <?php endforeach; ?>

Can please anyone edit this code so that I can show posts of the specific category which in my case is home slider. Thank you.

  • I really do not want to sound rude or anything, but did you actually tried to read the official documentation, or tried to do a simple site or google search. I think there is more info on this subject than any other subject in WordPress. C'mon man, at least do the basics yourself. We are not here to spoonfeed you because you did not even try to put some effort into solving this yourself – Pieter Goosen Jul 1 '16 at 6:54
  • I actually did tried and solved it but cant find the way I want to use. I edited this line <?php $myposts = get_posts( 'catgegory=3&numberposts=6&offset=$debut' ); and got the post from the category i wanted but i want to access it by the name of category. I googled it but couldn't find the exact solution. – Salik Asad Jul 1 '16 at 7:01
  • use category_name and pass the slug of the category. There is no parameter that accepts the name of a term. If you only have the name, use get_term_by() to get the complete category object, and from there get the ID and pass it to category – Pieter Goosen Jul 1 '16 at 7:04

Change $myposts = get_posts( 'numberposts=6&offset=$debut') to $myposts = get_posts( 'numberposts=6&category_name=home-slider&offset=$debut')

OR $myposts = get_posts( 'numberposts=6&category=1&offset=$debut')

replace 1 with your category ID

Hope this helps

  • Thank you it helped, can you please also tell me how to display the name of the category. I have used the_category() to show the name but it is showing me name with a link. How can i display category name without link. – Salik Asad Jul 1 '16 at 9:52

Check here

Note: The category parameter needs to be the ID of the category, and not the category name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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