Custom Post Types on WordPress Homepages |
| November 7, 2011 |
Let's imagine that you're now empowered by knowledge and have gone wild, creating lots of new custom post types. They work wonderfully, but by default they don't show on your homepage. So, how do you get these custom post types to show on the front page of your blog with the normal post types? There are a couple ways to do this, neither very difficult. Trying them both will give you practice editing functions.php and The Loop, both at a very beginning cut-and-paste-coder level. In method one, we'll show you where to find the functions.php file, and we'll also give you a few lines to paste into it. In method two, we'll be pasting a few lines of code in the The Loop, found at the top of the default index.php template file. Method 1 - Step 1![]()
Method 1 - Step 2![]() On the right side of the page there is a list of files. Scroll down and find Theme Functions and click on it. This will open the file in the editing window. Scroll all the way to the bottom of the window, and after the last line, paste the code. Method 1 - Step 3. Paste this code into your functions.php file![]() Paste the following code at the bottom of the editing window Click Update File to save it. That's all there is to it. After you paste it, change the array to a list of your content types. Type the names between single quotes 'name' separated by a comma, no comma after the last one in line. You could also make the changes in a text editor before you paste it. Don't delete any brackets or punctuation or add any stray marks. This code sets all the post types you would like to have displayed by using the pre_get_posts filter. At one point in WP 3.2's short history, someone found that the filter might break the new menu’s in WordPress 3.0. That did not happen for me. Even so i am going to include a second method in case you run into problems. This will also introduce you to The Loop, which is often modified by theme developers and you may need to tweak it at some point. Method 2 - Step 1. Open index.php in the editor![]()
Method 2 - Step 2![]() Find the Main Index Template and open it by clicking on the name. Method 2 - Step 3![]() What is The Loop.The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. Found at the top of the default index.php template file is the starting code for The Loop. and ends with: This is the real heart of the page. The rest of code mostly makes the output of The Loop look good. (An oversimplification, to be sure, but it makes my point.) Method 2 - Step 4![]() To display the posts from your custom post type, add the following code in the loop:
Add your list of post types in the array. Paste it right before the line. The comment line may not be in every template, but the first line of the loop will always be in the template. If you don't see the comment use the start line as your placement guide. Type the names between single quotes 'name' separated by a comma, no comma after the last one in line. You could also make the changes in a text editor before you paste it. Don't delete any brackets or punctuation or add any stray marks. Change movies with your custom post type name and add any others you have. The above code will display all the post from the regular post type (post) and from the custom post types. That’s it. The result![]() You can see that Hello World! (which is a normal post type and the Sample Page both show up on the home page. "page" is a post type. It shows up on the home page because we added the post type to the array You can now include or exclude any post type from your home page. You don't need to use both these methods, only one is sufficient. |
Open Source Training is not affiliated with or endorsed by the Joomla, WordPress or Drupal projects.
All product names and trademarks are the property of their respective owners.
Copyright © 2012 Open Source Training, LLC. All rights reserved.
Comments
We would love to get into a discussion with you on this. If you are a student at OSTraining.com, please log into the support forum (www.ostraining.com/.../) and post the question in there, so that one of our support techs can look into it for you. If you’re not a student, I hope you’ll consider becoming one, so that we can give you the attention you deserve. You can find out more about our online class at www.ostraining.com/online
Kind regards,
Nick
RSS feed for comments to this post