{"id":5622311,"date":"2014-12-30T17:19:46","date_gmt":"2014-12-30T17:19:46","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/code-to-display-posts-by-category-1\/"},"modified":"2016-08-22T13:57:43","modified_gmt":"2016-08-22T13:57:43","slug":"code-to-display-posts-by-category-1","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/code-to-display-posts-by-category-1\/","title":{"rendered":"Code to display posts by category."},"content":{"rendered":"<p>Hi I am a big newbie to wordpress and getting rather stressed out trying to find answers on google. I want to show my posts relating to differrent topics in their specific category.  ie. posts relating to news in the news cateogry, and opinion in opinion category etc.<\/p>\n<p>The problem is I have made a custom category template.<\/p>\n<p>I have put in the following code after my get header:<\/p>\n<blockquote>\n<ul>\n\t\t\t&lt;li class=&#8221;page_item&#8221;&gt;&lt;h1&gt;Recent News&lt;\/h1&gt;<br \/>\n\t\t&lt;?php<br \/>\n\t\t\t$myposts = get_posts(&#8216;numberposts=10&amp;category=&#8217;);<br \/>\n\t\t\tforeach($myposts as $post) :<br \/>\n\t\t\tsetup_postdata($post);<br \/>\n\t\t\t?&gt;<br \/>\n\t\t\t&lt;?php if ( ! in_category(6) ) { ?&gt;<br \/>\n\t\t\t\t&lt;li class=&#8217;page_item&#8217;&gt;<a>&#8221; rel=&#8221;bookmark&#8221; title=&#8221;Permanent Link to &lt;?php the_title(); ?&gt;&#8221;&gt;&lt;?php the_title(); ?&gt;<\/a><br \/>\n\t\t\t&lt;?php } ?&gt;<br \/>\n\t\t&lt;?php endforeach; ?&gt;\n\t<\/ul>\n<\/blockquote>\n<p>This is allowing me to see links to my posts in a given cateogry, eg.  <a href=\"http:\/\/www.integrateni.com\/category\/news\">www.integrateni.com\/category\/news<\/a> but I would like them in list form and with a thumbnail and excerpt.  Does anyone have any idea how change the code to allow this.<\/p>\n<p>My other BIG problem is that the same posts are showing up in each of my cateogries, is it necessary to make a separate category.php for each cateory, or is there a way of changing the code so it does this automatically when I create a new category.  ie. above in my h1 it says<\/p>\n<blockquote>\n<p>Recent News<\/p>\n<\/blockquote>\n<p> but I only want this for the news category, and would like it to say opinion for the opinion category, community for the community category etc.<\/p>\n<p>I hope someone understands what I am trying to explain and can help me out.<\/p>\n<p>Many thanks in advance<\/p>\n","protected":false},"template":"","class_list":["post-5622311","topic","type-topic","status-closed","hentry","topic-tag-new-topic"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/5622311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/5622311\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=5622311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}