如何在存档页面上显示自定义分类?

时间:2019-03-06 作者:shahbaz
<?php get_header();?>
<?php
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
$shahbaz = new WP_Query( 
    array(

        \'order\'         => \'ASC\',
        \'post_type\'     => \'services\',
        \'post_status\'   => \'publish\',
        \'posts_per_page\' => \'9\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'basics\',
                \'field\'    => \'taxonomy\',
                \'terms\'    => \'taxonomy\',
            ),
        ),



    ),
);
?>
<?php
$categories = get_the_terms( get_the_id(), $basics-slug );

if ( is_array( $categories ) ) {

    foreach ( $categories as $category ) {
        echo \'<a href="\' . get_term_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a> \';
    }

}
?>

<?php get_footer();?>
1 个回复
SO网友:MikeNGarrett

我只关注上面代码中的类别列表,而忽略自定义查询,因为这不是您要问的问题。

<?php
// I assume the custom taxonomy slug is "basics"
$categories = get_the_terms( get_the_ID(), \'basics\' );

// get_the_terms returns false, a wp error, or an array of term objects.
if ( $categories && ! is_wp_error( $categories ) ) {

    foreach ( $categories as $category ) {
        // get_term_link requires taxonomy slug as the second parameter.
        echo \'<a href="\' . esc_url( get_term_link( $category->term_id, \'basics\' ) ) . \'" title="\' . sprintf( esc_html__( \'View all posts in %s\' ), esc_html( $category->name ) ) . \'">\' . esc_html( $category->name ) . \'</a> \';
    }
}
?>
我调整了一些细节。内联注释应该提供一些详细信息。我还向输出中添加了转义,因为这是一种最佳实践。

相关推荐