0

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.

  • 2
    You probably want to create a single-{post_type}.php template in your theme folder. {post_type} is the string you have passed to register_post_type() as the first argument. Start trying to copy you theme's single.php file. More on the template hierarchy here. – Luis Sanz May 21 '16 at 10:03
  • Thanks. That answers the second part of the question very well. Can you tell me what function to use to get a reference to the particular entry that I will be styling through the mentioned single-{post_type}.php . I have coded the page that displays all the projects excerpt. Now I want the user to be able to have a full display of all the contents of a single project that he clicks on. – Abhilash Singh May 21 '16 at 10:10
  • 1
    You don't need to call any specific function. When you visit a single custom post type entry, the content is parsed through The Loop. You don't need to create a new query, the query is already created by WordPress. What you need to do is just run your while ( 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
0

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.

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.