【问题标题】:Require Featured Image in Wordpress Post for functions.php需要 Wordpress Post 中的特色图片以获得 functions.php
【发布时间】:2014-11-23 03:06:44
【问题描述】:

在仪表板中创建 WordPress 帖子时,我使用以下代码来要求功能图像...

function featured_image_requirement() {
     if(!has_post_thumbnail()) {
          wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' ); 
     } 
}
add_action( 'pre_post_update', 'featured_image_requirement' );

这项工作......但有两个问题/错误。

帖子不是页面

首先,我只希望它适用于 POSTS,而不适用于 PAGES。所以,当我去编辑我拥有的任何页面并尝试提交时......我收到需要特色图片的错误消息。所以我无法保存我的任何页面...... 如何更改上面的代码,使其仅在编辑 POST(而不是 PAGE)时有效

正在保存草稿...

第二个问题是 WordPress 自动保存草稿的问题。当我正在编辑一个页面时,PUBLISH 按钮将淡出(并且变得不可点击),因为该页面正在“保存草稿......”。这是 WordPress 的一个有用功能。然而,由于我启用了上面的脚本,这个保存延迟非常长。有时发布按钮根本无法点击。

有人知道如何在不需要所需功能的情况下“保存草稿”吗?

【问题讨论】:

  • 钩子的第一个参数将是帖子的ID,然后您可以使用此ID来检查它是页面还是帖子:function featured_image_requirement($post_ID) { if(get_post_type($post_ID) == 'post') { // not a page }}

标签: wordpress


【解决方案1】:

我不确定这是否是一个错字,但您添加了两次操作,所以应该只有一次。

无论如何,你可以试试这个:

function featured_image_requirement() {
  global $post;
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return;
  if ($post - > post_status == "publish" && !has_post_thumbnail()) {
    wp_die('You forgot to set the featured image. Click the back button on your browser and set it.');
  }
}
add_action('pre_post_update', 'featured_image_requirement');

【讨论】:

  • 这个解决方案没有解决我遇到的任何问题。
猜你喜欢
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 2015-09-01
  • 2012-09-03
  • 1970-01-01
  • 2019-02-01
相关资源
最近更新 更多