3

您好,我正在尝试为post_type= 'post' 设置默认特色图片,但不包括post_type= 'page'。

我在子主题的函数文件中编写了以下代码,但我不断收到此错误:

注意:尝试在第 18 行的 /home/ossettto/public_html/wp-content/themes/sport-child/functions.php 中获取非对象的属性

function wpforce_featured()
{
    global $post;
    $post_type = get_post_type($post->ID);
    if ($post_type === 'post')
    {
        $already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
        if (!$already_has_thumb)
        {
            //If post does not have a featured image then get the first post image and set as featured image.
            $attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1"); // Number 1 relates to taking post image number 1 and adding it as a featured image.
            if ($attached_image)
            {
                foreach ($attached_image as $attachment_id => $attachment)
                {
                    set_post_thumbnail($post->ID, $attachment_id);
                    //$attachment_id = attachment_url_to_postid( $image_url );
                    //echo $attachment_id;
                }
            }
            else
            {
                set_post_thumbnail($post->ID, '27264'); // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
            }
        }
    }
}

//end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

任何帮助将不胜感激。
谢谢。

4

1 回答 1

3

有两点不清楚:

  1. 为什么要在 , 和其他钩子中一起执行此the_post操作save_post
  2. 知道哪一行是第 18 行会很有帮助,但我猜是这一行$post_type = get_post_type( $post->ID );

但是,您收到通知的原因是这些操作不一定都$post为您准备好对象global $post。此外,这些动作都有不同的函数签名,$post在不同的地方作为参数传递。

鉴于您要连接的所有过滤器,您需要在您的函数周围创建一个“抽象”或“包装器”,以便您可以$post在参数的正确位置正确调用它。

查看文档以查看$post传递位置的示例:

the_post 动作- 作为唯一参数传递
save_post 动作- 作为第二个参数传递
draft_to_published(和其他挂钩) - 作为第三个参数传递

// New function that accepts proper parameters for save action
function wpforce_featured_on_save( $post_id, $post, $update ) {
    // No need to duplicate code.  Instead, call your original function
    // passing it the $post parameter
    wpforce_featured_status( $post );
}

// New function that accepts proper parameters for these actions
function wpforce_featured_on_status_change( $new, $old, $post ) {
    // No need to duplicate code.  Instead, call your original function
    // passing it the $post parameter
    wpforce_featured( $post );
}

// Your original function with slight modifications
// NOTE: ONLY accepts $post object - no global post
function wpforce_featured( $post ) {
      // REMOVED global $post - not helpful here
      $post_type = get_post_type( $post->ID );
      // ... the rest of your code here
}

// Your original hook, it's passing the $post_object parameter
add_action('the_post', 'wpforce_featured');
// The save hook, which passes parameters - modified to call a different function
add_action('save_post', 'wpforce_featured_save', 10, 3);
// The status change hooks, which pass parameters - modified to call a different function
add_action('draft_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('new_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('pending_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('future_to_publish', 'wpforce_featured_on_status_change', 10, 3);
于 2017-01-18T14:45:16.703 回答