从前端发布时,WordPress get_the_post_thumbnail_url()不起作用

时间:2019-05-27 20:11:35

标签: php wordpress posts

每当我从仪表板发布时,我都可以使用get_the_post_thumbnail_url()获取特色图像URL,并使用wp_mail()将其显示在电子邮件中。但是,当我从前端尝试此操作时,会得到一个空URL。

我已经尝试了许多前端插件,但是在特色图片方面没有一个起作用。帖子的其余字段在电子邮件中显示正常。

我使用以下代码:

// POST MAILMAN

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {


if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {

$latestPost_ID = $post->ID;
$latestPost_ImgUrl = get_the_post_thumbnail_url($latestPost_ID, 'full');
$latestPost_Category = get_the_category($latestPost_ID);
$latestPost_Category_Name = esc_html( $latestPost_Category[0]->name );
$latestPost_Url = get_post_permalink($latestPost_ID);
$latestPost_Title = get_post_field( 'post_title', $latestPost_ID );
$latestPost_Excerpt = get_post_field( 'post_excerpt', $latestPost_ID );
$todaydate = date("l") . ", " . date("j") . " " . date("F") . " " . date("Y");

在电子邮件中显示图像:

<td class="fluid-img" style="font-size:0pt; line-height:0pt; text-align:left;"><img src="' . esc_url($latestPost_ImgUrl) . '" border="0" width="650" height="366" alt="" /></td>

完成前端与后端后: https://imgur.com/a/TqP9L6a

我尝试过的一些插件: https://wordpress.org/plugins/accesspress-anonymous-post/ https://wordpress.org/plugins/wp-user-frontend/

1 个答案:

答案 0 :(得分:0)

对于面向公众或管理员的请求,我发现通过附件记录获取“特色图片”更为可靠。试试这个:

$attachment = wp_get_attachment_image_src(get_post_thumbnail_id($latestPost_ID), 'full', true);
$featuredImageUrl = $attachment[0];