我在一次WordPress安装中有多个作者帐户。通常我会创建一个帖子,并将作者设置为不同的帐户。然而,当我将图片上传到该帖子时,他们的附件页面会将作者列为我的帐户。如何设置帖子的作者,并将其传递到与该帖子相关的媒体?
如何为帖子和帖子附件设置作者
2 个回复
最合适的回答,由SO网友:brasofilo 整理而成
在主题中使用此functions.php
:
add_filter( \'add_attachment\', \'wpse_55801_attachment_author\' );
function wpse_55801_attachment_author( $attachment_ID )
{
$attach = get_post( $attachment_ID );
$parent = get_post( $attach->post_parent );
$the_post = array();
$the_post[\'ID\'] = $attachment_ID;
$the_post[\'post_author\'] = $parent->post_author;
wp_update_post( $the_post );
}
SO网友:RemBem
我扩展了@brasofilo的上述解决方案,也将附件发布日期更改为发布家长的日期。
不仅在上传附件时,而且在编辑附件时。您可以使用edit_attachment
此过滤器具有相同的功能。然而,当您这样做时wp_update_post
函数会导致无限循环并导致PHP内存错误,经过多次搜索,我找到了解决方法。实际上,在Codex.
解决方案是移除过滤器,如下所示:
function wpse_55801_attachment_author( $attachment_ID ) {
remove_filter(\'add_attachment\', \'wpse_55801_attachment_author\');
remove_filter(\'edit_attachment\', \'wpse_55801_attachment_author\');
$attach = get_post( $attachment_ID );
$parent = get_post( $attach->post_parent );
$the_post = array();
$the_post[\'ID\'] = $attachment_ID;
$the_post[\'post_author\'] = $parent->post_author;
$the_post[\'post_date\'] = $parent->post_date;
wp_update_post( $the_post );
add_filter( \'add_attachment\', \'wpse_55801_attachment_author\' );
add_filter( \'edit_attachment\', \'wpse_55801_attachment_author\' );
}
add_filter( \'add_attachment\', \'wpse_55801_attachment_author\' );
add_filter( \'edit_attachment\', \'wpse_55801_attachment_author\' );
结束