I have a custom post type called Projects. It contains a text-area "project description" and a repeater image field. I know that I can use the function WP_Query to return an array containing all my Projects post. This works when I want to show all the projects in one page. My question is how do I handle single entries in the Projects post type. More specifically, I want to know about the function that returns reference to a single entry of Project post type and the template file to be edited so as to achieve the styling of that single custom post page.
As per comments the single view of your CPT will be handled according to template hierarchy. Within appropriate template you can use template tags normally, so simply get_post()
without arguments will get you a current instance.
It is a little more tricky if you want to refer to a specific post in code outside of its context.
The most reliable within custom site is hardcoding its ID. Personally I also use get_page_by_title()
(despite name can work on arbitrary post type) which is easier for human to read, but depends on title staying unchanged.
single-{post_type}.php
template in your theme folder.{post_type}
is the string you have passed toregister_post_type()
as the first argument. Start trying to copy you theme'ssingle.php
file. More on the template hierarchy here. – Luis Sanz May 21 '16 at 10:03while ( have_posts() )
boucle and style the content within. For example, have a look at how it's done in the [_tk theme(github.com/Themekraft/_tk/blob/master/single.php). – Luis Sanz May 21 '16 at 10:26