不同柱样式的自定义立柱

时间:2014-06-01 作者:nCore

我正在尝试输出一个自定义类型的帖子,在第8列(缩略图)和第4列(内容)中显示粘性/特色作品,然后其余的帖子应该在第4列。到目前为止,我已经尝试了下面的代码,但只输出了所有的帖子,当然,如果我重复代码并将输出包含在第4列中,这是可以实现的,但我认为这不是最好的做法。

<?php
        $loop = new WP_Query( array( \'post_type\' => \'recentproject\', \'orderby\' => \'rand\', \'posts_per_page\' => \'1\') );

        ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <div class="col-8 columns">
            <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
            <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                <?php echo get_the_post_thumbnail(); ?>
            <?php endif; ?>
        </div>

        <div class="col-4 columns">
            <h4 class="project_title"><?php the_title(); ?></h2>
            <?php echo the_content(); ?>
        </div>
    <?php endwhile; ?>
    <div class="clear"></div>
    <div class="row">
        <?php
        $loop = new WP_Query( array( \'post_type\' => \'recentproject\', \'orderby\' => \'rand\', \'posts_per_page\' => \'3\') );
        ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-4 columns">
                <h4 class="project_title"><?php the_title(); ?></h4>
                <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
                <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                    <?php echo get_the_post_thumbnail(); ?>
                <?php endif; ?>
                <?php echo the_content(); ?>
            </div>
        <?php endwhile; ?>
    </div>
要解释我打算做什么,请参见下图。enter image description here

1 个回复
最合适的回答,由SO网友:Gareth Gillman 整理而成

您需要使用offset WP\\U查询中的参数,例如。

<?php
        $loop = new WP_Query( array( \'post_type\' => \'recentproject\', \'orderby\' => \'rand\', \'posts_per_page\' => \'1\') );
        $counter=0;
        ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); $counter++; ?>
        <div class="col-8 columns">
            <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
            <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                <?php echo get_the_post_thumbnail(); ?>
            <?php endif; ?>
        </div>

        <div class="col-4 columns">
            <h4 class="project_title"><?php the_title(); ?></h2>
            <?php echo the_content(); ?>
        </div>
    <?php endwhile; ?>
    <div class="clear"></div>
    <div class="row">
        <?php
        $loop = new WP_Query( array( \'post_type\' => \'recentproject\', \'orderby\' => \'rand\', \'posts_per_page\' => \'3\', \'offset\' => \'1\' ) );
        ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-4 columns">
                <h4 class="project_title"><?php the_title(); ?></h4>
                <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
                <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                    <?php echo get_the_post_thumbnail(); ?>
                <?php endif; ?>
                <?php echo the_content(); ?>
            </div>
        <?php endwhile; ?>
    </div>
基本上,它会忽略查询中的前xx篇文章,因此如果要忽略查询中的第一篇文章,可以使用offset=>1

我不认为它会与“兰德”订单一起工作,但兰德是必要的吗?Edit: Using count & 1 query

<?php
$loop = new WP_Query( array( \'post_type\' => \'recentproject\', \'orderby\' => \'rand\', \'posts_per_page\' => \'4\') );
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
 if($count == \'1\') { ?>
  <div class="col-8 columns">
   <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
   <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    <?php echo get_the_post_thumbnail(); ?>   
   <?php endif; ?>
  </div>

  <div class="col-4 columns">
   <h4 class="project_title"><?php the_title(); ?></h4>
    <?php echo the_content(); ?>
  </div>
 <?php } else { ?>

  <div class="col-4 columns">
   <h4 class="project_title"><?php the_title(); ?></h4>
   <?php edit_post_link(); // Always handy to have Edit Post Links available ?>
    <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
     <?php echo get_the_post_thumbnail(); ?>
    <?php endif; ?>
    <?php echo the_content(); ?>
   </div>

  <?php
  }
 $count++;
endwhile;
wp_reset_postdata();
?>
未测试,但应该可以工作,基本上它会统计帖子,并根据计数更改HTML代码

结束

相关推荐

PHPUnit测试WordPress插件

使用这个网站上的答案和其他资源,我已经开始使用PHPUnit测试和WordPress测试环境编写下一个插件。提取插件中的内容bootstrap.php:define( \'WP_TESTS_DIR\', \'pathToMyWordPressTestsFromSVN\'); define( \'TEST_PLUGIN_FILE\', \'pathToMyPlugin/myPlugin.php\' ); require_once WP_TESTS_DIR . \'includes/funct