Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active August 29, 2015 14:07
Show Gist options
  • Save danielpataki/2443a9e4f5aa44187b90 to your computer and use it in GitHub Desktop.
Save danielpataki/2443a9e4f5aa44187b90 to your computer and use it in GitHub Desktop.
Custom Post List Template
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_post-list-options',
'title' => 'Post List Options',
'fields' => array (
array (
'key' => 'field_543f8a046d65e',
'label' => 'Authors',
'name' => 'authors',
'type' => 'user',
'instructions' => 'Select some users, only posts from these users will be shown',
'role' => array (
0 => 'all',
),
'field_type' => 'multi_select',
'allow_null' => 1,
),
array (
'key' => 'field_543f8a206d65f',
'label' => 'Categories',
'name' => 'categories',
'type' => 'taxonomy',
'instructions' => 'Select the categories to show posts from',
'taxonomy' => 'category',
'field_type' => 'multi_select',
'allow_null' => 1,
'load_save_terms' => 0,
'return_format' => 'id',
'multiple' => 0,
),
array (
'key' => 'field_543f8a496d660',
'label' => 'Category Include/Exclude',
'name' => 'cartegory_include_exclude',
'type' => 'radio',
'instructions' => 'Set weather to include or exclude the categories selected above. If include is selected only posts from the selected categories will be shown. If exclude is selected all posts which do not belong to the chosen categories will be shown. ',
'choices' => array (
'include' => 'Include',
'exclude' => 'Exclude',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => 'include',
'layout' => 'horizontal',
),
array (
'key' => 'field_543f8aa06d661',
'label' => 'Post Status',
'name' => 'post_status',
'type' => 'checkbox',
'instructions' => 'Select the post statuses to pull posts from ',
'choices' => array (
'publish' => 'Published',
'future' => 'Scheduled',
'draft' => 'Draft',
'pending' => 'Pending',
'trash' => 'Trash',
),
'default_value' => 'publish',
'layout' => 'vertical',
),
array (
'key' => 'field_543f8ae16d662',
'label' => 'Post Type',
'name' => 'post_type',
'type' => 'checkbox',
'instructions' => 'Set the post types to list',
'choices' => array (
'post' => 'Post',
'page' => 'Page',
),
'default_value' => 'post',
'layout' => 'vertical',
),
array (
'key' => 'field_543f8aff6d663',
'label' => 'Posts Per Page',
'name' => 'posts_per_page',
'type' => 'number',
'instructions' => 'Set how many posts should be shown on a page',
'required' => 1,
'default_value' => 10,
'placeholder' => 'Number of posts to show',
'prepend' => '',
'append' => '',
'min' => '-1',
'max' => '',
'step' => 1,
),
array (
'key' => 'field_543f8b246d664',
'label' => 'Order By',
'name' => 'order_by',
'type' => 'select',
'instructions' => 'Set how you would like to order your posts',
'choices' => array (
'ID' => 'ID',
'author' => 'Post Author',
'title' => 'Post Title',
'name' => 'Post Name',
'type' => 'Post Type',
'date' => 'Published Date',
'modified' => 'Modification Dare',
'rand' => 'Random',
'comment_count' => 'Comment Count',
'menu_order' => 'Menu Order',
'' => '',
),
'default_value' => 'date',
'allow_null' => 0,
'multiple' => 0,
),
array (
'key' => 'field_543f8b976d665',
'label' => 'Order',
'name' => 'order',
'type' => 'radio',
'instructions' => 'Set the ordering direction',
'choices' => array (
'ASC' => 'Ascending',
'DESC' => 'Descending',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => 'DESC',
'layout' => 'horizontal',
),
array (
'key' => 'field_543f8baf6d666',
'label' => 'Has Featured Image',
'name' => 'has_featured_image',
'type' => 'true_false',
'instructions' => 'If checked only posts with featured images will be used',
'message' => 'Only posts with featured images?',
'default_value' => 0,
),
),
'location' => array (
array (
array (
'param' => 'page_template',
'operator' => '==',
'value' => 'page-templates/post-list.php',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
$authors = get_field( 'authors' );
$authors = array_map('current', $authors);
$args = array(
'post_type' => 'post',
'author__in' => $authors
);
$args = array(
'post_type' => 'post',
'author__in' => $authors,
'category__in' => get_field( 'categories' )
);
$args = array(
'post_type' => 'post',
'author__in' => $authors,
);
if( get_field( 'cartegory_include_exclude' ) == 'exclude' ) {
$args['category__not_in'] = get_field( 'categories' );
}
else {
$args['category__in'] = get_field( 'categories' );
}
<?php
/**
* Template Name: Post List
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$authors = get_field( 'authors' );
$authors = array_map('current', $authors);
$args = array(
'author__in' => $authors,
'post_status' => get_field( 'post_status' ),
'category__in' => get_field( 'categories' ),
'post_type' => get_field( 'post_type' ),
'posts_per_page' => get_field( 'posts_per_page' ),
'orderby' => get_field( 'orderby' ),
'order' => get_field( 'order' )
);
if( get_field( 'cartegory_include_exclude' ) == 'exclude' ) {
$args['category__not_in'] = get_field( 'categories' );
}
else {
$args['category__in'] = get_field( 'categories' );
}
if( !empty( get_field( 'has_featured_image' ) ) ) {
$args['meta_query'] = array(
array(
'key' => '_thumbnail_id',
'value' => '',
'compare' => '!=',
),
);
}
$postlist = new WP_Query( $args );
if ( $postlist->have_posts() ) :
while ( $postlist->have_posts() ) : $postlist->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
$args = array( 'post_type' => 'post' );
$postlist = new WP_Query( $args );
if( $postlist->have_posts() ) {
while( $postlist->have_posts() ) {
$postlist->the_post();
// list posts here
}
// post pagination
}
else {
// no posts content
}
$args = array( 'post_type' => 'post' );
$postlist = new WP_Query( $args );
if( $postlist->have_posts() ) {
while( $postlist->have_posts() ) {
$postlist->the_post();
get_template_part( 'content', get_post_format() );
}
twentyfourteen_paging_nav();
}
else {
get_template_part( 'content', 'none' );
}
$args = array(
'post_status' => get_field( 'post_status' ),
'post_type' => get_field( 'post_type' ),
'posts_per_page' => get_field( 'posts_per_page' ),
'orderby' => get_field( 'orderby' ),
'order' => get_field( 'order' )
);
<?php
/**
* Template Name: Post List
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
$args = array(
'post_type' => 'post',
);
if( !empty( get_field( 'has_featured_image' ) ) ) {
$args['meta_query'] = array(
array(
'key' => '_thumbnail_id',
'value' => '',
'compare' => '!=',
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment