特定の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>
^日付はなく、抜粋はありません。コードインスペクタでウェブサイトを表示しています。何故ですか?
"<?php ** echo ** get_the_date();?>"の "<?php get_the_date();?>"を変更して投稿の日付を表示することはできましたが、抜粋 – lelekcze