i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
答案 0 :(得分:1)
您可以使用echo get_the_permalink($post->ID)
获取帖子的uri
所以看起来你需要
echo get_the_permalink($post[0]->ID);
和
echo get_the_permalink($post[1]->ID);
在href中
但是你可能最好创建一个foreach循环来完成get_posts函数的帖子
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
答案 1 :(得分:0)
好的,首先,你没有循环你所做的查询(例如$ posts = get_posts($ args);)你只是显示第一篇文章的缩略图和当前页面的缩略图。
你需要像这样循环帖子:
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;