Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active December 12, 2015 02:38
Show Gist options
  • Save robneu/4700379 to your computer and use it in GitHub Desktop.
Save robneu/4700379 to your computer and use it in GitHub Desktop.
An inefficient section of a page template
<?php
/** Return the first taxonomy term name */
function strays_tax_terms( $taxonomy, $term ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
$term = array_pop($terms);
if ( !is_wp_error( $term ) ) {
return $term->name;
}
}
/** Get the taxonomy term names for taxonomies in pets-cpt.php */
$pet_breed = strays_tax_terms( 'breeds', $term );
$pet_color = strays_tax_terms( 'colors', $term );
$pet_size = strays_tax_terms( 'pet_sizes', $term );
$pet_born = get_post_meta($post->ID, 'scfs_born', true);
$pet_age = strays_tax_terms( 'pet_ages', $term );
$pet_sex = strays_tax_terms( 'pet_sexes', $term );
/** Return the first taxonomy term slug */
function strays_tax_links( $taxonomy, $term ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
$term = array_pop($terms);
if ( !is_wp_error( $term ) ) {
return $term->slug;
}
}
/** Get the taxonomy term slugs for taxonomies in pets-cpt.php */
$link = get_bloginfo( 'url' );
$breed_slug = $link . '/cat-breeds/' . strays_tax_links( 'breeds', $term ) . '/';
$color_slug = $link . '/cat-colors/' . strays_tax_links( 'colors', $term ) . '/';
$size_slug = $link . '/sizes/' . strays_tax_links( 'pet_sizes', $term ) . '/';
$age_slug = $link . '/ages/' . strays_tax_links( 'pet_ages', $term ) . '/';
$sex_slug = $link . '/genders/' . strays_tax_links( 'pet_sexes', $term ) . '/';
?>
<span class="breed">Breed - <a href="<?php echo esc_html( $breed_slug ) ?>"><?php echo esc_html( $pet_breed )?></a></span>
<span class="color">Color - <a href="<?php echo esc_html( $color_slug ) ?>"><?php echo esc_html( $pet_color ) ?></a></span>
<span class="size">Size - <a href="<?php echo esc_html( $size_slug ) ?>"><?php echo esc_html( $pet_size ) ?></a></span>
<span class="age">Age - <a href="<?php echo esc_html( $age_slug ) ?>"><?php echo esc_html( $pet_age ) ?></a></span>
<span class="sexes">Sex - <a href="<?php echo esc_html( $sex_slug ) ?>"><?php echo esc_html( $pet_sex ) ?></a></span>
<span class="born">Born - <?php echo esc_html( $pet_born )?></span>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment