WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting custom taxonomy link (3 posts)

  1. Minkowski
    Member
    Posted 4 years ago #

    Hi.

    I have this snippet of code but don't know how to get the permalink for the taxonomies where I've placed a #.

    <?php $args = array('taxonomy' => 'news'); ?>
    <?php $tax_menu_items = get_categories( $args );
    foreach ( $tax_menu_items as $tax_menu_item ):?>
    <li>
    	<a href="#">
    		<?php echo $tax_menu_item->name; ?>
    	</a>
    </li>
    <?php endforeach; ?>
    </ul>

    I've looked through the documentation and the category-template.php file but can't seem to figure it out.

    Any help would be greatly appreciated.

  2. MichaelH
    Member
    Posted 4 years ago #

    An example using get_term_link

    $taxonomy = 'news';
      $terms = get_terms( $taxonomy, '' );
      if ($terms) {
        foreach($terms as $term) {
            echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
        }
      }
  3. Minkowski
    Member
    Posted 4 years ago #

    Aha, thanks very much! And quick too!

    My final code for this is…

    <ul>
    <?php $args = array('taxonomy' => 'news'); ?>
    <?php $tax_menu_items = get_categories( $args );
    foreach ( $tax_menu_items as $tax_menu_item ):?>
    <li>
    	<a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
    		<?php echo $tax_menu_item->name; ?>
    	</a>
    </li>
    <?php endforeach; ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags