0

我有一个 wordpress 网站,它是关于媒体共享的。我已将 mp3 文件上传为专辑,并为每个专辑创建了一个帖子。但我想为每个自动上传的 mp3 文件创建单独的帖子。

我已经尝试过 mp3-to-post 插件,但最新版本的 wordpress 不支持该插件,我尝试上传插件,但它不起作用。

4

1 回答 1

0

使用do_action( 'add_attachment', $post_ID )钩子,您可以创建相应的帖子。对于 ref - 请参阅wp_insert_post函数

要执行上述操作,请在活动主题的 functions.php 中添加以下代码片段 -

function action_add_attachment( $post_ID ) { 
    if( !$post_ID ) return;
    $attachment = get_post( $post_ID );
    // Create album post object for attachment post ID
    $attachment_post = array(
      'post_title'    => 'Album - ' . $attachment->post_title,
      'post_content'  => 'Your post content goes here',
      'post_status'   => 'publish',
      'post_author'   => $attachment->post_author
    );

    // Insert the post 
    wp_insert_post( $attachment_post );
}

add_action( 'add_attachment', 'action_add_attachment', 99 ); 
于 2019-04-05T06:03:47.270 回答