Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJennifer Hinkle
8,365 Pointssingle-"custompage" not loading correctly
Hello,
I'm following the How to Build a Wordpress Theme, but am making my own site following the videos. I have a blog page and also two other custom post type pages (topics.php and news.php). When I click on a blog post title, it takes me to single.php as it should. However, when I click on a topics or news title, the display and loop are all wrong. All of my CSS disappears and also, instead of showing me only the one post, it shows me all of the posts. (Basically, single-topics.php is the exact same as topics.php except single-topics.php doesn't have any CSS applied and the header disappears). I hope that doesn't sound too confusing. Any help or direction on what I should do would be greatly appreciated!
Thanks, Jenn
2 Answers
Kevin Korte
28,149 PointsThere is a couple of ways to troubleshoot it. First, make sure you are following the steps here: http://codex.wordpress.org/Page_Templates
Second, on your template files, while in development, I find it helps to add this
<p>single.php</p>
type of code to the very top of your template. If it's the single-topics.php
file that add that line instead. Basically designate what the file name is so when your developing, you know what wordpress file is using. You can tell if WP is using single-topics.php or just the single.php file. It really helps. And than before development, just delete that one line of code from each page.
So what I'm saying is make sure you followed the steps in the first link, and than make sure WP is using the correct template.
Christophe Rudyj
Full Stack JavaScript Techdegree Student 13,011 Pointsin a custom post you need to have this sort of code for the "single" view to work correctly
'rewrite' => array( 'slug' => 'custompostname','with_front' => FALSE)
);```
Jennifer Hinkle
8,365 PointsHello, Thanks for the response/ help! I've read your advice, but I don't really understand what it means. I've tried googling it, but I still can't make sense of it. Could you point me in the direction explaining what a rewrite property is? Also, the code you suggested, where would I place that? In my single-topic page? Or my content-topic page? Thanks so much! Jenn
Christophe Rudyj
Full Stack JavaScript Techdegree Student 13,011 PointsThat code goes into your array when you declare a custom post
add_action( 'init', 'mudv_chara_register' );
function mudv_chara_register() {
$labels = array(
'name' => _x( 'Characters', 'chara' ),
'singular_name' => _x( 'Character', 'chara' ),
'add_new' => _x( 'Add New', 'chara' ),
'add_new_item' => _x( 'Add New Character', 'chara' ),
'edit_item' => _x( 'Edit Character', 'chara' ),
'new_item' => _x( 'New Character', 'chara' ),
'view_item' => _x( 'View Character', 'chara' ),
'search_items' => _x( 'Search Characters', 'chara' ),
'not_found' => _x( 'No characters found', 'chara' ),
'not_found_in_trash' => _x( 'No characters found in Trash', 'chara' ),
'parent_item_colon' => _x( 'Parent Character:', 'chara' ),
'menu_name' => _x( 'Characters', 'chara' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'editor' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array( 'slug' => 'chara','with_front' => FALSE),
'capability_type' => 'post'
);
register_post_type( 'chara', $args );
}
in my example the custom post name is chara basicaly what it does it's forcing wordpress to use yourdomain.com/custompostname/
there's a more complete explication here but its mearly to ensure that your single page works http://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-post-types-taxonomies/
then since your doing only single page you don't need to do a <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
because you have a single page its already going to get the post you want to acess the if while post is mostly used in category pages because it needs to re loop for each post other wise its better to use the-post(); right after get_header ();
Jennifer Hinkle
8,365 PointsJennifer Hinkle
8,365 PointsHi Kevin, Thanks for the advice! I actually keep "whateverpage.php" at the top of all the pages I'm still developing, so they we're all linking correctly. It turns out my
<?php get_header(); ?>
was missing the initial "<?php" ... of course it was just me having a typo. So my CSS now works perfectly. But for some reason when I go to "single-topics.php" it is still listing all of the "topics" posts. Must be something I'm doing wrong with my loop. I was wondering, does the:
is_single()
function work the same for custom post types as for regular blog posts? If that's not the problem, I'll just rake my code for more typos!
Thanks again for all of your help! (Here and on other forum questions I've asked!) You've really helped save my project! Jenn
Below is my code for my content-topic.php page for reference.
Also my single-topic.php.
Christophe Rudyj
Full Stack JavaScript Techdegree Student 13,011 PointsChristophe Rudyj
Full Stack JavaScript Techdegree Student 13,011 Pointsthe reason why your loop fails is because of the rewrite propety i mentioned below normaly you have category-customname and single-customname
also you dont have to reloop for the single-custom post as its going to get the current custop post topic
so basically just do the get_header(); the_post();