Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Last active February 27, 2018 09:07
Show Gist options
  • Select an option

  • Save GarySwift/2340606c30dbf9b20651d8d0a5793ac6 to your computer and use it in GitHub Desktop.

Select an option

Save GarySwift/2340606c30dbf9b20651d8d0a5793ac6 to your computer and use it in GitHub Desktop.
WordPress Template: CPT, custom post type loop (To use with FoundationPress theme)
<?php
/**
* Template Name: Custom Post
*
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
get_header();
wp_reset_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_type = 'custom_post';
$posts_per_page = 1;
$args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
);
global $wp_query;
$wp_query = new WP_Query($args);
?>
<div class="main-container">
<div class="main-grid">
<main class="main-content">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; // End have_posts() check. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php
if ( function_exists( 'foundationpress_pagination' ) ) :
foundationpress_pagination();
elseif ( is_paged() ) :
?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
</nav>
<?php endif; ?>
</main>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer();
<?php
/*
* Template Name: CPT Index
*/
get_header();
if( get_field('page_post_type') ) {
$post_type = get_field('page_post_type');
}
else {
$post_type = 'post';
}
?>
<div class="main-wrap" role="main">
<article <?php post_class('main-content') ?> id="page-cpt-<?php echo $post_type; ?>">
<?php while ( have_posts() ) : the_post(); ?>
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_content(); ?>
<?php edit_post_link( __( 'Edit', 'foundationpress' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<?php endwhile; ?>
<?php
wp_reset_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $post_type,
'posts_per_page' => 8,
'paged' => $paged,
);
global $wp_query;
$wp_query = new WP_Query($args);
if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post(); ?>
<div class="clearfix">
<?php
if ( has_post_thumbnail( $post->ID ) ) : ?>
<img src="<?php echo the_post_thumbnail_url('large'); ?>" alt="Image for <?php get_the_title() ?>">
<?php endif; ?>
<h4><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h4>
<div class="entry-content"><?php the_excerpt();?></div>
<div class=""><a href="<?php the_permalink() ?>" class="button">Read More</a></div>
</div>
<?php
endwhile; ?>
<?php
endif; // End have_posts() check.
/* Then the pagination links */
/* Display navigation to next/previous pages when applicable */ ?>
<?php if ( function_exists( 'foundationpress_pagination' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
</nav>
<?php } ?>
</article>
<?php do_action( 'foundationpress_after_content' ); ?>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
<?php
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_573d737196da9',
'title' => 'Page Settings: CPT Index',
'fields' => array (
array (
'key' => 'field_573d737b104c6',
'label' => 'Page Post Type',
'name' => 'page_post_type',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
),
'default_value' => array (
),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
'disabled' => 0,
'readonly' => 0,
),
),
'location' => array (
array (
array (
'param' => 'page_template',
'operator' => '==',
'value' => 'page-templates/page-cpt.php',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
/*
* Gets all posts types and injects them into select box so user
* can select post type
*
* Used by two ACF field groups
*/
function acf_load_post_types_choices( $field ) {
// reset choices
$field['choices'] = array();
// Posts are built in so we add them here instead
$field['choices']['post' ] = 'Post';
// Filter the query so only custom post types are returned
$args = array(
'_builtin' => false
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
// Get post types
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
// Do not add the types in the array
if ( !in_array($post_type, array('acf-field-group','acf-field'), true ) ) {
$field['choices'][$post_type] = ucwords( str_replace('_', ' ', $post_type) );
}
}
// return the field
return $field;
}
// Unknown injection
// add_filter('acf/load_field/name=post_type', 'acf_load_post_types_choices');
add_filter('acf/load_field/name=page_post_type', 'acf_load_post_types_choices');
endif;
<?php
/*
* Template Name: CPT Index
*/
get_header();
if( get_field('page_post_type') ) {
$post_type = get_field('page_post_type');
}
else {
$post_type = 'post';
}
?>
<div id="page" role="main">
<article class="main-content">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $post_type,
'posts_per_page' => 4,
'paged' => $paged,
);
global $wp_query;
$wp_query = new WP_Query($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?>
<div class="content-wrapper">
<h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<div class="entry-content"><?php the_excerpt();?></div>
</div>
<?php
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif; // End have_posts() check.
wp_reset_query();
?>
<!-- then the pagination links -->
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( function_exists( 'foundationpress_pagination' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
</nav>
<?php } ?>
</article>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment