2017-02-14 4 views
0

私は次のコードを使用して、ワードプレスのショートコード[educationposts]を作成して、 "education"というカテゴリの投稿を表示しています。私はでそれを解決する方法のようにショートコードを変更することにより、任意のカテゴリを表示することができますよう柔軟であることが、このショート機能を設定する[ポスト=教育]または[ポスト=ブログ]どのようにショートカット変数ですべてのカテゴリの投稿を取得するワードプレスショートコード機能をカスタマイズするには?

おかげ

/*** 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 ***/ 
関連する問題