简码 获取所有帖子 [ 显示帖子的类别问题 ]



我试图通过以下链接来做一个短代码来获取所有帖子

我的快捷代码放在子主题的functions.php文件中。:-

function myprefix_custom_grid_shortcode( $atts ) {
$atts = shortcode_atts( array(
'posts_per_page' => '-1',
'term'           => '',
), $atts, 'myprefix_custom_grid' );
extract( $atts );
$output = '';
$query_args = array(
'post_type'      => 'post', // Change this to the type of post you want to show
'posts_per_page' => $posts_per_page,
);
if ( $term ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field'    => 'ID',
'terms'    => $term,
),
);
}
// Query posts
$custom_query = new WP_Query( $query_args );
if ( $custom_query->have_posts() ) {
$output .= '<ul class="yj-trainingcamp-list clearfix">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$categories = get_the_category();
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
}
$output .= '<li class="yj-trainingcamp-item">
<a href="' . get_permalink() . '" title="' . get_the_title() . '" class="trainingcamp-atom-wrap" target="_blank">
<div class="cover-img-zone">
<div class="el-image cover-img"><img src="' .get_the_post_thumbnail_url($post->ID, 'full'). '" alt="" class="el-image__inner" style="object-fit: cover;"></div></div><div class="infos"><div class="camp-info"><div class="camp-name"><span class="camp-num">第10期</span>' . get_the_title() . '</div>
<div class="nums"><div class="sale-count">' .get_the_excerpt(). ' ' . trim($output_cats, $separator) . ' </div>
</div>
</div>
<div class="teacher-info">
<div class="teacher-img-zone">
<div class="el-image teacher-img">' . get_avatar( get_the_author_meta('user_email') , 32 ) . '
</div>
</div>
<div class="teacher-name">作者: ' .get_the_author(). ' | ' .get_the_modified_time('M j, Y'). '</div>
</div>
</div>
</a>
</li>';
}
$output .= '</ul>';
wp_reset_postdata();
}
return $output;
}
add_shortcode( 'myprefix_custom_grid', 'myprefix_custom_grid_shortcode' );

以下是显示多个div 的问题

想要在每个帖子中显示帖子的类别名称和类别链接(foreach(,但不起作用。。。以下是我怀疑的部分。。。

$categories = get_the_category();
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
}

现在还不确定,因为我无法调试它,也无法阅读图片上的文本,但正如wordpress文档中所述,您可以设置函数的post-id:

get_the_category( int $id = false )

页面中的一个小示例:从环外获取文章类别

因此,我建议您可以将post ID添加到get_the_category中,如下所示:

$categories = get_the_category(get_the_ID());
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
}

否则,我想foreach循环会将所有类别(名称和链接(添加到每个帖子中,因为您正在对所有发布的类别进行交互。

最新更新