CPT =自定义帖子类型
我已经创建了一个能够在我的wordpress中完美运行的CPT。
以下是代码:
register_post_type( 'clases-tinka',
// CPT Options
array(
'labels' => array(
'name' => __( 'Academy' ),
'singular_name' => __( 'Academy' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tinka-academy'),
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ),
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'taxonomies' => array( 'academy-tax','category' ),
)
);
我在php文档中循环所有这些CPT,你可以在这里看到http://tinka.com.co/es/tinka-academy/
以下是代码:
$i = 1;
//added before to ensure it gets opened
echo '<div class="row-cursos">';
$wp_query = new WP_Query( array( 'post_type' => 'clases-tinka', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC' ));
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// post stuff...
?>
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="clasesclass">
<div><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div><a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a></div>
<div class="datosclase">
<?php
$horast = get_field('horas_del_curso');
$diast = get_field('dias_split');
$personast = get_field('cantidad_de_personas');
?>
<h4><?php if ($horast == "1") { echo "Curso de " .$horast. " Hora";} else { echo "Curso de " .$horast . " Horas";} ?></h4>
<h4><?php echo $diast ?></h4>
<h4><?php if ($personast == "1") { echo "Grupo de " . $personast ." Persona";} else { echo "Grupo de " .$personast ." Personas";} ?></h4>
<p>Category: <?php $terms = get_terms("academy-tax"); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { echo $term->name; } } ?>
</p>
</div>
<div class="botonenlace">
<a href="<?php the_permalink(); ?>" target="_self" class="tcvpb-button tcvpb-button_red tcvpb-button_rounded tcvpb-button_small ripplelink ">Ver Más<i class="typicons-media-play"></i></a>
</div>
</div>
</div>
<?php
// if multiple of 3 close div and open a new div
if($i % 3 == 0) {echo '<div style="clear:both"></div></div><div class="row-cursos"><hr>';}
$i++; endwhile; endif;
//make sure open div is closed
echo '</div>';
?>
</div> <!-- Container End -->
<div class="container">
<?php
$terms = get_the_terms( 'clases-tinka', 'category' );
// Want a list of category here
?>
</div>
我不太清楚分类法是如何运作的,我只想要列出我在该CPT中创建的类别。
答案 0 :(得分:0)
你的意思是你想要在php的底部列出每个课程类别,对吗?
嗯,首先,函数get_the_terms()只检索附加到给定帖子的术语(通过id或post对象本身)。
你应该看看get_taxonomies()功能。有了这个,您可以查询您的CPT类别。