Changing the Genesis Archive Page Settings for Custom Post Types

By default all archive pages and types in Genesis share the same archive settings as set in the Genesis > Theme Settings > Content Archives section, but you can change these settings for a specific custom post type archive and leave the defaults intact for the regular archive pages.

genesis-archive-setting-custom-post-type

 

Looking at the Content Archives in Theme Settings you can discover the field setting name by inspecting the element on each field which will give you the option name or key.  So the below covers the fields that we see in the theme settings…

  • Display = content_archive
  • Limit content to = content_archive_limit
  • Featured Image = archive_thumbnail
  • Featured Image size =image_size
  • Featured Image alignment = image_aligment
  • Entry Pagination = posts_nav

These keys have values which are either a set of defined values in a dropdown list, an integer value or an on/off checkbox, Genesis provides a filter which allows us to change the value, it is called genesis_pre_get_option_{$key}.

This is from genesis/lib/functions/options.php file…

 //* Allow child theme to short-circuit this function
 $pre = apply_filters( "genesis_pre_get_option_{$key}", null, $setting );
 if ( null !== $pre ) {
 return $pre;
 }

The {key} takes on the themes value such as ‘content_archive‘ for example…

genesis_pre_get_option_content_archive

So we can use this filter to alter the custom post type archive settings by a conditional (this example uses the CPT testimonial) whilst leaving the default archive settings intact.

<?php
// Content Archive Full or Excerpt
add_filter( 'genesis_pre_get_option_content_archive','cpt_change_content' );
function cpt_change_content() {
if ( is_post_type_archive( 'testimonial' ) ) { // Add in your CPT
return 'full'; // Other values - 'excerpt'
}
}
// Content amount limited in characters to display
add_filter( 'genesis_pre_get_option_content_archive_limit','cpt_change_content_limit' );
function cpt_change_content_limit() {
if ( is_post_type_archive( 'testimonial' ) ) {
return 28; // Runs in accordance with 'full' set on content archive - returns limited character count
}
}
// Show the featured image
add_filter( 'genesis_pre_get_option_content_archive_thumbnail','cpt_change_image_presence' );
function cpt_change_image_presence() {
if ( is_post_type_archive( 'testimonial' ) ) {
return 1; // 1 = display it, 0 = omit it
}
}
// Size of the image
add_filter( 'genesis_pre_get_option_image_size','cpt_change_image_size' );
function cpt_change_image_size() {
if ( is_post_type_archive( 'testimonial' ) ) {
return 'thumbnail'; // Other values are any set image sizes as well as thumbnail, medium and large
}
}
// Alignment of the image
add_filter( 'genesis_pre_get_option_image_alignment','cpt_change_image_align' );
function cpt_change_image_align() {
if ( is_post_type_archive( 'testimonial' ) ) {
return 'alignright'; // Other values alignleft and none
}
}
// Pagination, numeric or text
add_filter( 'genesis_pre_get_option_posts_nav','cpt_change_pagination' );
function cpt_change_pagination() {
if ( is_post_type_archive( 'testimonial' ) ) {
return 'prev-next'; // Other value is 'numeric'
}
}
view raw settings-cpt.php hosted with ❤ by GitHub

You can either add these to your functions.php or make a plugin, if a setting is not used on the Custom Post Type then it will inherit the default content archive setting from the regular theme setting.

The code can be tidied up a bit if you are going to alter a lot of the options, you can bundle all the options into one conditional and use a hook to fire it, the genesis_before_loop is used in this example below.

<?php
add_action('genesis_before_loop','cpt_change_all');
function cpt_change_all() {
if ( is_post_type_archive( 'testimonial' ) ) {
add_filter( 'genesis_pre_get_option_content_archive','cpt_change_content' );
function cpt_change_content() {
return 'full';
}
add_filter( 'genesis_pre_get_option_content_archive_limit','cpt_change_content_limit' );
function cpt_change_content_limit() {
return 28;
}
add_filter( 'genesis_pre_get_option_content_archive_thumbnail','cpt_change_image_presence' );
function cpt_change_image_presence() {
return 1;
}
add_filter( 'genesis_pre_get_option_image_size','cpt_change_image_size' );
function cpt_change_image_size() {
return 'thumbnail';
}
add_filter( 'genesis_pre_get_option_image_alignment','cpt_change_image_align' );
function cpt_change_image_align() {
return 'alignright';
}
add_filter( 'genesis_pre_get_option_posts_nav','cpt_change_pagination' );
function cpt_change_pagination() {
return 'prev-next';
}
}
}
view raw one-action.php hosted with ❤ by GitHub

Even better is to create an archive template for that CPT and just add the filters straight in that…

<?php
// Archive Template - archive-custom-post-type-name.php
add_filter( 'genesis_pre_get_option_content_archive','cpt_change_content' );
function cpt_change_content() {
return 'full';
}
add_filter( 'genesis_pre_get_option_content_archive_limit','cpt_change_content_limit' );
function cpt_change_content_limit() {
return 28;
}
add_filter( 'genesis_pre_get_option_content_archive_thumbnail','cpt_change_image_presence' );
function cpt_change_image_presence() {
return 1;
}
add_filter( 'genesis_pre_get_option_image_size','cpt_change_image_size' );
function cpt_change_image_size() {
return 'thumbnail';
}
add_filter( 'genesis_pre_get_option_image_alignment','cpt_change_image_align' );
function cpt_change_image_align() {
return 'alignright';
}
add_filter( 'genesis_pre_get_option_posts_nav','cpt_change_pagination' );
function cpt_change_pagination() {
return 'prev-next';
}
genesis();
view raw archive.php hosted with ❤ by GitHub

Page Layout and Breadcrumbs

A couple of other Genesis theme settings that you are more likely to change for your Custom Post Type Archives are page layouts and breadcrumbs that are also in the Genesis theme settings.

Breadcrumbs

Breadcrumbs has  checkbox options on/off, one for archive and one for single posts…

  • breadcrumb_single
  • breadcrumb_archive

Page Layout

Page layout has 1 option/key …

  • site_layout

With 6 possible values

  • full-width-content
  • content-sidebar
  • sidebar-content
  • content-sidebar-sidebar
  • sidebar-content-sidebar
  • sidebar-sidebar-content

Both of these can be used with an inline function instead of creating a separate function – since the breadcrumbs is a boolean value; either true/false it can use the WordPress  __return_true function and Genesis comes with site layout inline functions for the corresponding layout…

  • __genesis_return_full_width_content
  • __genesis_return_content_sidebar 
  • __genesis_return_sidebar_content
  • __genesis_return_content_sidebar_sidebar 
  • __genesis_return_sidebar_content_sidebar
  • __genesis_return_sidebar_sidebar_content

So they can also be used in the archive template
(For the setting for breadcrumbs on the single CPT posts, you would either create a single CPT template and add it or via the functions.php as illustrated previously)

<?php
// Archive Template - archive-custom-post-type-name.php
// Use breadcrumbs
add_filter( 'genesis_pre_get_option_breadcrumb_archive','__return_true' );
// Set layout to full-width
add_filter( 'genesis_pre_get_option_site_layout','__genesis_return_full_width_content' );
genesis();
view raw other.php hosted with ❤ by GitHub

Also handy are a couple of WordPress filters to control post excerpt and post display

Controlling Post Excerpts

Apart from showing the full content and content limitation using, you can show the excerpts instead and you can control the excerpt amount (normally 55 words) using the WordPress filter excerpt_length

Controlling Amount of Posts to Display

To set a specific amount of posts to display without changing the core blog posts amount and just changing for the CPT archive you can use the action pre_get_posts with a couple of conditionals to target the front end, main WP query and CPT.

<?php
// Archive Template - archive-custom-post-type-name.php
// Modify the length of post excerpts
add_filter( 'excerpt_length', 'themeprefix_excerpt_length', 999 );
function themeprefix_excerpt_length( $length ) {
return 10; // pull first 10 words
}
// Set post display amount
add_action( 'pre_get_posts', 'set_posts_per_page_for_testimonials_cpt' );
function set_posts_per_page_for_testimonials_cpt( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'testimonial' ) ) {
$query->set( 'posts_per_page', '3' );
}
}
genesis();
view raw other2.php hosted with ❤ by GitHub

That’s it – I tend to find that making archive templates is an easier and cleaner approach and just adding the custom settings in.

Leave a Comment





%d bloggers like this: