Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active February 7, 2022 15:35
Show Gist options
  • Save srikat/11519661 to your computer and use it in GitHub Desktop.
Save srikat/11519661 to your computer and use it in GitHub Desktop.
Single and Archive templates for Custom Post Type in Genesis. http://sridharkatakam.com/single-archive-templates-custom-post-type-genesis/
<?php
//* Remove breadcrumbs
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
//* Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove entry meta in entry header
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
//* Add 'one-half' class to Entry Header to float it left
add_filter( 'genesis_attr_entry-header', 'sk_genesis_attributes_entry_header' );
/**
* Add attributes for entry header element.
*
* @param array $attributes Existing attributes.
*
* @return array Amended attributes.
*/
function sk_genesis_attributes_entry_header( $attributes ) {
$attributes['class'] = 'entry-header one-half first';
return $attributes;
}
//* Add 'one-half' class to Entry Content to float it right
add_filter( 'genesis_attr_entry-content', 'sk_genesis_attributes_entry_content' );
/**
* Add attributes for entry content element.
*
* @param array $attributes Existing attributes.
*
* @return array Amended attributes.
*/
function sk_genesis_attributes_entry_content( $attributes ) {
$attributes['class'] = 'entry-content one-half';
return $attributes;
}
//* Display values of custom fields (those that are not empty)
add_action( 'genesis_entry_header', 'sk_display_custom_fields' );
//* Remove default post image
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
//* Add post image in Entry Content above Excerpt
add_action( 'genesis_entry_content', 'sk_display_featured_image', 9 );
function sk_display_featured_image() {
$image_args = array(
'size' => 'medium',
'attr' => array(
'class' => 'alignright',
),
);
$image = genesis_get_image( $image_args );
if ( $image ) {
echo '<a href="' . get_permalink() . '">' . $image .'</a>';
}
}
//* To remove empty markup, '<p class="entry-meta"></p>' for entries that have not been assigned to any Genre
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
add_action( 'genesis_entry_footer', 'sk_custom_post_meta' );
genesis();
//* [Site-wide] Modify the Excerpt read more link
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_more($more) {
return '... <a class="more-link" href="' . get_permalink() . '">Read More</a>';
}
//* [Dashboard] Add Archive Settings option to Books CPT
add_post_type_support( 'books', 'genesis-cpt-archives-settings' );
/**
* [Dashboard] Add Genre Taxonomy to columns at http://example.com/wp-admin/edit.php?post_type=books
* URL: http://make.wordpress.org/core/2012/12/11/wordpress-3-5-admin-columns-for-custom-taxonomies/
*/
add_filter( 'manage_taxonomies_for_books_columns', 'books_columns' );
function books_columns( $taxonomies ) {
$taxonomies[] = 'genre';
return $taxonomies;
}
//* [All Book pages] Function to display values of custom fields (if not empty)
function sk_display_custom_fields() {
$book_price = get_field( 'book_price' );
$book_author = get_field( 'book_author' );
$book_published_year = get_field( 'book_published_year' );
$book_purchase_link = get_field( 'book_purchase_link' );
if ( $book_price || $book_author || $book_published_year || $book_purchase_link ) {
echo '<div class="book-meta">';
if ( $book_price ) {
echo '<p><strong>Price</strong>: $' . $book_price . '</p>';
}
if ( $book_author ) {
echo '<p><strong>Author</strong>: ' . $book_author . '</p>';
}
if ( $book_published_year ) {
echo '<p><strong>Year Published</strong>: ' . $book_published_year . '</p>';
}
if ( $book_purchase_link ) {
echo '<a href="' . $book_purchase_link . '" class="button">Buy this book</a>';
}
echo '</div>';
}
}
//* [All Book pages] Show Genre custom taxonomy terms for Books CPT single pages, archive page and Genre taxonomy term pages
add_filter( 'genesis_post_meta', 'custom_post_meta' );
function custom_post_meta( $post_meta ) {
if ( is_singular( 'books' ) || is_post_type_archive( 'books' ) || is_tax( 'genre' ) ) {
$post_meta = '[post_terms taxonomy="genre" before="Genre: "]';
}
return $post_meta;
}
/**
* [All Book pages] Display Post meta only if the entry has been assigned to any Genre term
* Removes empty markup, '<p class="entry-meta"></p>' for entries that have not been assigned to any Genre
*/
function sk_custom_post_meta() {
if ( has_term( '', 'genre' ) ) {
genesis_post_meta();
}
}
/**
* [WordPress] Template Redirect
* Use archive-books.php for Genre taxonomy archives.
*/
add_filter( 'template_include', 'sk_template_redirect' );
function sk_template_redirect( $template ) {
if ( is_tax( 'genre' ) )
$template = get_query_template( 'archive-books' );
return $template;
}
//* [Single Book pages] Custom Primary Sidebar for single Book entries
genesis_register_sidebar( array(
'id' => 'primary-sidebar-book',
'name' => 'Primary Sidebar - Book',
'description' => 'This is the primary sidebar for Book CPT entry'
) );
<?php
// Force content-sidebar layout setting
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_content_sidebar' );
//* Reposition Breadcrumbs
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
add_action( 'genesis_before_content', 'genesis_do_breadcrumbs' );
//* Remove the entry meta in the entry header
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
add_action( 'genesis_entry_content', 'sk_show_featured_image_single_book_pages', 9 );
/**
* Display Featured Image floated to the right in single Posts.
*
* @author Sridhar Katakam
* @link http://sridharkatakam.com/how-to-display-featured-image-in-single-posts-in-genesis/
*/
function sk_show_featured_image_single_book_pages() {
$image_args = array(
'size' => 'medium',
'attr' => array(
'class' => 'alignleft',
),
);
genesis_image( $image_args );
}
add_action( 'genesis_before_sidebar_widget_area', 'sk_display_custom_fields' );
/**
* One line description with a period at the end.
*
* @author Sridhar Katakam
* @link http://sridharkatakam.com/
*/
add_action( 'genesis_entry_content', 'sk_add_buy_button' );
function sk_add_buy_button() {
if ( get_field( 'book_purchase_link' ) ) {
echo '<a href="' . get_field( 'book_purchase_link' ) . '" class="button">Click here to buy</a>';
}
}
//* Previous and Next Post navigation
add_action('genesis_after_content_sidebar_wrap', 'sk_custom_post_nav');
function sk_custom_post_nav() {
echo '<div class="prev-next-post-links">';
previous_post_link('<div class="previous-post-link">&laquo; %link</div>', '<strong>%title</strong>' );
next_post_link('<div class="next-post-link">%link &raquo;</div>', '<strong>%title</strong>' );
echo '</div>';
}
add_action( 'genesis_after_header', 'sk_change_genesis_primary_sidebar' );
/**
* Show custom Primary sidebar in Primary Sidebar location.
*
* @author Sridhar Katakam
* @link http://sridharkatakam.com/
*/
function sk_change_genesis_primary_sidebar() {
if( is_active_sidebar( 'primary-sidebar-book' ) ) {
// Remove the Primary Sidebar from the Primary Sidebar area.
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // for when Genesis Simple Sidebars is active
add_action( 'genesis_sidebar', 'sk_do_sidebar' );
}
}
function sk_do_sidebar() {
dynamic_sidebar( 'primary-sidebar-book' );
}
//* To remove empty markup, '<p class="entry-meta"></p>' for entries that have not been assigned to any Genre
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
add_action( 'genesis_entry_footer', 'sk_custom_post_meta' );
genesis();
/* Single and Archive templates for Books CPT
----------------------------------------------- */
.button:hover {
color: #fff;
}
.type-books .entry-meta {
border-top: none;
margin: 0;
padding: 0;
text-align: right;
}
/* books CPT archive page */
.post-type-archive-books .book-meta p {
margin-bottom: 5px;
}
.post-type-archive-books .book-meta .button {
padding: 8px 15px;
font-size: 13px;
letter-spacing: 1px;
margin-top: 10px;
margin-bottom: 10px;
}
.post-type-archive-books .cpt-archive-description {
background: #fff url(images/book-shelf.png) no-repeat right bottom;
}
/* books CPT single pages */
.single-books .book-meta {
background: #a1d1db;
color: #111;
padding: 40px;
margin-bottom: 40px;
border-radius: 3px;
}
.single-books .entry-title, .post-type-archive-books .entry-title {
margin-bottom: 30px;
}
.prev-next-post-links {
overflow: hidden;
margin-bottom: 40px;
clear: both;
}
.previous-post-link {
float: left;
}
.next-post-link {
float: right;
}
@media only screen and (max-width: 568px) {
.type-books .entry-content {
margin-top: 20px;
}
}
@media only screen and (max-width: 320px) {
.type-books .entry-content img.alignleft {
float: none;
}
}
@chellestein
Copy link

Very good! Thank you :)

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