【问题标题】:Setting default image title based on post title根据帖子标题设置默认图片标题
【发布时间】:2017-06-19 18:06:55
【问题描述】:

我想创建一个过滤器,默认情况下,在文件上传期间将帖子标题添加为 wordpress 中的所有图片标题。我知道为此我可以使用:

wp_insert_attachement_data

我写过类似的东西:

add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );

function wpq_insert_attachment_data( $data, $postarr ) {

$append = get_the_title( $post->post_parent );

// Append to the image caption field: 
if( false == stripos( $data['post_excerpt'], $append ) ) {
$data['post_excerpt'] .= $append; 
}

// Append to the image description field: 
if( false == stripos( $data['post_content'], $append ) ) {
$data['post_content'] .= $append; 
}

return $data;
} 

但它不起作用。任何想法为什么?

【问题讨论】:

    标签: wordpress filter


    【解决方案1】:

    请检查下面的代码,它正在用帖子标题替换默认图像标题。我试过了,效果很好。

     function wpq_insert_attachment_data($data, $postarr){
    
          $posttitle = get_the_title( $postarr['post_parent'] );
          $data['post_title'] = $posttitle;
          $data['post_name'] = $posttitle;    
          return $data;
            }
    add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
    

    【讨论】:

    • @Pawel Skaba:欢迎你来:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2021-10-03
    相关资源
    最近更新 更多