2016-12-13 1 views
0

特定のhtml/cssプロパティを持つ最新の投稿を表示したいと思います。私の現在の解決策は、タイトルとURLのみを投稿に表示しています(日付または抜粋なし)。ここで一部のデータのみを表示するWordpressクエリ

は、私はindex.phpの中でアクションを呼び出すのfunctions.phpでの私の「機能バージョン#1」のコードである:ここで

function latest_article() { 
    $content_latest_php = ""; 

    $query_latest_article = wp_get_recent_posts(array(
    'numberposts' => 1 
)); 

    foreach ($query_latest_article as $post) { 
    $content_latest_php = get_template_part('content-latest', get_post_format()); 
    }; 

    return $content_latest_php; 
}; 

は、別の方法を使用しようと、私の「機能バージョン#2」であります同じ目標を達成するために:

<div class="article-latest"> 
    <a class="article-link" href="<?php the_permalink() ?>"> 
    <img class="article-latest-image" src="<?php bloginfo('template_directory');?>/img/article-thumbnail.png"></img> 
    <div class="article-latest-date"><?php get_the_date(); ?></div> 
    <div class="article-latest-title"><?php the_title(); ?></div> 
    <div class="article-latest-text" href="<?php the_permalink() ?>"><?php get_the_excerpt() ?></div> 
    </a> 
</div> 

そして、これが結果です:

function latest_article() { 
    $args = array(
    'post_type' => 'post', 
    'posts_per_page' => 1, 
    'orderby' => 'date', 
    'order' => 'DESC' 
); 

    $query_latest_post = new WP_Query($args); 
    if ($query_latest_post->have_posts()) { 
    while ($query_latest_post->have_posts()) { 
     $query_latest_post->the_post(); 
     get_template_part('content-latest', get_post_format()); 
    } 
    } 
    wp_reset_postdata(); 
}; 

する。これは、コンテンツlatest.phpです

<div class="article-latest"> 
    <a class="article-link" href="http://mywebsite.com/the-latest-post/"> 
    <img class="article-latest-image" src="http://mywebsite.com/wp-content/themes/nameofthetheme/img/article-thumbnail.png"></img> 
    <div class="article-latest-date"></div> 
    <div class="article-latest-title">The name of the latest post</div> 
    <div class="article-latest-text" href="http://mywebsite.com/the-latest-post/"></div> 
    </a> 
</div> 

^日付はなく、抜粋はありません。コードインスペクタでウェブサイトを表示しています。何故ですか?

+0

"<?php ** echo ** get_the_date();?>"の "<?php get_the_date();?>"を変更して投稿の日付を表示することはできましたが、抜粋 – lelekcze

答えて

0

あなたはget_the_excerpt()get_the_date()使用している:これらの両方の機能リターン関連する情報を、彼らはそれを出力しませんだからどちらかが必要echoへ:

<?php 
echo get_the_date(); 
echo get_the_excerpt(); 
?> 

それとも実際に出力がこれらの機能を使用します。

<?php 
the_date(); 
the_excerpt(); 
?> 

ワードプレスは、この規則をたくさん使用していますが、関数がget - get_the_title()get_the_date()などで始まっている場合は、出力する代わりに通常returnとなります。 theで始まる関数は、通常の出力ループ内の現在の投稿と関係があります。

例を取るために:あなたはthe_exerpt()のソースを見ていた場合、あなたはそれが実際にget_the_excerpt()を呼び出すことがわかりたい:

function the_excerpt() { 

/** 
* Filters the displayed post excerpt. 
* 
* @since 0.71 
* 
* @see get_the_excerpt() 
* 
* @param string $post_excerpt The post excerpt. 
*/ 
echo apply_filters('the_excerpt', get_the_excerpt()); 
} 
+0

説明をありがとう、今私は問題なく日付を得ることができます!抜粋はまだロードされていませんが、私は自分のコードのエラーではないと思います。 – lelekcze

+0

まず、投稿に実際に抜粋があるかどうか確認してください: 'content-latest.php'の最後に' print_r($ post); 'を追加してください。それはpost配列を出力します。あなたがそれをコメントにコピーすれば、私は一見します。 – Dre

+0

「... [post_excerpt] => [post_status] => ... 'という抜粋はありませんが、自動生成のものを使うべきだと思いました。 また、私はカスタムの抜粋の長さとより多くの属性を使用していますが、私の問題tho(?)の原因であってはなりません。 – lelekcze

0

あなたは

コンテンツ-最新のテーマファイルでこれを試みることができます
<div class="article-latest"> 
    <a class="article-link" href="<?php the_permalink() ?>"> 
    <img class="article-latest-image" src="<?php bloginfo('template_directory');?>/img/article-thumbnail.png"></img></a> 
    <?php the_date()('Y-m-d', '<div class="article-latest-date">', '</div>'); ?> 
    <div class="article-latest-title" href="<?php the_permalink() ?>"><?php the_title(); ?></a></div> 
    <div class="article-latest-text"><?php the_excerpt() ?></div> 
</div> 
関連する問題