【问题标题】:WordPress get_the_post_thumbnail_url() not working when posting from front-end从前端发布时,WordPress get_the_post_thumbnail_url() 不起作用
【发布时间】:2019-10-13 09:03:29
【问题描述】:

每当我从仪表板发帖时,我都可以使用 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/

【问题讨论】:

  • $recent_posts = wp_get_recent_posts( array( 'numberposts' =&gt; '1' ) );这里的逻辑是什么? $latestPost_ID 应该是$latestPost_ID = $post-&gt;ID;
  • 嗨@Sky,我同意你的看法。我在测试期间这样做了。它基本上抓取最新的帖子,并将其限制为 1。所以它的逻辑几乎与 $latestPost_ID = $post->ID; 相同。我现在更换了它,测试时一切仍然相同

标签: php wordpress posts


【解决方案1】:

我发现通过附件记录获取特色图片更可靠,无论是面向公众还是管理员请求。试试这个:

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

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2016-09-09
    • 2016-05-14
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2013-08-13
    • 1970-01-01
    相关资源
    最近更新 更多