下面的代码可以很好地获取帖子的 URL 和标题:
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 4 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <br />
<?php
endforeach;
wp_reset_postdata();
但是我需要的是获取特色图像的文本/url(包括路径),但不将其打印到屏幕上。这样做的目的是为了让我可以使用 ImageMagick 转换创建自定义大小的图像并将其显示到屏幕上,而不是原始图像。
我会做类似的事情
convert "$image" -resized (my custom size) -strip 400x/$image
转换代码有点复杂,因为我打算创建大的方形拇指,但这就是目标。然后,我会将这些图像发布在页面上的方形拇指墙风格画廊中,并附上各个帖子的链接。
有没有像 the_featured_image() 这样的东西可以让我获得图像,然后我可以将其设置为变量并运行转换代码?
最佳答案
您可以使用 get_the_post_thumbnail_url
来获取特色图片的 URL,如果没有,则返回 false。
所以在你的 foreach
循环中
$thumb_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); //can also be $post->ID
if($thumb_url)
{
//do your stuff
}
关于php - 如何获取与 WordPress 博客特色图像关联的图像文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492848/