2016-12-25 90 views
0

這是顯示類別的帖子。但它有一些錯誤,請修復它。WordPress短代碼顯示一些錯誤

這是我的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

試試這個代碼:

<?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');