問題描述
是否有任何方式在預覽模式下查看您的主頁,以便我們可以在發佈之前檢查我的帖子在主頁上的顯示方式 (即確保功能圖像正確等等) 。
最佳解決方案
我想你可以將您的帖子設置為 private
,以便在主頁上私下查看。
Once you change the visibility to private, the post or page status changes to “Privately Published” as shown. Private posts are automatically published but not visible to anyone but those with the appropriate permission levels (Editor or Administrator).
見 here 。
這是一個想法,在網站上查看 draft
的帖子:
我們重新使用 preview
參數將 draft
帖子添加到當前頁面視圖中,例如:
- example.com/?preview=true
- example.com/2014/01/?preview=true
然後,我們修改所有前端查詢,對於登錄用户,使用:
add_filter( 'posts_where', function( $where ){
if( ! is_admin()
&& is_user_logged_in()
&& 'true' === get_query_var( 'preview' )
&& ! is_singular() )
{
global $wpdb;
$from = sprintf( "%s.post_status = 'publish'", $wpdb->posts ) ;
if( current_user_can( 'edit_others_posts' ) )
{
// add drafts from all users:
$to = sprintf( "%s.post_status IN ( 'publish', 'draft' ) ", $wpdb->posts ) ;
}
else
{
// add drafts from current user:
$sql = " ( %s.post_status = 'publish'
OR ( %s.post_status = 'draft' AND %s.post_author = %d ) ) ";
$to = sprintf( $sql,
$wpdb->posts,
$wpdb->posts,
$wpdb->posts,
get_current_user_id()
);
}
$where = str_ireplace( $from, $to, $where );
}
return $where;
});
但是我們可以使用 is_main_query()
限制對主查詢的修改。
ps:這可能需要一些測試或調整… 但你得到的想法;-)
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。