【问题标题】:How do I obtain the text of the image associated with a Wordpress blog featured image?如何获取与 Wordpress 博客特色图片相关联的图片文本?
【发布时间】:2017-09-29 16:05:56
【问题描述】:

下面的代码可以很好的获取帖子的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();

但是我需要的是获取特色图片的文本/网址,包括路径,但不将其打印到屏幕上。这样做的目的是让我可以使用 ImageMagick 转换创建自定义大小的图像,并将其显示到屏幕而不是原始图像。

我会做类似的事情

convert "$image" -resized (my custom size) -strip 400x/$image

转换代码有点复杂,因为我打算创建大的方形拇指,但这就是目标。然后,我会将图片发布到页面上的方形拇指墙风格画廊中,并附上指向各个帖子的链接。

有没有像 the_featured_image() 这样的东西可以让我得到图像,然后我可以将其设置为变量并运行转换代码?

【问题讨论】:

  • 您是指帖子缩略图吗? the_post_thumbnail()

标签: php mysql wordpress imagemagick imagemagick-convert


【解决方案1】:

您可以使用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
}

【讨论】:

  • 很好,工作。我将如何使用 the_title();并将其放入 $the_title 之类的变量中,然后用它做我想做的事情,而不是打印到屏幕上?
  • @BobM get_the_title()$post-&gt;post_title
猜你喜欢
  • 2017-08-16
  • 2016-01-29
  • 2020-01-02
  • 2018-04-11
  • 2015-02-02
  • 1970-01-01
  • 2012-06-30
  • 2015-04-20
  • 2022-08-24
相关资源
最近更新 更多