Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Forked from billerickson/template-gallery.php
Last active February 24, 2019 17:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpsmith/5062834 to your computer and use it in GitHub Desktop.
Save wpsmith/5062834 to your computer and use it in GitHub Desktop.
Custom Genesis Category Template with Pagination
<?php
/**
* Custom Category Template
*
* @package my_child_theme
* @since 1.0.0
* @author Travis Smith <t@wpsmith.net>
* @copyright Copyright (c) 2013, Travis Smith
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @link https://gist.github.com/wpsmith/5062834
*
*/
// Remove default loop
//remove_action( 'genesis_loop', 'genesis_do_loop' );
function wps_category_loop_args() {
return array(
'category_name' => get_query_var( 'category_name' ),
'paged' => get_query_var( 'paged' ),
);
}
add_action( 'genesis_loop', 'wps_category_do_loop_intro', 5 );
/**
* Custom Loop Intro
*
*/
function wps_category_do_loop_intro() {
$args = wp_parse_args( array( 'tag_id' => 12, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'showposts' => 2, ), wps_category_loop_args() );
$query = new WP_Query( $args );
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post(); global $post;
echo '<h1 id="category-title" class="category title">';
the_title();
echo '</h1>';
the_content();
endwhile;
// If you want pagination for pt 1
//genesis_posts_nav();
endif;
}
// Remove Title
remove_action( 'genesis_post_title ', 'genesis_do_post_title' );
remove_action( 'genesis_post_content ', 'genesis_do_post_content' );
add_action( 'genesis_post_content', 'wps_category_do_post_content' );
/**
* Custom Content
*
*/
function wps_category_do_post_content() {
// Moved this to pre_get_posts
//$args = wp_parse_args( array( 'tag__not_in' => 12, 'wps_category_query' => true, ), wps_category_loop_args() );
echo '<div class="category-item">';
the_post_thumbnail('thumbnail');
echo '<div class="post-content">';
printf( '<h2>%s</h2>', get_the_title() );
the_content();
printf( '<a href="%s">%s</a>', get_permalink(), __( 'View Details', 'text-domain' ) );
echo '</div>';
echo '</div>';
}
genesis();
<?php
add_action( 'pre_get_posts', 'wps_category_pre_get_posts' );
/**
* Set posts per page for the main query part
*
*/
function wps_category_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_category() ) ) {
$query->set( 'posts_per_page', 2 );
$query->set( 'tag__not_in', 12 );
}
}
@djalondon
Copy link

Thanks - this worked for me when I removed a closing bracket in line 9 of functions.php

@mattmcgiv
Copy link

Works. Thanks @djalondon for catching that extra paren

@SRD75
Copy link

SRD75 commented Mar 15, 2017

Can't see where pagination is added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment