WordPress自定义帖子类型类别链接

问题描述 投票:3回答:2

我创建了一个自定义的帖子类型videoCourse,它继承了默认类别,我正在寻找一种方法来列出我的所有类别,并链接到每个列出特定类别帖子的页面,例如:分类名为“Sciences”的链接videoCourses/Sciences

wordpress custom-post-type
2个回答
0
投票

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
投票

我到这儿谷歌搜索,所以帮助其他人登陆这里...

使用自定义帖子类型时,你可能不得不使用get_termsget_the_terms而不是get_category等。

请参阅codex:get_terms / get_the_terms(查看这些页面底部的代码片段应该有帮助)。

所以你可以使用这样的东西(从get_terms页面复制)列出所有带有术语存档链接的术语,用一个交集(·)分隔:

<?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;
}

?>
© www.soinside.com 2019 - 2024. All rights reserved.