自动设置特色图片

时间:2018-02-26 作者:JohnG

我该如何更改每篇帖子,使其具有相同的硬编码特色图片?!说这支雪茄:

https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg

(实际上我并不想这么做,但这是更复杂问题的绊脚石。)

我想应该是这样的:

function set_post_thumbnail( $post, $thumbnail_id ) {
    $thumbnail_id = "https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg";
    update_post_meta( $post->ID, \'_thumbnail_id\', $thumbnail_id );

}

add_action( \'save_post\', \'set_post_thumbnail\' );
。。。但没有雪茄。我错过了什么?谢谢

3 个回复
SO网友:Dave Romsey

Method 1: Using the publish_post hook* and media_sideload_image()

在这里,我们将从URL上传图像,将其附加到帖子,并在帖子发布时将其设置为特色图像。

*实际上,我们使用的是动态挂钩{$new_status}_{$post->post_type}, 指的是转换post 将类型发布到publish 地位看见wp-includes/post.php 有关详细信息。

此示例代码在发布帖子类型时适用post. 如果手动设置了特征图像,我们会进行检查,以便不会自动分配特征图像。您可以根据需要自由定制。

/**
 * Download image, attach it to the current post, and assign it as featured image
 * upon publishing the post.
 *
 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
 * refer to the new post status and post type, respectively.
 *
 * Please note: When this action is hooked using a particular post status (like
 * \'publish\', as `publish_{$post->post_type}`), it will fire both when a post is
 * first transitioned to that status from something else, as well as upon
 * subsequent post updates (old and new status are both the same).
 *
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 */
add_action( \'publish_post\', \'wpse_default_featured_image\', 10, 2 );
function wpse_default_featured_image( $post_id, $post ) {
    // Bail if there is already a post thumbnail set.
    $current_post_thumbnail = get_post_thumbnail_id( $post_id );
    if ( \'\' !== $current_post_thumbnail ) {
        return;
    }

    $url = \'https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg\';
    $title = "The default featured image.";

    $image = media_sideload_image( $url, $post_id, $title, \'id\' );
    set_post_thumbnail( $post_id, $image );
}

Method 2: Applying a filter to the post thumbnail.

通过使用过滤器,我们可以“虚拟”设置帖子缩略图。此方法可以更轻松地动态更改所有帖子的帖子缩略图。

Filter 1: post_thumbnail_html

这个post_thumbnail_html 过滤器可用于覆盖帖子缩略图的HTML输出:

/**
 * Filters the post thumbnail HTML.
 *
 * @param string       $html              The post thumbnail HTML.
 * @param int          $post_id           The post ID.
 * @param string       $post_thumbnail_id The post thumbnail ID.
 * @param string|array $size              The post thumbnail size. Image size or array of width and height
 *                                        values (in that order). Default \'post-thumbnail\'.
 * @param string       $attr              Query string of attributes.
 */
add_filter( \'post_thumbnail_html\', \'wpse_post_thumbnail_html\', 10, 5 );
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    return \'<img src="https://www.cigarsofcuba.co.uk/acatalog/slide1.jpeg" alt="Have a cigar">\';
}
Filter 2: 使用get_post_metadata_thumbnail_id 元键。

这里有另一种方法可以用不同的ID覆盖每个帖子的缩略图ID。

我建议实现一个自定义设置或选项页面,允许上传/选择特殊的缩略图(本地、从URL或媒体库)。然后您可以使用get_post_metadata 筛选以修改与每个post缩略图的元键关联的值_thumbnail_id:

/**
 * Dynamic hook: "get_{$meta_type}_metadata"
 * 
 * Filters whether to retrieve metadata of a specific type.
 * 
 * The dynamic portion of the hook, `$meta_type`, refers to the meta
 * object type (comment, post, or user). Returning a non-null value
 * will effectively short-circuit the function.
 *
 * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
 *                                     or an array of values.
 * @param int               $object_id Object ID.
 * @param string            $meta_key  Meta key.
 * @param bool              $single    Whether to return only the first value of the specified $meta_key.
 */
add_filter( \'get_post_metadata\', \'wpse_featured_image_id_override\', 100, 4 );
function wpse_featured_image_id_override( $value, $object_id, $meta_key, $single ) {
    $thumbnail_id_key = \'_thumbnail_id\';

    // Bail if this is not the correct meta key. Return the original value  immediately.
    if ( ! isset( $meta_key ) || $thumbnail_id_key !== $meta_key ) {
        return $value;
    }

    // Add additional guard clauses if necessary... (check post type, etc.)

    // Get the id for an image uploaded elsewhere.
    // This could be pulled from the customizer or a plugin options page.
    return 807;  // Example ID
}

SO网友:Phill Healey

您只需在保存页面/帖子时使用以下内容覆盖特色图像(无论是否设置):

//this is called on saving page/post
add_action(\'save_post\', \'force_featured_image\');

function force_featured_image( $post_id ){
   //set the featured image
   set_post_thumbnail( $post_id, [image_id] );
}
NOTE将“[image\\u id]”替换为您的图像id。

SO网友:Ajay Prajapati
function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
    if (! is_singular()) {
        $html = \'<a href="\' . get_permalink( $post_id ) . \'" title="\' 
                . esc_attr( get_the_title( $post_id ) ) . \'">\' . $html . \'</a>\';
        return $html;
    } else {
        return $html;
    }
}
add_filter( \'post_thumbnail_html\', \'wpb_autolink_featured_images\', 10, 3 );
结束

相关推荐

Functions.php过滤器未应用于AJAX调用

我已经使用php向菜单中添加了一个元素(为了便于说明,简化了代码):add_filter( \'wp_nav_menu_\' . $menu_slug . \'_items\', \'add_menu_item\' , 10, 2 ); function add_menu_item ( $items ) { $item = sprintf(\'<li class=\"custom-item\">%s</li>\', menu_item_content ()