哪个是节目类别帖子。但它有一些错误。请任何人修复它。
这是我的WordPress短代码
function my_form_shortcode($atts) {
ob_start();
$atts = shortcode_atts(
array(
'cat' => '1',
'showposts' => '5',
), $atts, 'my_form_shortcode' );
//YOUR CODE START
$recent = new WP_Query();
$query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
$recent->query( $query );
$is_first_post = true;
while( $recent->have_posts() ) : $recent->the_post(); ?>
<div class="panel-heading"><?php the_category(', '); ?></div>
<ul class="panel-grid">
<li class="ex">
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail( 'image', array( 'class' => 'img-responsive' ));
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile;
//YOUR CODE END
return ob_get_clean();
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
在此代码类别标题名称显示所有帖子之前。但我想在第一篇文章之前显示类别标题。这段代码出了什么问题。求你帮帮我..
答案 0 :(得分:0)
试试这个...... https://developer.wordpress.org/reference/functions/get_the_category_by_id/
function get_the_category_by_ID( $cat_ID ) {
$cat_ID = (int) $cat_ID;
$category = get_term( $cat_ID, 'category' );
if ( is_wp_error( $category ) )
return $category;
return ( $category ) ? $category->name : '';
}
&#13;
答案 1 :(得分:0)
尝试此代码:
<?php
function my_form_shortcode($atts) {
ob_start();
$atts = shortcode_atts(
array(
'cat' => '1',
'showposts' => '5',
), $atts, 'my_form_shortcode' );
//YOUR CODE START
$recent = new WP_Query();
$query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
$recent->query( $query );
$is_first_post = true; ?>
<div class="panel-heading"><?php the_category(', '); ?></div>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul class="panel-grid">
<li class="ex">
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail( 'image', array( 'class' => 'img-responsive' ));
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
</ul>
<?php endwhile;
//YOUR CODE END
return ob_get_clean();
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );