【问题标题】:Why does Wordpress featured image not save when I publish my post?为什么我发布帖子时没有保存 Wordpress 特色图片?
【发布时间】:2018-05-14 11:51:07
【问题描述】:

我从另一个开发者那里继承了这个网站。我们最近更新了 Advanced Custom Fields PRO 插件以修复一个不同的问题,它运行良好。今天早上我接到一个电话,说特色图片在添加到帖子时出现,但当他们发布时,特色图片消失了。

我已检查以确保允许使用精选图片。现有帖子显示特色图片,但编辑或添加新帖子然后发布不会将图片附加到帖子。

我查看了 save_post 钩子方法,它们根本没有触及特色图像。

有什么想法吗?

这是通过“save_post”挂钩添加的方法:

function save_listing($listing_id, $post) {
  global $wpdb;
  if ('listings' != $post->post_type)
    return;
  //store fullDescription, keywords and categories in wp_listings
  $full_description = get_field('listing_full_description', $listing_id);
  $keywords = get_field('listing_keywords', $listing_id);
  $terms = wp_get_post_terms($listing_id, 'listing-categories', ['fields' => 'names']);
  $categories = implode(", ", $terms);
  $tier = get_field('listing_tier', $listing_id);
  $tier = array_search($tier, ['standard', 'premium', 'platinum']) + 1;
  $wpdb->replace('wp_listings', ['id' => $listing_id, 'title' => $post->post_title, 'fullDescription' => $full_description, 'keywords' => $keywords, 'categories' => $categories, 'tier' => $tier], ['%d', '%s', '%s', '%s', '%s', '%d']);

  $locations = get_field('listing_locations', $listing_id);
  $wpdb->delete('wp_locations', ['listing_id' => $listing_id], ['%d']);
  if (!isset($locations))
    return;
  if ($post->post_status != 'publish')
    return;
  foreach ($locations as $loc_number => $location) {
    $disabled = isset($location['disable_map_location']) && $location['disable_map_location'] ? 1 : 0;

    $wpdb->insert('wp_locations', ['listing_id' => $listing_id, 'loc_number' => $loc_number, 'lat' => $location['map']['lat'], 'lng' => $location['map']['lng'],
      'zip' => $location['zip'], 'city' => $location['city'], 'state' => $location['state'], 'disabled' => $disabled], ['%d', '%d', '%f', '%f', '%s', '%s', '%s', '%d']);
  }
}

add_action('save_post', 'save_listing', 100000000, 2);

还验证了是否支持特色图片

if (function_exists('add_theme_support')) {  
add_theme_support('post-thumbnails'); }

我已经验证,在帖子保存时,帖子元数据包括“_thumbnail_id”,并且有一个与该 ID 对应的帖子附件。但是,当页面重新加载时,没有特色图片。

【问题讨论】:

  • 哪个主题? Des主题支持特色图片??添加一些代码
  • 尝试删除浏览器缓存,看看是否可行。如果不是,您确定 the_post_thumbnail() 在模板文件中吗?
  • 这是一个自定义主题。到目前为止(过去两年),特色图片一直在工作。没有要添加的代码。问题在于 WordPress 编辑屏幕。发布帖子时,特色图片会消失。
  • 添加了 save_post 钩子方法。不过没有提及特色图片。
  • 我也尝试了一个标准的帖子并得到了相同的结果。我可以添加特色图片,但当我发布时它会消失。

标签: wordpress


【解决方案1】:

所以在保存后查看 $_POST 然后交叉引用帖子元数据后,我发现 _thumbnail_id 没有保存在元数据中。仍然不知道为什么,但我通过将此代码添加到我的 save_post 挂钩方法来修复它:

update_post_meta($post->ID, '_thumbnail_id', filter_input(INPUT_POST, '_thumbnail_id') );

这感觉有点老套,但整个网站都很老套:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多