是否有一种方法可以使用php函数自动将图像id类附加到帖子内容中现有的附加图像?那太棒了。
这是我到目前为止的情况。从理论上讲,这似乎应该奏效。我做错了什么?
function be_attachment_id_on_images( $attr, $attachment ) {
if( !strpos( $attr[\'class\'], \'wp-image-\' . $attachment->ID ) )
$attr[\'class\'] .= \' wp-image-\' . $attachment->ID;
return $attr;
}
add_filter( \'wp_get_attachment_image_attributes\', \'be_attachment_id_on_images\', 10, 2 );
if ( current_user_can( \'manage_options\' ) && is_admin()){
function my_update_posts() {
$args = array(
\'post_type\' => \'post\',
\'numberposts\' => -1,
\'category\' => 1,
\'ID\' => 1,
);
$myposts = get_posts($args);
foreach ($myposts as $mypost){
$content = $mypost->post_content;
apply_filters(\'be_attachment_id_on_images\', $content);
$mypost->post_content = $content;
wp_update_post( $mypost );
}
}
add_action( \'admin_init\', \'my_update_posts\' );
}
SO网友:GDY
无耻地从这里抄袭:https://letswp.io/add-attachment-id-to-the-class-of-wordpress-images/
add_filter( \'wp_get_attachment_image_attributes\', \'add_image_id_class\', 10, 2 );
function add_image_id_class( $attr, $attachment ) {
$class_attr = isset( $attr[\'class\'] ) ? $attr[\'class\'] : \'\';
$has_class = preg_match( \'/wpimage\\_[0-9]+/\', $class_attr, $matches );
if ( !$has_class ) {
$class_attr .= sprintf( \' wpimage_%d\', $attachment->ID );
$attr[\'class\'] = ltrim( $class_attr );
}
return $attr;
}
这将添加一个类,如
wpimage_{ATTACHMENT_ID}
例如
wpimage_22
.