3
votes

I created a custom post type videoCourse that inherit the default categories i am looking for a way to list all my categories with link to each a page that list posts of a specific category, Example: Categoriy named "Sciences" the link videoCourses/Sciences

2

2 Answers

0
votes

https://codex.wordpress.org/Function_Reference/get_category_link

<?php
    // Get the ID of a given category
    $category_id = get_cat_ID( 'Category Name' );

    // Get the URL of this category
    $category_link = get_category_link( $category_id );
?>

<!-- Print a link to this category -->
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
0
votes

I got here Googling, so to help anyone else landing here…

When working with custom post types you'll probably have to use get_terms or get_the_terms instead of get_category etc.

See codex here: get_terms / get_the_terms (looking at the code snippets at the bottom of those pages should help).

So you could use something like this (copied from get_terms page) to list all the terms with link to term archive, separated by an interpunct (·):

<?php

$args = array( 'hide_empty=0' );

$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    $count = count( $terms );
    $i = 0;
    $term_list = '<p class="my_term-archive">';
    foreach ( $terms as $term ) {
        $i++;
        $term_list .= '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
        if ( $count != $i ) {
            $term_list .= ' &middot; ';
        }
        else {
            $term_list .= '</p>';
        }
    }
    echo $term_list;
}

?>