Skip to content

Instantly share code, notes, and snippets.

@bnecreative
Last active October 20, 2022 20:06
Show Gist options
  • Save bnecreative/327a6603bf74e2173e8283cd19bac943 to your computer and use it in GitHub Desktop.
Save bnecreative/327a6603bf74e2173e8283cd19bac943 to your computer and use it in GitHub Desktop.
Sweetness - Edit page heading wrapper background image and add category images
<?php //Don't copy this line...
/*
* Taxonomy CMB2 Fields
*
* Add custom fields to the Post Taxonomy
*
*/
add_action( 'cmb2_admin_init', function() {
$cmb_tax = new_cmb2_box( array(
'id' => 'bne_category_meta',
'object_types' => array( 'term' ),
'taxonomies' => array( 'category' ),
'new_term_section' => true,
) );
// Field
$cmb_tax->add_field( array(
'name' => __( 'Thumbnail', 'bne' ),
'desc' => __( 'May be used in various places such as the category archive page.', 'bne' ),
'id' => 'bne_category_image',
'type' => 'file',
'options' => array(
'url' => false,
'add_upload_files_text' => __( 'Add or Upload Image', 'bne' ),
),
'column' => true,
) );
});
/*
* Page Heading Background Properties
*
* Allows you to adjust the default background image used within this area on custom post types and taxonomies.
* Requires the Page Headings option set to above in Theme Options.
*
*/
add_filter( 'bne_page_heading_bg', function( $background_css ) {
// Post Category
if( is_category() ) {
$term_id = get_queried_object_id();
if( $term_id ) {
$term_image = get_term_meta( $term_id, 'bne_category_image', true );
if( $term_image ) {
$background_css['background-image'] = $term_image;
}
}
}
// Single Blog Post
if( is_single() ) {
if( has_post_thumbnail() ) {
$image = get_the_post_thumbnail_url( get_the_id(), 'full' );
$background_css['background-image'] = $image;
}
}
return $background_css;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment