Php 显示包含其类别的文章
我有以下代码:Php 显示包含其类别的文章,php,wordpress,Php,Wordpress,我有以下代码: function x_shortcode_recent_posts_v2( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => 'post', 'count' => '', 'category'
function x_shortcode_recent_posts_v2( $atts ) {
extract( shortcode_atts( array(
'id' => '',
'class' => '',
'style' => '',
'type' => 'post',
'count' => '',
'category' => '',
'offset' => '',
'orientation' => '',
'no_image' => '',
'fade' => ''
), $atts, 'x_recent_posts' ) );
$allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
$type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';
$id = ( $id != '' ) ? 'id="' . esc_attr( $id ) . '"' : '';
$class = ( $class != '' ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
$style = ( $style != '' ) ? 'style="' . $style . '"' : '';
$count = ( $count != '' ) ? $count : 3;
$category = ( $category != '' ) ? $category : '';
$category_type = ( $type == 'post' ) ? 'category_name' : 'portfolio-category';
$offset = ( $offset != '' ) ? $offset : 0;
$orientation = ( $orientation != '' ) ? ' ' . $orientation : ' horizontal';
$no_image = ( $no_image == 'true' ) ? $no_image : '';
$fade = ( $fade == 'true' ) ? $fade : 'false';
$js_params = array(
'fade' => ( $fade == 'true' )
);
$data = cs_generate_data_attributes( 'recent_posts', $js_params );
$output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";
$q = new WP_Query( array(
'orderby' => 'date',
'post_type' => "{$type}",
'posts_per_page' => "{$count}",
'offset' => "{$offset}",
"{$category_type}" => "{$category}"
) );
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
if ( $no_image == 'true' ) {
$image_output = '';
$image_output_class = 'no-image';
} else {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
$bg_image = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
$image_output = '<div class="x-recent-posts-img"' . $bg_image . '>'.the_category(', ').'</div>';
$image_output_class = 'with-image';
}
$output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="#" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), the_title_attribute( 'echo=0' ) ) ) . '">'
. '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
. '<div class="entry-wrap">'
. $image_output
. '<div class="x-recent-posts-content">'
.'<p>'.the_category().'</p>'
. '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
. '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
. '<span class="x-recent-posts-excerpt">' . substr(get_the_excerpt(), 0,150) . '</span>'
. '</div>'
. '</div>'
. '</article>'
. '</a>';
endwhile; endif; wp_reset_postdata();
$output .= '</div>';
return $output;
}
function x\u shortcode\u recent\u posts\u v2($atts){
提取(短码)附件(数组)(
“id'=>”,
“类”=>“”,
'样式'=>'',
'type'=>'post',
'计数'=>'',
'类别'=>'',
'偏移量'=>'',
'方向'=>'',
'无图像'=>'',
“褪色”=>“
),$atts,'x_最近的帖子');
$allowed_post_types=应用_过滤器('cs_recent_post_post_types',数组('post'=>'post');
$type=(isset($allowed_post_types[$type])?$allowed_post_types[$type]:'post';
$id=($id!=“”)?'id=“”.esc_attr($id)。“:”;
$class=($class!='')“x-recent-posts cf”.esc_attr($class):“x-recent-posts cf”;
$style=($style!='')?'style=“.”.$style.“:”;
$count=($count!='')?$count:3;
$category=($category!='')?$category:'';
$category_type=($type='post')?'category_name':'portfolio category';
$offset=($offset!='')?$offset:0;
$orientation=($orientation!='')。$orientation:'horizontal';
$no_image=($no_image=='true')?$no_image:'';
$fade=($fade='true')?$fade:'false';
$js_params=数组(
'fade'=>($fade=='true')
);
$data=cs_生成_数据_属性('recently_posts',$js_params);
$output=“”;
$q=新的WP\U查询(数组(
'orderby'=>'date',
'post_type'=>“{$type}”,
'每页发布'=>“{$count}”,
'offset'=>“{$offset}”,
“{$category\u type}”=>“{$category}”
) );
如果($q->have_posts()):而($q->have_posts()):$q->the_post();
如果($no_image=='true'){
$image_输出=“”;
$image_output_class='no image';
}否则{
$image=wp_get_attachment_image_src(get_post_thumbnail_id(),'entry crapped');
$bg_image=($image[0]!='')?'style=“背景图像:url('.$image[0]);”:'';
$image_output='';
$image_output_class='with image';
}
$output.='';
endwhile;endif;wp_reset_postdata();
$output.='';
返回$output;
}
我想做的是将每篇文章的分类显示在x-recent-posts-img
div中
我已经尝试获取\u类别(),但它不起作用,现在我正在使用\u类别,但没有显示任何内容
谢谢你的帮助 我猜,因为您对类别使用变量,您有自定义分类法,请使用
get_the_terms()
我猜,因为您对类别使用变量,您有自定义分类法,请使用get_the_terms()