2017-02-14 3 views
0

다음 코드를 사용하여 "educationposts"라는 카테고리의 게시물을 표시하는 WordPress 단축 코드 [educationposts]를 만듭니다.단축 코드 변수로 모든 카테고리 게시물을 가져 오는 WordPress 단축 코드 기능을 사용자 정의하는 방법은 무엇입니까?

감사

/*** show post category ***/ 
function pwp_postsbycategory() { 
$the_query = new WP_Query(array('category_name' => 'education', 'posts_per_page' => 5)); 

if ($the_query->have_posts()) { 
    $string .= '<ul class="postsbycategory widget_recent_entries">'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     if (has_post_thumbnail()) { 
      $string .= '<li>'; 
      $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(75, 75)) . get_the_title() .'</a></li>'; 
     } else { 
      // if no featured image is found 
      $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; 
     } 
    } 
} else { 
    // no posts found 
} 
$string .= '</ul>'; 

return $string; 

/* Restore original Post Data */ 
wp_reset_postdata(); 
} 
// Add a shortcode 
add_shortcode('educationposts', 'pwp_postsbycategory'); 
/*** show post category ***/ 

답변

0

내가하여 해결이 [글 = 교육]와 같은 단축 코드 또는 [글 = 블로그]를 변경하여 카테고리를 보여줄 수있는 유연성이 단축 코드 기능을 설정하는 방법

$ atts in 함수를 설정하십시오.

/*** show post category ***/ 
function pwp_postsbycategory($atts) { 
$the_query = new WP_Query(array('category_name' => $atts['cat'], 'posts_per_page' => 5)); 

if ($the_query->have_posts()) { 
    $string .= '<ul class="postsbycategory widget_recent_entries">'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     if (has_post_thumbnail()) { 
      $string .= '<li>'; 
      $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(75, 75)) . get_the_title() .'</a></li>'; 
     } else { 
      // if no featured image is found 
      $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; 
     } 
    } 
} else { 
    // no posts found 
} 
$string .= '</ul>'; 

return $string; 

/* Restore original Post Data */ 
wp_reset_postdata(); 
} 
// Add a shortcode 
add_shortcode('posts', 'pwp_postsbycategory'); 
/*** show post category ***/