filter to replace header image when visiting a term archive

This commit is contained in:
Leo Germani 2018-04-21 22:49:59 -03:00
parent 59dab1aee0
commit cd2b9b6403
4 changed files with 63 additions and 8 deletions

View File

@ -14,6 +14,7 @@ class Term extends Entity {
$parent,
$description,
$user,
$header_image_id,
$taxonomy;
@ -125,6 +126,15 @@ class Term extends Entity {
function get_taxonomy() {
return $this->get_mapped_property('taxonomy');
}
/**
* Get Header Image ID attribute
*
* @return string
*/
function get_header_image_id() {
return $this->get_mapped_property( 'header_image_id' );
}
// Setters
@ -173,6 +183,17 @@ class Term extends Entity {
$this->set_mapped_property('taxonomy', $value);
}
/**
* Set Header Image ID
*
* @param [string] $value
*
* @return void
*/
function set_header_image_id( $value ) {
$this->set_mapped_property( 'header_image_id', $value );
}
/**
*

View File

@ -79,6 +79,15 @@ class Terms extends Repository {
'on_error' => __('The user is empty or invalid', 'tainacan'),
'default' => get_current_user_id(),
'validation' => v::numeric(),
],
'header_image_id' => [
'map' => 'termmeta',
'title' => __('Header Image', 'tainacan'),
'type' => 'string',
'description'=> __('The image to be used in term header', 'tainacan'),
'on_error' => __('Invalid image', 'tainacan'),
//'validation' => v::numeric(),
'default' => ''
],
'hide_empty' => [
'map' => 'hide_empty',

View File

@ -199,16 +199,26 @@ class Theme_Helper {
function header_image($image) {
$object = false;
if ($collection_id = tainacan_get_collection_id()) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_id);
$header_image = $collection->get_header_image_id();
if (is_numeric($header_image)) {
$src = wp_get_attachment_image_src($header_image, 'full');
if (is_array($src)) {
$image = $src[0];
}
$object = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_id);
} elseif ($term = tainacan_get_term()) {
$object = \Tainacan\Repositories\Terms::get_instance()->fetch($term->term_id, $term->taxonomy);
}
if (!$object)
return $image;
$header_image = $object->get_header_image_id();
if (is_numeric($header_image)) {
$src = wp_get_attachment_image_src($header_image, 'full');
if (is_array($src)) {
$image = $src[0];
}
}
return $image;
}

View File

@ -40,5 +40,20 @@ function tainacan_the_metadata($field = null, $hide_empty = true) {
* @uses get_post_type() WordPress function, which looks for the global $wp_query variable
*/
function tainacan_get_collection_id() {
return Repositories\Collections::get_instance()->get_id_by_db_identifier(get_post_type());
if ( is_post_type_archive() || is_single() ) {
return Repositories\Collections::get_instance()->get_id_by_db_identifier(get_post_type());
}
return false;
}
/**
* When visiting a term archive, returns the current term object
*
* @return false|\WP_Term
*/
function tainacan_get_term() {
if ( is_tax() ) {
return get_queried_object();
}
return false;
}