Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active September 6, 2022 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/4820d383171876a2b42331d5aa372258 to your computer and use it in GitHub Desktop.
Save damiencarbery/4820d383171876a2b42331d5aa372258 to your computer and use it in GitHub Desktop.
Hero header for category archive with Genesis (and CMB2) - Overlay category title on category image - https://www.damiencarbery.com/2019/12/hero-header-for-category-archive-with-genesis-and-cmb2/
<?php
/*
Plugin Name: Hero header for category archive with Genesis (and CMB2)
Plugin URI: https://www.damiencarbery.com/2019/12/hero-header-for-category-archive-with-genesis-and-cmb2/
Description: Overlay category title on category image.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.2
*/
add_action( 'cmb2_admin_init', 'dcwd_category_image_metabox' );
function dcwd_category_image_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'dcwd_category_image',
'title' => 'Category Hero Image',
'object_types' => array( 'term', ),
'taxonomies' => array( 'category' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$cmb->add_field( array(
'name' => 'Choose category hero image', // This is not needed here.
'desc' => 'Choose an image 1600 wide x 1067 tall.',
'type' => 'file',
'id' => 'hero_image'
) );
}
add_action( 'genesis_after_header', 'dcwd_archive_hero_image' );
function dcwd_archive_hero_image() {
// if we are not on a category archive, abort.
if ( !is_category() ) {
return;
}
global $wp_query;
$term = is_tax() ? get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ) : $wp_query->get_queried_object();
if ( ! $term ) {
return;
}
$image = get_term_meta( $term->term_id, 'hero_image', true );
?>
<div class="post-hero" style="background-image: url('<?php echo $image; ?>')">
<div class="wrap">
<?php genesis_do_taxonomy_title_description(); ?>
</div>
</div>
<?php
// Category title displayed above so remove the default location.
remove_action( 'genesis_before_loop', 'genesis_do_taxonomy_title_description', 15 );
}
// Verify that CMB2 plugin is active.
add_action( 'admin_notices', 'verify_cmb2_active' );
function verify_cmb2_active() {
if ( ! defined( 'CMB2_LOADED' ) ) {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
?>
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div>
<?php
//error_log( 'CMB2 is not active.' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment