2016-07-18 2 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_title` 대신`get_the_title`을`the_excerpt` 대신`get_the_excerpt`을 사용하고 있습니다 뜻이

//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

을보십시오. 내 코드를 사용해 볼 수 있습니까? –