2016-12-24 4 views
-2

サー、 私はこのカテゴリのすべての投稿とサムネイルを1番目の投稿に表示するこのコードを持っています。このコードでWordPressショートコードを作成するには

<?php $recent = new WP_Query(); ?> 
<?php $recent->query('cat=1&showposts=5'); ?> 
    <?php $is_first_post = true; ?> 
    <?php while($recent->have_posts()) : $recent->the_post(); ?> 
<ul> 
    <li> 

    <?php 
      if ($is_first_post && has_post_thumbnail()) { 
       the_post_thumbnail(); 
       $is_first_post = false; 
      } 
      ?> 
       <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
      </a> 

     </li> 
</ul> 
<?php endwhile; ?> 

しかし、これをショートコードを使用して表示します。カテゴリ&投稿番号 を使用していますが、私はショートコードを作成できません。私を助けてください。

+1

[ワードプレスショートコードを作る](http://stackoverflow.com/questions/41311453/make-wordpress-shortcode) –

答えて

0
// Add Shortcode 
function recentpost_shortcode_func() { 

    $recent = new WP_Query(); 
    $recent->query('cat=1&showposts=5'); 
    $is_first_post = true; 
    $html = ''; 
    while($recent->have_posts()) : $recent->the_post(); 
    $html .='<ul><li>'; 
    if ($is_first_post && has_post_thumbnail()) { 
    $html .=get_the_post_thumbnail(); 
    $is_first_post = false; 
    } 
    $html .='<a href="'.get_the_permalink().'">'; 
    $html .=get_the_title(); 
    $html .='</a></li></ul>'; 
    endwhile; 
    return $html; 

} 
add_shortcode('recentpost', 'recentpost_shortcode_func'); 
関連する問題