サポート » 使い方全般 » 記事タイトル一覧をショートコードで表示させたい

  • 解決済 ringo777

    (@ringo777)


    //ここから投稿一覧のショートコード(7件)
    
    function postFunc($atts) {
            extract(shortcode_atts(array(
                  "num" => '7',
                  "cat" => ''
            ), $atts));
           global $post;
           $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
           $retour='<ul>';
           foreach($myposts as $post) :
                  setup_postdata($post);
                  $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
           endforeach;
           $retour.='</ul> ';
           wp_reset_postdata();
           return $retour;
    }
    add_shortcode("postlist", "postFunc");
    
    //ここまで

    functions.phpに上記のように記述すると指定の件数分うまく表示されるのに
    下記のように記述すると1件しか表示されなくなります。
    なぜこうなるのか教えて頂けるとうれしいです。よろしくお願いします。
    Wordpress初心者です。

    //ここから投稿一覧のショートコード(7件)
    
    function postFunc($atts) {
            extract(shortcode_atts(array(
                  "num" => '7',
                  "cat" => ''
            ), $atts));
           	$args=array(
    		'numberposts' => '.$num.',
    		'order'       => 'DESC',
    		'orderby'     => 'post_date',
    		'category'    => '.$cat'
    );
           global $post;
           $myposts = get_posts($args);
           $retour='<ul>';
           foreach($myposts as $post) :
                  setup_postdata($post);
                  $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
           endforeach;
           $retour.='</ul> ';
           wp_reset_postdata();
           return $retour;
    }
    add_shortcode("postlist", "postFunc");
    
    //ここまで
1件の返信を表示中 - 1 - 1件目 (全1件中)
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「記事タイトル一覧をショートコードで表示させたい」には新たに返信することはできません。