0

やあみんな私は投稿のタイトルを表示するウィジェットを作成していて、その投稿とその日付からの抜粋です。私が持っているコードはここにあります:

public function widget( $args, $instance ) {


        extract( $args );

        $headline = $instance['headline'];
        $category = $instance['category'];
        $numberposts = $instance['numberposts'];
        $readmore = $instance['readmore'];


        echo $before_widget;

        echo $before_title;

        echo "<p class=\"headline\">$headline</p>";

        echo $after_title;

        $args = array( 'numberposts' => $numberposts, 'category_name' => $category );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
        echo '<a href="' . get_permalink($recent["ID"]) . '" title=" '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> ';
        echo the_time('F j, Y');
        echo the_excerpt();

        }

ご覧のとおり、私は時間と投稿からの抜粋を呼び出そうとしています。動作していますが、ウェルカムと呼ばれる最初の投稿からの日付と抜粋のみが表示されています。日付と個々の投稿からの抜粋を表示したいと思います。サイドバーにウィジェットがあるサイトへのリンクを投稿します。私が十分に明確でない場合、またはより多くの情報が必要な場合は申し訳ありませんが、私はこれに非常に慣れていません。

http://www.modmacro.us/wpsandbox/

4

1 に答える 1

1

まず、the_excerpt()はすでにechoステートメントです。returnステートメントではありません。the_date()と同じです。それらの前に「get_」を付けると、必要な情報が返されます。または、その前にあるechoコマンドを削除することもできます。

2番目のこと:これらの関数が機能するためには、Wordpressループで使用する必要があります。

私たちはすでにループに入っているので、必要な情報を反映するようにWordpressグローバルを適切に変更することを確認する必要があります。

foreach( $recent_posts as $recent ){
    setup_postdata(get_post($recent['ID']));
    echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' .   get_the_title().'</a> ';
    echo get_the_time('F j, Y', $recent['ID']);
    the_excerpt();
}
wp_reset_postdata();
于 2013-01-04T01:55:30.753 に答える