2016-07-18 26 views
1

WordPressで最新のブログ投稿を呼び出すショートコードを作成しました。 タイトルと内容を<h2><p>に改めましたが、これは適用されていません。 htmlは生成されていますが、必要なタグはありません。私は間違って何を書いていますかWordPressのショートコード問題

これは私のコードです:

<?php 

//blog posts shortcode 

function my_recent_post() 
{ 
    global $post; 

    $html = ""; 

    $my_query = new WP_Query(array(
     'post_type' => 'post', 
     'cat' => '4', 
     'posts_per_page' => 1 
)); 

    if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); 

     $html .= "<span>" . the_post_thumbnail('medium', array('class' => 'img-responsive')) . "</span>"; 
     $html .= "<h2>" . the_title() . "</h2>"; 
     $html .= "<p>" . the_excerpt() . "</p>"; 

    endwhile; endif; 

    return $html; 
} 
add_shortcode('blog', 'my_recent_post');  
?> 

答えて

3

問題は、HTMLの代わりにそれを返す印刷機能を使用していることです。 私はあなたが `the_excerpt`代わりに` get_the_excerpt`、と `the_title`代わりに` get_the_title`を使用していることを意味し、この

//blog posts shortcode 
add_shortcode('blog', 'my_recent_post');  

function my_recent_post() { 
    global $post; 

    $html = ""; 

    $my_query = new WP_Query(array(
     'post_type' => 'post', 
     'cat' => '4', 
     'posts_per_page' => 1 
    )); 

    if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); 

     $html .= "<span>" . get_the_post_thumbnail($post->ID, 'medium', array('class' => 'img-responsive')) . "</span>"; 
     $html .= "<h2>" . get_the_title() . "</h2>"; 
     $html .= "<p>" . get_the_excerpt() . "</p>"; 

    endwhile; endif; 

    return $html; 
} 
+0

を試してみてください。私のコードを使ってみることはできますか? –

関連する問題