将特权用户数组与我当前的查询相结合

时间:2014-10-14 作者:Jimmy

我有一个带有以下查询的自定义页面模板。同位素文件管理器是我的分类,所有这些都很好。我现在想让这个页面只被少数人看到specific, currently logged in authors. 示例:帖子仅对具有这些ID之一(1、2或3)的作者可见,并且已登录

    <ul id="filters">
    <li><a href="#" data-filter="*">Everything</a></li>
    <?php 
        $terms = get_terms("category"); // get all categories, but you can use any taxonomy
        $count = count($terms); //How many are they?
        if ( $count > 0 ){  //If there are more than 0 terms
            foreach ( $terms as $term ) {  //for each term:
                echo "<li><a href=\'#\' data-filter=\'.".$term->slug."\'>" . $term->name . "</a></li>\\n";
                //create a list item with the current term slug for sorting, and name for label
            }
        } 
    ?>
</ul>

<?php $the_query = new WP_Query( \'posts_per_page=20\' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
    <div id="isotope-list">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
    $termsArray = get_the_terms( $post->ID, "category" );  //Get the terms for this particular item
    $termsString = ""; //initialize the string that will contain the terms
        foreach ( $termsArray as $term ) { // for each term 
            $termsString .= $term->slug.\' \'; //create a string that has all the slugs 
        }
    ?> 
    <div class="<?php echo $termsString; ?> item">

        <?php the_title(); ?>

    </div> <!-- end item -->
    <?php endwhile;  ?>
    </div> <!-- end isotope-list -->
<?php endif; ?>
我可以使用以下方法实现特权用户的数组:但我不知道如何将其与上面列出的查询相结合。

    <?php
$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user

// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) {
    $query = array(
         \'posts_per_page\' => 20,
         \'author__in\' = $authors
     );
    $the_query = new WP_Query( $query );
}
?>

<?php if ( $query ) : ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<?php the_title(); ?>

<?php endwhile;?>
<?php else : ?>
This message will be displayed if the signed in user is not in the authors array.
<?php endif; ?>

2 个回复
最合适的回答,由SO网友:Jimmy 整理而成

未来帮助他人的有效解决方案:

    <ul id="filters">

  <li><a href="#" data-filter="*">Everything</a></li>

  <?php 

        $terms = get_terms("category"); // get all categories, but you can use any taxonomy

        $count = count($terms); //How many are they?

        if ( $count > 0 ){  //If there are more than 0 terms

            foreach ( $terms as $term ) {  //for each term:

                echo "<li><a href=\'#\' data-filter=\'.".$term->slug."\'>" . $term->name . "</a></li>\\n";

                //create a list item with the current term slug for sorting, and name for label

            }

        } 

    ?>

</ul>

<?php

$authors = array( 1, 2, 3 ); // privileged users array

$signed_in_user = wp_get_current_user(); // get current user

// check if singed in user is in authors array

if ( in_array( $signed_in_user->ID, $authors ) ) {

$query = array(

         \'posts_per_page\' => 20,

         \'author__in\' = $authors

);

?>

<?php $the_query = new WP_Query( $query ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<div id="isotope-list">

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 

    $termsArray = get_the_terms( $post->ID, "category" );  //Get the terms for this particular item

    $termsString = ""; //initialize the string that will contain the terms

        foreach ( $termsArray as $term ) { // for each term 

            $termsString .= $term->slug.\' \'; //create a string that has all the slugs 

        }

    ?>

  <div class="<?php echo $termsString; ?> item">

    <?php the_title(); ?>

  </div>

  <!-- end item -->

  <?php endwhile;  ?>

  <?php else: ?>

  This message will be displayed if the signed in user is not in the authors array.

  <!-- end isotope-list -->

<?php endif; ?>

</div>

<?php } ?>
谢谢你的提问。com:)

SO网友:bysanchy

只需在代码之前添加此项

$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user

// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) :
然后在你的代码之后

<?php else : ?>
This message will be displayed if the signed in user is not in the authors array.
<?php endif; ?>

结束

相关推荐

WP_QUERY ORDERBY在WP4.0更新后不起作用

在我的Wordpress网站上,我有一个用于显示事件的自定义帖子类型和一个显示数据库中所有事件的页面。在该页面上,我创建了一个新的WP\\u查询,然后立即显示结果。 $q_events = new WP_Query(array(\'post_type\' => \'lc_events\', \'orderby\' => \'ID\')); if ($q_events->have_posts()) : while ( $q_events->have_posts(