php - 简码获取所有帖子[显示帖子的类别问题]
问题描述
我尝试通过以下链接做一个简码来获取所有帖子
我的短代码放在子主题的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' );
想在每个帖子中显示帖子的类别名称和类别链接( 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文档中所述,您可以为该函数设置帖子 ID:
get_the_category( int $id = false )
页面中的一个小示例: Get the Post Categories From Outside the Loop
因此,我建议您可以像这样将帖子 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 循环会将所有类别(名称和链接)添加到每个帖子,因为您正在对所有已发布的类别进行交互。
推荐阅读
- c# - 将 JToken 转换为 JArray
- excel - 寻找 VBA 代码以将未保存的 Excel 工作簿合并到合并的工作簿中
- heroku - Heroku云上的scrapy+splash
- javascript - 使用 peggy js 使用 JavaScript 解析表达式
- reactjs - 如何在 Next.js 应用程序中拥有不同的 env 文件?
- python - 有两个数据框,如何创建第三个
- javascript - 如何使用 Chart.js 从图表中删除底线
- javascript - Vue: Access component object inside methods
- python-3.x - 无法在 Python 中读取大型机生成的文件
- r - R:pairwise.t.test 在一个缺失值的长格式数据帧上,使用一个命令