自动在图像中添加标题

时间:2013-04-19 作者:Daniela

我想为以图像名称或备选文本显示的每张图片自动添加标题

示例标题添加自动

example

2 个回复
SO网友:Balas

每次在后端上载图像时,都可以通过编程方式添加标题、alt和描述。

https://brutalbusiness.com/automatically-set-the-wordpress-image-title-alt-text-other-meta/

我试过了,效果很好。这是为您将来上传的所有图像准备的。

试试这个,让我知道。

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
--------------------------------------------------------------------------------------*/
add_action( \'add_attachment\', \'my_set_image_meta_upon_image_upload\' );
function my_set_image_meta_upon_image_upload( $post_ID ) {

    // Check if uploaded file is an image, else do nothing

    if ( wp_attachment_is_image( $post_ID ) ) {

        $my_image_title = get_post( $post_ID )->post_title;

        // Sanitize the title:  remove hyphens, underscores & extra spaces:
        $my_image_title = preg_replace( \'%\\s*[-_\\s]+\\s*%\', \' \',  $my_image_title );

        // Sanitize the title:  capitalize first letter of every word (other letters lower case):
        $my_image_title = ucwords( strtolower( $my_image_title ) );

        // Create an array with the image meta (Title, Caption, Description) to be updated
        // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
        $my_image_meta = array(
            \'ID\'        => $post_ID,            // Specify the image (ID) to be updated
            \'post_title\'    => $my_image_title,     // Set image Title to sanitized title
            \'post_excerpt\'  => $my_image_title,     // Set image Caption (Excerpt) to sanitized title
            \'post_content\'  => $my_image_title,     // Set image Description (Content) to sanitized title
        );

        // Set the image Alt-Text
        update_post_meta( $post_ID, \'_wp_attachment_image_alt\', $my_image_title );

        // Set the image meta (e.g. Title, Excerpt, Content)
        wp_update_post( $my_image_meta );

    } 
}

SO网友:yuyazz

在插件中,这些解决方案来自爱斯基摩人和萨姆。。我们只需选择post\\u expert为空的所有附件并对其进行udpate,当其为空时,将其标题放在描述和标题中

    $s_query = "SELECT ID, post_title FROM `wp_posts` WHERE NOT post_content <=> post_title AND post_type=\'attachment\'";
        $r_result = mysql_query($s_query);

        if($r_result)
        {
            while($row = mysql_fetch_row($r_result)){
                $s_update_query = \'UPDATE `wp_posts` SET post_content="\'.$row[1].\'", post_excerpt="\'.$row[1].\'" WHERE ID="\'.$row[0].\'"\';
                mysql_query($s_update_query);
            }
        }
这里是针对相同结果的另一个类似查询:

    mysql_select_db(DB_NAME)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM wp_posts WHERE post_type= \'attachment\' AND post_excerpt= \'\'";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
$query = "UPDATE wp_posts SET post_excerpt=post_title WHERE post_type=\'attachment\' AND post_excerpt=\'\'";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
因此,使用这些解决方案,我们可以导入(选择)wp gallery中的许多图像。首先,标题没有出现,我们需要更新帖子以在前端看到它。

结束