簡體   English   中英

修改主循環以在用戶注冊日期之后顯示帖子

[英]Modify main loop to show posts after date user registered

我已經玩了幾天了,卻沒有成功。

我想修改index.php,以便主循環僅顯示在登錄用戶在站點上注冊后發布的帖子(對於未登錄的用戶,其內容已隱藏)

我可以獲取注冊日期,也可以修改循環,以使其顯示某個日期之后的帖子,但不能一起執行。

誰能幫我解決這個問題? 這是我到目前為止的代碼-

    <?php $regdate = date("Y-m-d", strtotime(get_userdata(get_current_user_id( ))->user_registered)); ?>

        <?php
        function filter_where($where = '') {
        $where .= " AND post_date >= '2016-02-18'";
        return $where;
        }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        ?>

這看起來像是WP Query的候選者, 它已經建立了可通過日期查詢的參數。

在您提供的情況下,您的代碼將如下所示:

<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array 
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query        
$args = array(
    'date_query' => array(
        array(
            'after'    => array(
                # Setting date to array above allows to call specific values within that date    
                'year'  => $regdate['year'],
                'month' => $regdate['month'],
                'day'   => $regdate['day'],
            ),
            # Include posts from the day the user registered  
            'inclusive' => true,
        ),
    ),
    # Display all posts on a single page.
    'posts_per_page' => -1,
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="post post-<?php the_ID(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
    <h2 class="blog__custom-content-title"><?php the_title();?></h2>
    <?php the_content();?>
</article>
<?php endwhile; ?>
<?php else: ?>
    <h2>No posts to display</h2>
<?php endif; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM