Last active
February 10, 2017 13:08
-
-
Save robincornett/7028494 to your computer and use it in GitHub Desktop.
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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