Skip to content

Instantly share code, notes, and snippets.

@anneallen
Created July 14, 2013 22:56
Show Gist options
  • Save anneallen/5996453 to your computer and use it in GitHub Desktop.
Save anneallen/5996453 to your computer and use it in GitHub Desktop.
Add Images to Taxonomies
/******Step 1, Add to Functions.php **************
**************************************************/
add_action( 'admin_init', 'custom_add_taxonomy_archive_options' );
/**
* Loop through the custom taxonomies and add the archive options to each
* custom taxonomy edit screen.
*
* @category Genesis
* @package Admin
* @subpackage Term-Meta
*
* @since 1.6.0
*
* @see genesis_taxonomy_archive_options() Add Disyplay title / description checkboxes
*/
function custom_add_taxonomy_archive_options() {
foreach ( get_taxonomies( array( 'show_ui' => true ) ) as $tax_name ) {
remove_action($tax_name . '_edit_form', 'genesis_taxonomy_archive_options', 10, 2 );
add_action( $tax_name . '_edit_form', 'custom_taxonomy_archive_options', 10, 2 );
}
}
/**
* Add new fields for display on archives.
*
* Displays on the category / tag edit forms.
* The variables $tag and $taxonomy are passed via the hook so that we can use
* them.
*
* @category Genesis
* @package Admin
* @subpackage Term-Meta
*
* @since 1.6.0
*
* @see genesis_add_taxonomy_archive_options() Callback caller
*
* @param string $tag Name of the term
* @param string $taxonomy Name of the taxnomy
*/
function custom_taxonomy_archive_options( $tag, $taxonomy ) {
$tax = get_taxonomy( $taxonomy );
?>
<h3><?php echo esc_html( $tax->labels->singular_name ) . ' ' . __( 'Archive Settings', 'genesis' ); ?></h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top"><label for="meta[headline]"><?php _e( 'Archive Headline', 'genesis' ); ?></label></th>
<td>
<input id="meta[headline]" name="meta[headline]" type="text" value="<?php echo esc_attr( $tag->meta['headline'] ); ?>" size="40" />
<p class="description"><?php _e( 'Leave empty if you do not want to display a headline.', 'genesis' ); ?></p>
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="meta[intro_text]"><?php _e( 'Archive Intro Text', 'genesis' ); ?></label></th>
<td>
<textarea id="meta[intro_text]" class="widefat" name="meta[intro_text]" rows="5" cols="30"><?php echo esc_textarea( $tag->meta['intro_text'] ); ?></textarea>
<p class="description"><?php _e( 'Leave empty if you do not want to display any intro text.', 'genesis' ); ?></p>
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="meta[archive_image_url]"><?php _e( 'Archive Image', 'genesis' ); ?></label></th>
<td>
<input id="meta[archive_image_url]" name="meta[archive_image_url]" type="text" value="<?php echo esc_url( $tag->meta['archive_image_url'] ); ?>" size="40" />
<p class="description"><?php _e( 'Leave empty if you do not want to display a archive image.', 'genesis' ); ?></p>
</td>
</tr>
</tbody>
</table>
<?php
}
add_filter( 'genesis_term_meta', 'custom_term_meta', 10, 3);
function custom_term_meta($value, $term, $taxonomy) {
if ( !isset ($value['archive_image_url'] )) {
$value['archive_image_url'] = '';
}
return $value;
}
remove_action( 'genesis_before_loop', 'genesis_do_taxonomy_title_description', 15 );
add_action( 'genesis_before_loop', 'custom_do_taxonomy_title_description', 15 );
/**
* Add custom headline and / or description to category / tag / taxonomy archive pages.
*
* If the page is not a category, tag or taxonomy term archive, or we're not on
* the first page, or there's no term, or no term meta set, then nothing extra
* is displayed.
*
* If there's a title to display, it is marked up as a level 1 heading.
* If there's a description to display, it runs through wpautop() before being
* added to a div.
*
* @since 1.3.0
*
* @global WP_Query $wp_query
* @return null Returns null if not the correct achive page, not page 1, or no
* term meta is set.
*/
function custom_do_taxonomy_title_description() {
global $wp_query;
if ( ! is_category() && ! is_tag() && ! is_tax() )
return;
if ( get_query_var( 'paged' ) >= 2 )
return;
$term = is_tax() ? get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ) : $wp_query->get_queried_object();
if ( ! $term || ! isset( $term->meta ) )
return;
$headline = $intro_text = '';
if ( $term->meta['headline'] )
$headline = sprintf( '<h1>%s</h1>', $term->meta['headline'] );
if ( $term->meta['intro_text'] )
$intro_text = wpautop($term->meta['intro_text']);
if ( $term->meta['archive_image_url'] )
$imageUrl = $term->meta['archive_image_url'];
if ( $headline || $intro_text || $imageUrl) {
printf ('<div class="taxonomy-description">%s<div class="category-description-content">' , $headline);
if ( !empty($imageUrl) ) {
printf( '<img src="%s"></img>', $imageUrl );
}
echo $intro_text;
echo '</div></div>';
}
}
/**
* Add term meta to results of get_terms
* See /genesis/lib/functions/options.php for more info
*
*
* Genesis is forced to create its own term-meta data structure in
* the options table. Therefore, the following function merges that
* data into the term data structure, via a filter.
*
* @param array $terms
* @param string $taxonomy Taxonomy name that $terms are part of.
* @param array $args
* @return array $terms*/
function be_get_terms_filter( $terms, $taxonomy, $args ) {
foreach( $terms as $term )
$term = genesis_get_term_filter( $term, $taxonomy );
return $terms;
}
add_filter( 'get_terms', 'be_get_terms_filter', 10, 3 );
function get_the_term_image_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
/******Step 2 Shortcode to return clickable image+tax-name+text(tax-term) **********
************************************************************************************/
function custom_do_taxonomy_title_image ($atts){
extract(shortcode_atts(array(
'tname'=>'category',
'tnicename'=>'category',
), $atts));
$term = get_term_by( 'name', strip_tags(do_shortcode('[post_terms before="" taxonomy="'.$tname.'"]')), $tname );
if ( ! $term || ! isset( $term->meta ) )
return;
$headline = $intro_text = '';
if ( $term->meta['archive_image_url'] )
$imageUrl = $term->meta['archive_image_url'];
$urls= $term->slug;
if ( $term->name )
$headline = sprintf( '<span class="terms">%s</span>', $term->name);
$link = get_term_link( $term, $tname );
if ( $headline || $intro_text || $imageUrl) {
$return_string .='<a href="' . esc_url( $link ) . '" rel="tag">';
if ( !empty($imageUrl) ) {
$return_string .='<div class="tax-image"><img src="'.$imageUrl.'"></img></div>';
}
$return_string .= '<div class="tax-name">'.$tnicename.'</div>
<div class="tax-term-text">'.$headline.'</div></a>';
}
return $return_string;
}
add_shortcode('tax-terms-image', 'custom_do_taxonomy_title_image');
@anneallen
Copy link
Author

Probably not the best way to do this - any comments welcome!

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