按自定义分类法调用 wordpress post
Calling wordpress post by custom taxonomy
我在 WordPress 中定义了一个自定义分类法,如下所示:
function content_selector() {
register_taxonomy(
'contentselector',
'post',
array(
'label' => __( 'Content Selector' ),
'rewrite' => array( 'slug' => 'cs' ),
'hierarchical' => true,
)
);
}
add_action( 'init' , 'content_selector' );
它出现在新帖子中并且可以分配值,实际上它看起来很有效。但是当我使用下面的函数调用这个分类的帖子时,没有成功。
add_shortcode('rps', 'rpsf');
function rpsf() {
$args =[ 'posts_per_page' => 1, array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms')
))];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<div class="rpst">
<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span</a>
</div>
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
我是不是在定义分类法或调用帖子时出错了?
如果您正确设置代码格式并保持所用符号的一致性(例如,[]
与 array()
),调试代码会更容易。
在 'cleaning' 完成您的 $args
定义后,可以清楚地看到它的结构不正确:
$args = array(
'posts_per_page' => 1,
array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
)
);
它应该看起来更像这样:
$args = array(
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
);
我在 WordPress 中定义了一个自定义分类法,如下所示:
function content_selector() {
register_taxonomy(
'contentselector',
'post',
array(
'label' => __( 'Content Selector' ),
'rewrite' => array( 'slug' => 'cs' ),
'hierarchical' => true,
)
);
}
add_action( 'init' , 'content_selector' );
它出现在新帖子中并且可以分配值,实际上它看起来很有效。但是当我使用下面的函数调用这个分类的帖子时,没有成功。
add_shortcode('rps', 'rpsf');
function rpsf() {
$args =[ 'posts_per_page' => 1, array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms')
))];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<div class="rpst">
<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span</a>
</div>
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
我是不是在定义分类法或调用帖子时出错了?
如果您正确设置代码格式并保持所用符号的一致性(例如,[]
与 array()
),调试代码会更容易。
在 'cleaning' 完成您的 $args
定义后,可以清楚地看到它的结构不正确:
$args = array(
'posts_per_page' => 1,
array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
)
);
它应该看起来更像这样:
$args = array(
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
);