• So I feel like I am doing something obviously simple, yet can’t figure out what!

    I’ve created my custom post type ‘project’ and created single-project.php to display the individual project posts. It’s working (sort of), my problem is that instead of displaying the content for just the 1 post, it shows the content for all of the posts i’ve created.

    Here’s my single-project.php file:
    http://pastebin.com/z20gVtEL

    Any help would be much appreciated!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,

    I’d recommend u to create the “Projects” post type. Then u’d have Projects as the main WordPress custom type and project could be used as a single page slug.

    Something like this:

    $args = array(
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'project' ),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title' , 'editor', 'thumbnail')
      );
      register_post_type( 'projects', $args );
    Thread Starter lmstreng

    (@lmstreng)

    Thanks Jos
    Unfortunately i’m not familiar with slugs just yet :/

    What’s the benefit of creating the custom post type and then a single page slug this way instead of just the single- .php template?

    Hey,
    I don’t personally like naming the post types the same way as the rewrite slugs. I think there’s no difference around it at all. If i’m wrong, please correct me. I guess it’s simpler to have the post type having the role of a group and the elements being part of that group, not the group themselves :D.

    It’s like, get_posts() and get_post()

    You can find all options avaiable in register_post_type() at:
    http://codex.wordpress.org/Function_Reference/register_post_type

    Moderator bcworkz

    (@bcworkz)

    The use of plural vs. singular forms as names is a matter of personal preference. Whether it makes sense or not, all default post types are singular form: post, page, attachment, etc. Follow the same scheme or change it, it doesn’t really matter. single-{$post_type}.php is the proper template name, there are not {$post_type}.php templates in the standard Template Hierarchy. Themes and plugins could modify the template hierarchy, so anything is possible in the end.

    Which is the only explanation I can think of to explain what’s going on. The template should only be used for single queries, yet you are getting multiple results. Normally, multiple results end up on an archive template. The other issue is why are you getting multiple results? I’m assuming you are using a permalink that contains a single project slug or ID, yet you are getting multiple results. Something is modifying the query.

    In order to narrow down the infinite possibilities introduced by themes and plugins and to establish a known installation, please deactivate all your plugins and switch to one of the default twenty* themes. It shouldn’t be an issue, but do a full backup before doing this for good measure. I suspect the problem will go away. We then know where to start looking for a solution. If it does not go away, please share the permalink you are using so we can inspect the resulting page.

    The name of your post type has nothing to do with your problem nor do any of the other suggestions mentioned above. The problem is the code in your template.

    Looking at your code from the pastebin link here is what is happening.

    FYI. When WordPress loads a template it has already made the query and has the intended post or posts ready to display. In your template you are creating a new query which is loading all posts with the ‘project’ post type. #Line 12

    Next on line 15 you are looping through all the posts you just queried above on line 12.

    On line 17 you are checking is_single() THIS WILL ALWAYS EVALUATE AS TRUE in a single-post_type.php template because it uses the global $wp_query object and not the new WP_Query object you created on line 12.

    So to fix your problem remove the new WP_Query object line 12 through 14 then change line 15 to

    <?php while ( have_posts() ) : the_post(); ?>

    You also don’t need the is_search stuff at the bottom because this it will never return true using the single-post_type.php template. Here is fixed version of your template.

    http://pastebin.com/mVXE8fMp

    Hey Chris,

    thanks for your answer. I had doubts myself about it.

    Regards !

    I have been searching for the answer to this problem for about 14 hours now. I mean non stop if I was not troubleshooting, testing, backing up, changing my loop, I was on Google and looking through here. I must say I am so happy that I finally found this wonderful post.

    I had this same issue and I had my code very similar so it worked out. Thanks guys.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom Post Type, the single-.php problem’ is closed to new replies.