Wordpress在博客代码段/链接的主页上显示特色图片



我想展示Wordpress特色的博客文章图像,以及博客文章的片段和标题(已经有了(。我们通常不使用PHP,所以我在获取要显示的图像时遇到了问题。这是为一个客户的网站,结果是比我们预期的要多得多的工作。该代码最初是从他们标题为"活动"的部分提取图像。我们放弃了该内容,并再次使用"帖子"部分。

该区域的代码如下。非常感谢您的帮助。感谢

<?php
$args = array('post_type' => 'post','order'=> 'ASC', 'posts_per_page' =>2,'orderby' => 'date','order'=>'DESC');
// The Query
query_posts( $args );?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<?php //$post_thumbnail_id = get_post_thumbnail_id( get_the_ID () ); ?> 
<?php /*$image_id = get_post_thumbnail_id(get_the_ID ());
$image_url = wp_get_attachment_image_src($image_id,'post-thumb', true);*/
?>
<li>
<?php //if ( has_post_thumbnail() ){  ?>
<?php if(get_field('show_this_activity_image_in_home_page')){?>
<div class="actImg">
<?php
$attachment_id = get_field('show_this_activity_image_in_home_page');
$size = "post-thumb"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );

好的,代码看起来像是在使用高级自定义字段,该字段本来会返回媒体项的ID。当您从自定义帖子类型移动到本机帖子时,这些ACF值将不再可用。

当你已经用the_post();设置了帖子数据时,你应该能够用get_the_post_thumbnail_url();获得缩略图url

请参阅https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/以便对此功能进行解释。

最新更新