Skip to content

Instantly share code, notes, and snippets.

@srikat srikat/category.php
Last active Apr 23, 2019

Embed
What would you like to do?
Category images Grid Template in Genesis. http://sridharkatakam.com/category-images-grid-template-genesis/
<?php
/**
* This file adds the category archive template.
*
*/
//* Force full width content layout
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove the breadcrumb navigation
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
//* Remove the post info function
remove_action( 'genesis_entry_header', 'genesis_post_info', 5 );
//* Remove the post content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
//* Remove the post image
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
//* Add portfolio body class to the head
// add_filter( 'body_class', 'executive_add_portfolio_body_class' );
function executive_add_portfolio_body_class( $classes ) {
$classes[] = 'executive-pro-portfolio';
return $classes;
}
/**
* Display as Columns
*
*/
function be_portfolio_post_class( $classes ) {
$columns = 4; // Set the number of columns here
$column_classes = array( '', '', 'one-half', 'one-third', 'one-fourth', 'one-fifth', 'one-sixth' );
$classes[] = $column_classes[$columns];
global $wp_query;
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post % $columns )
$classes[] = 'first';
return $classes;
}
add_filter( 'post_class', 'be_portfolio_post_class' );
//* Add the featured image after post title
add_action( 'genesis_entry_header', 'sk_category_grid' );
function sk_category_grid() {
if ( $image = genesis_get_image( 'format=url&size=category-image' ) ) {
printf( '<div class="category-grid-post-featured-image"><a href="%s" rel="bookmark"><img src="%s" alt="%s" /></a></div>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
}
}
//* Customize entry meta in the entry header
add_filter( 'genesis_post_info', 'sp_post_info_filter' );
function sp_post_info_filter($post_info) {
// $post_info = '[post_date] by [post_author_posts_link] [post_comments] [post_edit]';
$post_info = '[post_date] [post_edit]';
return $post_info;
}
//* Remove the post meta function
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
genesis();
//* Add new image size
add_image_size( 'category-image', 350, 263, true );
//* Display left floating featured image on single Posts
add_action( 'genesis_entry_content', 'show_featured_image', 9 );
function show_featured_image() {
if (is_singular( 'post' ))
genesis_image( array( 'size' => 'medium', 'attr' => array ('class' => 'alignleft') ) );
}
<?php
/*
Template Name: Categories Grid
*/
# Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
# Remove the breadcrumb
// remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
# Remove the post title
// remove_action( 'genesis_post_title', 'genesis_do_post_title' );
add_filter( 'body_class', 'categories_grid_body_class' );
/**
* Adds a css class to the body element
*
* @param array $classes the current body classes
* @return array $classes modified classes
*/
function categories_grid_body_class( $classes ) {
$classes[] = 'categories-grid';
return $classes;
}
# Remove the post content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
# Add custom post content
add_action( 'genesis_entry_content', 'sk_do_post_content' );
/**
* Outputs custom post content
*
* @return void
*/
function sk_do_post_content() {
$i = 0;
$columns = 4; // Set the number of columns here (2 to 6)
$column_classes = array( '', '', 'one-half', 'one-third', 'one-fourth', 'one-fifth', 'one-sixth' );
$class = $column_classes[$columns];
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
if (z_taxonomy_image_url($category->term_id)) { // if category image exists
if ($i % $columns == 0) {
echo '<div class="'. $class . ' first category-item">';
}
else {
echo '<div class="' . $class . ' category-item">';
}
// Category title
echo '<h2 class="category-title"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></h2>';
// Category image linking to category archive
echo '<a href="' . get_category_link( $category->term_id ) . '"><img src="'. z_taxonomy_image_url($category->term_id, 'category-image') . '" /></a>';
// Category description
echo category_description( $category->term_id );
// Custom 'Read More' link
echo '<a href="' . get_category_link( $category->term_id ) . '">All Posts under ' . $category->name . ' category &raquo;</a>';
echo '</div>';
$i++;
}
}
}
genesis();
/* Categories Grid
----------------------------------------- */
.categories-grid .entry-header {
margin-bottom: 40px;
}
.categories-grid h2.category-title a,
.categories-grid h3.category-title a {
border-bottom: none;
}
.categories-grid .category-item {
margin-bottom: 40px;
}
.category .entry-title {
word-wrap: break-word;
}
.categories-grid .category-item p {
margin-top: 10px;
margin-bottom: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.