Skip to content

Instantly share code, notes, and snippets.

@robincornett robincornett/page_tax.php
Last active Feb 10, 2017

Embed
What would you like to do?
Genesis page template to show a list of terms from a taxonomy. Add a custom field in the page editor. query_args, value is the name of your taxonomy. (updated 4/5/2016 to allow for error due to invalid taxonomy name)
<?php
/**
* Template Name: Taxonomy Listing
* This file lists taxonomies on a page. Based on http://codex.wordpress.org/Function_Reference/get_term_link
*
* @author Robin Cornett
* @link http://robincornett.com/taxonomy-list/
*/
add_action( 'genesis_entry_content', 'robin_do_taxonomy_loop' ); // Add custom loop
function robin_do_taxonomy_loop() {
$tax = 'category';
if ( genesis_get_custom_field( 'taxonomy' ) ) {
$tax = genesis_get_custom_field( 'taxonomy' ); // custom field taxonomy => name
}
$args = genesis_get_custom_field( 'query_args' ); // custom field query_args => orderby=name&order=ASC (example)
$terms = get_terms( $tax, $args );
$class = 'two-columns';
if ( '3' === genesis_get_custom_field( 'columns' ) ) {
$class = 'three-columns';
}
if ( ! is_wp_error( $terms ) ) {
echo '<ul class="' . $class . '">';
foreach ( $terms as $term ) {
echo '<li><a href="' . get_term_link( $term ) .'">' . $term->name . '</a></li>';
}
echo '</ul>';
}
else {
echo 'No taxonomy custom field was created.';
}
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.