[英]How to show a post only to the user who posted and the admin?
Is there a way to limit who can see posts in one specific category, so that only to the user who posted it and the admin can see them? 有没有一种方法可以限制谁可以看到一个特定类别的帖子,以便只有发布该帖子的用户和管理员才能看到它们?
I have set up a custom post type with its own template, a custom post status and a new category for this. 我已经使用其自己的模板设置了自定义帖子类型,自定义帖子状态和为此的新类别。 I want posts in this category only to be visible to the user who posted it and the admin.
我希望此类别的帖子仅对发布该帖子的用户和管理员可见。
I tried to do this on the code below, but it doesn't work - only the admin can see the posts. 我试图在下面的代码上执行此操作,但是它不起作用-只有管理员才能看到帖子。
<?php
/**
* The template for displaying all single posts.
*/
get_header(); ?>
<?php
$user_id = the_author_meta('ID');
$admin_ch = get_user_by('id', 1);
$cu_user = get_current_user_id();
if($cu_user == $user_id || $cu_user == $admin_ch) {
while ( have_posts() ) : the_post(); ?>
<div class="container-block">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div id="sh">
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php if ( 'post' === get_post_type() && $meta = screenr_posted_on( false ) ) : ?>
<?php
endif; ?>
<?php
if ( ! get_theme_mod( 'disable_featured_image', 0 ) ) {
if (has_post_thumbnail()) {
echo '<div class="entry-thumb">';
the_post_thumbnail('screenr-blog-list');
echo '</div>';
}
}
?>
</header><!-- .entry-header -->
</div>
<div class="entry-meta">
<span class="p-date">
On
<?php $post_date = get_the_date( 'l F j, Y' ); echo $post_date; ?>
By
<?php $user_name = the_author_meta( 'display_name', $ID ); echo $user_name; ?>
</span>
</div><!-- .entry-meta -->
<div class="entry-content">
<?php
the_content( sprintf(
/* translators: %s: Name of current post. */
wp_kses( esc_html__( 'Continue reading %s', 'screenr' ), '<span class="meta-nav">→</span>' ),
the_title( '<span class="screen-reader-text">"', '"</span>', false )
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'screenr' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php show_publish_button(); ?>
</article><!-- #post-## -->
</div>
<?php endwhile; // End of the loop.
} else {
echo "You can't show this";
}
?>
<?php get_footer(); ?>
It won't let me leave this as a comment but on this line: 它不会让我留下评论,而是在这一行:
if($cu_user == $user_id || $admin_ch) {
Change it to: 更改为:
if($cu_user == $user_id || $cu_user == $admin_ch) {
As it is written, after the or, there isn't an argument. 在编写时,在or后面没有参数。 You need to give an argument there too.
您也需要在那里争论。
Also the proper function is: 同样正确的功能是:
get_the_author_meta('ID')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.