Php 带有分类子项的自定义帖子类型
通过登陆,我们展示了分类。单击任何类别可显示子类别。但这也显示了孩子的孩子。下面是我们检索子类别的代码Php 带有分类子项的自定义帖子类型,php,wordpress,parent-child,custom-post-type,custom-taxonomy,Php,Wordpress,Parent Child,Custom Post Type,Custom Taxonomy,通过登陆,我们展示了分类。单击任何类别可显示子类别。但这也显示了孩子的孩子。下面是我们检索子类别的代码 $terms = get_terms( 'msproduct' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ echo '<ul>'; foreach ( $terms as $term ) { echo '<li>' . $term->name .
$terms = get_terms( 'msproduct' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
你也许可以用这个。我不确定你的结构是什么,但是假设你在
上的页面是_tax()
,那么检查这个条件就可以了
$taxonomy = "msproduct";
$args = (is_tax()) ? array() : array('parent' => 0); // don't show children if you're on a taxonomy page
$terms = get_terms( $taxonomy, $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
$taxonomy=“msproduct”;
$args=(是否为税())?数组():数组('parent'=>0);//如果您在分类页面上,则不要显示子项
$terms=get_terms($taxonomy,$args);
如果(!empty($terms)&&!is_wp_error($terms)){
回声“”;
foreach($terms作为$term){
回显“- ”.$term->name.“
”;
}
回声“
”;
}
参考:
谢谢您的回复。商用Ovan是主要类别,它包含直接产品和子类别。如果涉及到类别,它可以有一个产品或多个产品。index.php、taxonomy-msproduct.php、single-msproduct.php是这些页面。如果它是一个直接的产品点击它会带我到一个页面与所有的细节。但是,如果它包含多个产品,那么将显示一个产品循环。这都是关于
$taxonomy = "msproduct";
$args = (is_tax()) ? array() : array('parent' => 0); // don't show children if you're on a taxonomy page
$terms = get_terms( $taxonomy, $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}