Skip to content

Instantly share code, notes, and snippets.

@jcook3195
Last active September 19, 2017 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcook3195/bb50a27e19a6d5417a19905aa2fc968c to your computer and use it in GitHub Desktop.
Save jcook3195/bb50a27e19a6d5417a19905aa2fc968c to your computer and use it in GitHub Desktop.
Template for Custom Post with CPTUI
<?php
/**
* Template Name: Custom Template -- Replace this with your post type title
*/
remove_action('genesis_loop', 'genesis_do_loop'); //Remove the default custom post type loop
add_action('genesis_loop', 'custom_post_loop'); //Add custom post type loop
remove_action('genesis_before_post_content', 'genesis_post_info'); //Stop post page from displaying the post info (date, comments, etc.)
remove_action('genesis_after_post_content', 'genesis_post_meta'); //Stop post page from displaying the post meta (category, tags, etc.)
function custom_post_loop() { //Initiate the custom post type loop
?>
<!-- This section is not necessary. Any information here will appear at the top of the page before any of the posts. -->
<h1>Title of Custom Post Type</h1>
<p>Description of Custom Post Type</p>
<?php //Register this with the CPTUI and assign it to your custom post type
add_action('init', 'cptui_register_my_cpts_custom_post'); //Swap 'custom_post' with the name of your post type. ('resource_post', 'job_post', etc.)
function cptui_register_my_cpts_custom_post() { //Be sure to swap 'custom_post' with the name of your post type here also.
$labels = array( //Create an array to assign values to the name and singular name you already set within CPTUI plugin
"name" => __('Custom Posts', ''), //Plural posts name
"singular_name" => __('Custom Post', '') //Singular posts name
);
}
?>
<!-- This begins the section where you will loop in the posts themselves and display them on the page. I will
have multiple loops to split them by category, but if you do not want to divide the posts by category you may
have a single loop and leave the 'category_name' out of the array. There are many different options for how you
can display the posts and I will cover some different ways in the future. -->
<?php //Set up variable for post loop
$loop = new WP_Query(array( //Set loop variable which is the array of parameters for the custom post type
'post_type' = 'custom_post', //Specify the post type
'posts_per_page' => 100, //Set the amount of posts per page
'category_name' => 'first_category')); //Filter by category, replace with the name of the category -- omit this to show posts of all categories
?>
<br>
<h1>First Category</h1> <!-- Standard HTML, this will be what displays before your posts. Seperates categories -->
<br>
<?php
while($loop->have_posts()) : $loop->the_post(); //Begin post loop
?>
<div class="post-container"> <!-- Just an example container. Display the post however you would like -->
<a href="<?php the_permalink(); ?>"> <!-- If you want the post to link to the single post page, it is necessary to include this
somewhere -->
<h2 class="post-title"><?php the_title(); ?></h2><!-- This displays the post title -->
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?> <!-- Displays the posts thumbnail, which is the featured image -->
</div>
</a>
</div>
<?php endwhile; wp_reset_query(); ?> <!-- Ends the while loop, and resets the query so another post loop can be initiated -->
<!-- That is all that's necessary to complete the custom post type loop (besides the genesis loop at the end of the file),
everything after this is showing how to filter if you have multiple categories. From here on out is just repeating
the previous loop but with other categories. Repeat for as many categories as you need. -->
<?php
$loop = new WP_Query(array(
'post_type' => 'custom_post',
'posts_per_page' => 100,
'category_name' => 'second-category'));
?>
<br>
<h1>Second Category</h1> <!-- Standard HTML, this will be what displays before your posts. Seperates categories -->
<br>
<?php
while($loop->have_posts()) : $loop->the_post(); //Begin post loop
?>
<div class="post-container"> <!-- Just an example container. Display the post however you would like -->
<a href="<?php the_permalink(); ?>"> <!-- If you want the post to link to the single post page,
it is necessary to include this somewhere -->
<h2 class="post-title"><?php the_title(); ?></h2><!-- This displays the post title -->
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?> <!-- Displays the posts thumbnail, which is the featured image -->
</div>
</a>
</div>
<?php endwhile; wp_reset_query(); ?> <!-- Ends the while loop, and resets the query so another post loop can be initiated -->
<?php } genesis(); //Close custom_do_loop function and enable action hooks ?>
<?php
/*
Single Post Template: Custom Post -- Replace with your post type name
*/
?>
<?php
add_action('genesis_before_loop', 'genesis_do_breadcrumbs'); //Display breadcrumbs like date
?>
<?php get_header(); //Hook the themes default header ?>
<?php genesis_before_content_sidebar_wrap(); //Add this if you want the sidebar on the site. Hooks the sidebar at top of page ?>
<div id="content-sidebar-wrap">
<?php genesis_before_content(); //Hooks in content immediately after body tag of source ?>
<div class="content">
<!-- Loop in all the content you want in the post here -->
<!-- If it is just a standard post with no custom fields do this
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
-->
<!-- If usting Advanced Custom Fields, loop in the fields how you please. For Example:
<h1><?php the_title(); ?></h1>
<p>The first field says: <?php the_field('first_field'); ?></p>
<br>
<p>The second field says: <?php the_field('second_field'); ?></p>
-->
<!-- Loop in everything just like normal HTML, and just add the hooks for the different areas of content -->
</div><!-- End content -->
<?php genesis_after_content(); ?><!-- Hook content just before body closing tag -->
</div><!-- End content-sidebar-wrap -->
<?php genesis_after_content_sidebar_wrap(); ?> <!-- Hook sidebar end just before closing body tag -->
<?php get_footer(); ?> <!-- Hook in the default footer for the theme -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment