获取分类ID:WordPress新手求助按分类展示CPT服务
如何根据嵌套分类展示你的自定义服务文章
嗨,兄弟,别担心,我帮你搞定这个!既然你已经创建了Servicios这个自定义文章类型(CPT),接下来我们分几步实现按分类展示服务的功能:
第一步:确认分类与CPT的关联
首先得确保你的自定义分类法已经和Servicios绑定。如果还没设置,或者不确定,你可以在注册分类法的代码里加上'post_types' => array('servicios')(这里假设你的CPT别名是servicios,如果不是就改成你实际的)。示例代码如下:
function create_servicios_taxonomy() { $args = array( 'hierarchical' => true, // 开启嵌套分类支持 'labels' => array( 'name' => __( 'Categorías de Servicios', 'textdomain' ), 'singular_name' => __( 'Categoría de Servicio', 'textdomain' ), ), 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'categoria-servicio' ), 'post_types' => array('servicios') // 关联到你的自定义文章类型 ); register_taxonomy( 'categoria_servicio', array('servicios'), $args ); } add_action( 'init', 'create_servicios_taxonomy', 0 );
第二步:创建分类专属模板页面
WordPress有模板层级规则,你可以创建一个专门的模板来展示指定分类下的服务。如果你的分类法别名是categoria_servicio,就创建一个名为taxonomy-categoria_servicio.php的文件,放到你的主题文件夹中。
这里给你两种实现方式,按需选择:
方式一:使用默认循环(简单快捷)
WordPress会自动识别当前访问的分类,加载对应内容,直接用默认循环就能快速实现:
<?php get_header(); ?> <div class="service-container"> <h1><?php single_term_title(); ?> - Servicios</h1> <?php if ( have_posts() ) : ?> <div class="service-grid"> <?php while ( have_posts() ) : the_post(); ?> <div class="service-card"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> </div> <?php endwhile; ?> </div> <?php the_posts_navigation(); ?> <?php else : ?> <p>No hay servicios disponibles en esta categoría.</p> <?php endif; ?> </div> <?php get_footer(); ?>
如果想包含子分类的服务内容,可以在模板开头添加这段代码修改查询规则:
<?php add_action( 'pre_get_posts', function( $query ) { if ( ! is_admin() && $query->is_main_query() && is_tax( 'categoria_servicio' ) ) { $query->set( 'include_children', true ); // 开启子分类内容包含 } } ); ?>
方式二:自定义WP_Query(更灵活)
如果需要自定义排序、数量限制等规则,就用这种方式:
<?php get_header(); ?> <?php $current_term = get_queried_object(); // 获取当前访问的分类对象 $args = array( 'post_type' => 'servicios', 'posts_per_page' => 10, // 每页显示10条 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'categoria_servicio', 'field' => 'term_id', 'terms' => $current_term->term_id, 'include_children' => true, // true包含子分类,false仅显示当前分类 ), ), ); $service_query = new WP_Query( $args ); ?> <div class="service-container"> <h1><?php echo $current_term->name; ?> - Servicios</h1> <?php if ( $service_query->have_posts() ) : ?> <div class="service-grid"> <?php while ( $service_query->have_posts() ) : $service_query->the_post(); ?> <div class="service-card"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php if ( has_post_thumbnail() ) : ?> <div class="service-img"> <?php the_post_thumbnail( 'medium' ); ?> </div> <?php endif; ?> <?php the_excerpt(); ?> </div> <?php endwhile; ?> </div> <?php the_posts_navigation(); ?> <?php wp_reset_postdata(); // 重置查询,避免影响后续内容 ?> <?php else : ?> <p>No hay servicios disponibles en esta categoría.</p> <?php endif; ?> </div> <?php get_footer(); ?>
第三步:测试访问
现在你可以在后台给Servicios文章分配分类(包括嵌套子分类),然后访问分类的URL(比如你的网站域名/categoria-servicio/你的分类别名/),就能看到对应分类下的服务了。
额外小技巧:添加前端分类筛选器
如果想让用户在前端手动选择分类筛选服务,可以添加一个下拉菜单:
<?php $terms = get_terms( array( 'taxonomy' => 'categoria_servicio', 'hide_empty' => true, 'hierarchical' => true, ) ); ?> <form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <select name="categoria_servicio" id="service-category-select"> <option value="">Todas las categorías</option> <?php foreach ( $terms as $term ) : ?> <option value="<?php echo $term->slug; ?>" <?php if ( isset( $_GET['categoria_servicio'] ) && $_GET['categoria_servicio'] == $term->slug ) echo 'selected'; ?>> <?php echo str_repeat( '-- ', $term->depth ) . $term->name; ?> <!-- 用--显示嵌套层级 --> </option> <?php endforeach; ?> </select> <input type="submit" value="Ver Servicios"> <input type="hidden" name="post_type" value="servicios"> </form>
用户选择分类提交后,就会跳转到对应的分类页面展示服务。
内容的提问来源于stack exchange,提问作者Lorenzo Romero
相关产品推荐
相关产品推荐

