作为管理员,我可以预览预定的帖子。在post列表中,如果您悬停在预定的帖子标题上,就会出现一个“预览”链接。
作为一名撰稿人,我可以预览一篇文章,如果它是在草稿或未决。我不能再预览的帖子,一旦它已被安排。
撰稿人很想看看编辑对他们的文章做了些什么,并希望看到它的预览,而它是排定的,尚未出版。要明确的是:我并不是在寻找一种方法,允许投稿人在排定后修改文章,只需(预先)查看。
在wordpress的角色和功能中,我没有找到相关的功能。似乎有阅读,read_private_posts,read_private_pages,但没有什么像read_own_future_posts。我不知道为什么这不是默认行为。这是奇怪的,投稿人可以查看自己的文章来写它,一旦它发表,但不能介于两者之间。还是我在这里完全错过了什么?
发布于 2018-03-31 17:12:15
我为此创建了一个自定义插件。如果您不知道如何使用它,请阅读基础在wordpress网站上。即使您不是技术人员,也不难:基本上转到wp/plugins,创建一个带有插件名的文件夹,创建一个带有插件名的.php文件,添加一个标题并粘贴下面的代码块。
然后上传文件,转到wordpress网站。你会看到你的插件出现在安装的插件中。启动它。好了。
步骤1:如果文章作者有预览链接,他们应该能够预览文章。这对于贡献者来说是可行的,但是对于任何角色都应该有效。
//allow post preview if you are the post owner, whatever role you might have (e.g. contributor)
function jv_change_post( $posts ) {
if(is_preview() && !empty($posts)){
$current_user_id = get_current_user_id();
$author_id= $posts[0]->post_author;
if($current_user_id == $author_id)
$posts[0]->post_status = 'publish';
}
return $posts;
}
add_filter( 'posts_results', 'jv_change_post', 10, 2 );据我所知,它所做的是:每当wordpress对文章进行查询时,检查当前显示的页面是否为post预览版。如果是,并且当前用户是文章的作者,则将状态更改为发布。这将允许作者查看帖子,无论帖子的真实状态如何(例如,“未来”,如果是计划的话)。
这并不能真正改变文章的状态,它只会使当前的查询结果认为文章已经发布(因此可以查看)。
注意:我从另一个stackexchange职位获得了这段代码的核心。
第二步:如何从一开始就给文章作者一个预览链接?我通过将链接作为一个列添加到posts屏幕(管理部分)来解决这个问题。这是代码:
//add preview link to posts overview in admin section (as extra column)
// add the column
function add_contributor_preview_column( $columns ) {
if( !current_user_can( 'edit_others_posts' ) ) {
$previewColumn = array( 'preview' => __( 'Preview', 'your_text_domain' ) );
return array_merge($previewColumn, $columns);
}
else
return $columns;
}
add_filter( 'manage_posts_columns' , 'add_contributor_preview_column' );
// add contents to the column
function display_post_preview( $column, $post_id ) {
if($column == "preview"){
$post = get_post($post_id);
if($post->post_status == 'future')
echo 'preview';
else
echo 'view';
}
}
add_action( 'manage_posts_custom_column' , 'display_post_preview', 10, 2 );不管怎么说,当我做的时候,我过滤掉了管理屏幕上的帖子。投稿人只看到自己的帖子,不感到困惑。这个部分与这个问题没有直接关系,但是它有助于清理贡献者的界面。
//only show your own posts in the edit page if you're a contributor (avoid confusion)
function posts_for_current_author($query) {
if(!is_admin())
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
$current_user_id = get_current_user_id();
$query->set('author', $current_user_id );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');https://wordpress.stackexchange.com/questions/299049
复制相似问题