2017-09-23 36 views
0

有沒有辦法限制誰可以在一個特定的類別中看到帖子,這樣只有發佈它的用戶和管理員才能看到它們?如何僅向發佈的用戶和管理員顯示帖子?

我已經設置了一個自定義的帖子類型,它有自己的模板,一個自定義帖子狀態和一個新的類別。我希望此類別的帖子只對發佈它的用戶和管理員可見。

我試圖在下面的代碼上做到這一點,但它不工作 - 只有管理員可以看到帖子。

<?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">&rarr;</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(); ?> 
+0

但遺憾的是它看到的帖子只爲管理員 – Hobo

+0

有什麼喲試圖調試代碼?你是否嘗試打印'$ cu_user','$ user_id'和'$ admin_ch'的值來判斷它們是否正確?另外,請參閱[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve) - 您發佈的大多數代碼都與問題無關(例如,您如何顯示帖子是不相關的),所以更新你的代碼只包含相關的部分,這樣我們就能更容易地看到代碼中發生了什麼。 – FluffyKitten

回答

0

它不會讓我離開這個作爲一個評論,但在這條線:

if($cu_user == $user_id || $admin_ch) { 

將其更改爲:

if($cu_user == $user_id || $cu_user == $admin_ch) { 

它被寫入,之後或沒有爭論。你也需要在那裏發表一個論點。

而且適當的功能是:

get_the_author_meta('ID') 
+0

我使用這個但不適用於我 – Hobo

+0

這看起來很簡單,但是您確定沒有更改管理員的ID嗎?出於安全原因,這是一個很好的做法。也許在我們繼續之前仔細檢查一下。 – JD2381

+0

是的,我確定,我沒有更改管理員的ID – Hobo

相關問題