简体   繁体   English

WordPress的:创建2个循环。 首先是一个即时贴或最新帖子。 所有其他职位第二

[英]Wordpress: creating 2 loops. First for one sticky OR the latest post. Second for all other posts

I'm new to Wordpress and PHP and I've been battling with this for a while now. 我是Wordpress和PHP的新手,并且已经为此奋斗了一段时间。 What I have for the moment is 2 loops. 目前,我有2个循环。

I want to first loop to only ever show 1 post: either a sticky post, or the latest post if there are no sticky posts. 我想首先循环以仅显示1条帖子:粘性帖子,或者如果没有粘性帖子,则显示最新帖子。

The second loop should show all other recent posts, like a basic blog, except any posts which are stickied or the recent post which appears in the first loop. 第二个循环应显示所有其他最近的帖子,例如基本博客,但所有粘贴的帖子或出现在第一个循环中的最近的帖子除外。

What I have: 我有的:

My code shows the latest post in the first loop and other posts in the second loop. 我的代码在第一个循环中显示了最新帖子,在第二个循环中显示了其他帖子。

The problem: 问题:

When I make a sticky post, it appears in the first loop along with the latest post (I only want the sticky post to appear here in this situation and not both), and it also appears at the top of the second loop. 当我发布即时贴时,它与最新帖子一起出现在第一个循环中(我只希望即时贴在这种情况下出现在此处,而不是两者都出现),并且它也出现在第二个循环的顶部。

Here it is in action: http://bit.ly/1qS0MMC 它正在起作用: http : //bit.ly/1qS0MMC

I've managed to get this far but I can't seem to finish it. 我已经设法做到了这一点,但似乎无法完成。

<div id="main" class="site-main">
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <div id="featuredsection">
               <?php $args = array(
                  'posts_per_page' => 1,
                     );
                   $my_query = new WP_Query( $args );
                   while ( $my_query->have_posts() ) : $my_query->the_post();
               $do_not_duplicate = $post->ID; ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class( '' ); ?> >
                /*---Featured post template ---*/
                </div>
               <?php endwhile; ?>
            </div>
<div id="gridcontainer">
<?php
    $counter = 1; //start counter
    $grids = 2; //Grids per row
    global $query_string; //Need this to make pagination work

if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( $post->ID == $do_not_duplicate ) continue; ?>

<?php
//Show the left hand side column
    if($counter == 1) :
?>
        <div id="post-<?php the_ID(); ?>" <?php post_class( 'griditemleft' ); ?> >
            /*---Left column post template ---*/
        </div>
<?php
//Show the right hand side column
elseif($counter == $grids) :
?>
        <div id="post-<?php the_ID(); ?>" <?php post_class( 'griditemright' ); ?>>
            /*---Right columnpost template ---*/
        </div>

<div class="clear"></div>
<?php
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
//Pagination can go here if you want it.
endif;
?>
<?php
untitled_content_nav( 'nav-below' );
?>
    </div>
</div>

Would it be easier to create separate instances of WP_Query and loop for each post block required? 为所需的每个post块创建单独的WP_Query实例并循环会更容易吗?

You can then pass your $arg parameters such as 'ignore_sticky_posts' => 1 too your required WP_Query loop. 然后,您也可以传递$ arg参数(例如'ignore_sticky_posts'=> 1)作为必需的WP_Query循环。

http://codex.wordpress.org/Class_Reference/WP_Query http://codex.wordpress.org/Class_Reference/WP_Query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM