2017-04-24 30 views
1

私はindex.phpファイルの異なる場所に10個のコンテナがある静的なページを持っています。最新の10個の記事を表示したいのですが。私はこのようなポストのためのデータ(タイトル、テキスト、サムネイル、作者、時間)をエコーし​​たい: Wordpressの最新の投稿

 <div class="text"> 
     <?php 
      echo '$textfrompost3'; 
     ?> 
    </div> 

<div class="title"> 
    <?php 
     echo '$titlefrompost3'; 
    ?> 
    </div> 

     ... 
     ... 

私のPHPファイル

:事前に

<?php 
    // the query 
    $the_query = new WP_Query(array(
    'category_name' => 'Allgemein', 
     'posts_per_page' => 10, 
     "orderby"  => "date", 
     "order"   => "DESC" 
    )); 

?> 

<?php if ($the_query->have_posts()) : ?> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 



    // insert values into variables 


    <?php endwhile; ?> 
    <?php wp_reset_postdata(); ?> 


<?php endif; ?> 

おかげで、私は任意のヘルプあなたに感謝します提供することができます。

+0

可能な複製を(HTTP :/ /stackoverflow.com/questions/41631565/how-to-display-latest-posts-wordpress) –

+0

は、ループ内のお互いの直下にある投稿を印刷しているため、変数または何か他のもの –

+0

これを試してみてください。 https://wordpress.stackexchange.com/questions/217299/wp-query-results-stored-in-variables-「変数にWP_QUERYの結果を取得する」と検索したときにGoogleが返したもの –

答えて

1

WP_Queryへの電話をget_posts()に置き換えるだけで、ポストオブジェクトの配列が提供されます。

$latest_posts = get_posts(array(
    'category_name' => 'Allgemein', 
    'posts_per_page' => 10, 
)); 

私は、上記の例ではorderorderbyを落としました。表示する投稿の数が異なるグローバル設定を使用しているかもしれませんが、デフォルトの注文を変更することはほとんどありません。

ドキュメント:https://codex.wordpress.org/Template_Tags/get_posts

それでは、私が出力する第三のポストのタイトルをしたいとしましょう、私は次のよう行うことができます。[最新のポストのWordpressを表示する方法]の

// let's be sure we have a third post. 
if (isset($latest_posts[2])) { 
    echo $latest_posts[2]->post_title; // remember arrays have a zero-based index. 
} 
+0

投稿者を取得する方法サムネイル? :/ post_author doensは存在しません。私はget_the_authorなどを使用したいが、動作しない。 –

関連する問題