For those unfamiliar, custom post types is a powerful feature in WordPress that allows you to create new types of content beyond the traditional "posts" and "pages." This can be incredibly useful when you want to make different types of content on your website, such as testimonials, portfolios, or team members. This way, when the theme updates, you get to keep all your changes.Welcome, fellow WordPress enthusiasts! Today, we'll dive into custom post types. ![]() Note: If you’re making modifications to existing files, or creating new ones, it’s always good to make those changes from a child theme. More info on this function can be found here. This function restores the global $post variable of the main query after a secondary query loop has been executed. Make sure to call the wp_reset_postdata() function after the endwhile statement. Use “->” symbol when trying to call a function of a class like this: $myQuery->have_posts().Ģ. Make sure to use the “=>” symbol when creating the $args array for the WP_Query object. This is how we query the WordPress the database, and load articles from custom post types on the landing page. You need to add the following piece of code after the main WordPress loop in your index.php file. This is an example of how a simple index.php file would look like, and what changes we can apply to it to reach our goal: We can modify our theme’s index.php, and make it load our Podcast post type by creating an additional query to the database. In our example, this would mean to display podcast and blog posts together on the landing page.īy default, WordPress uses a template file called index.php (if there’s no front-page.php or home.php) to display posts on the landing page. Querying Custom Post Types on the Landing Page Alongside Regular PostsĪ more advanced approach when dealing with custom post types, is to display them alongside other types of content on your front page. To get started, you can copy the contents from single.php and start making changes per your needs. This comes in handy when you have custom fields or taxonomies. ![]() Similarly, you can create a file called single-podcasts.php to display your single articles from Podcast CPT in a different way. By default, WordPress uses single.php to display single entries on the front-end. A good practice is to copy the contents of archive.php into your new file, and start making modifications from there.Īnother thing you can do, is to create a custom template for your single entries. If you open Podcassfrom your menu, you’ll see a blank page, because WordPress loads your custom template - which is currently empty. If such file exists, WordPress will automatically use this template file when displaying content from your custom post type. All you need to do here, is create a file called archive-podcasts.php in your theme’s directory. If you don’t like how the default archive.php template displays your Podcast posts, you can create a custom template. Using Custom Templates for Archive Pages and Single Post Entries If you click on this link, it will display the articles from Podcasts post type using the default archive.php template. The next time you open your website, you will see Podcasts on your menu. Then, set the name of the menu item to Podcasts, and save/update your menu. ![]() Change with your website’s name, and podcasts with your CPT name (if they are different). If you’re on a fresh WordPress installation, your CPT link will look like this: But if you changed the structure to display links in more SEO and user friendly manner, your post type URL can look like this: Depending on your setup, you need to get this link, and add it in your menu as a custom link. From here, you need to navigate to Appearance->Menus, and add a custom link to your navigation. In order for this to work, you need to go to Settings->s, and check what type of link structure you’re using. ![]() Our WordPress Podcast Themes can work with a custom post type and by using the regular “Posts” type as well!ģ Ways To Display Custom Post Types in WordPress 1. Before trying these methods, make sure you have at least 1 articles published under your CPT. In this guide, we will discover 3 ways you can display articles from the Podcast post type on your website. Just to recap, we created a custom post type called Podcast, and a custom taxonomy - Podcast Category. In the previous article in this series, we’ve created a Custom Post Type, it’s learn how you can display it on your website. Need help with your WordPress Project? Hire an experienced developer today!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |