分开第一篇文章并忽略重复

时间:2015-01-13 01:06:38

标签: php wordpress

我循环浏览定义类别中的前几个帖子,第一个帖子与其余帖子分开,但第一个帖子仍然包含在内。我需要$ i 2号等等,不要包括已经显示的第一个帖子。

我认为$ i会有某种突破。我尝试将$ i切换为2,作为我现在正在玩的启动器。

enter image description here

<div class="article-block-v1">
<?php
//get terms (category ids 11,2,33,34), then display one post in each term
$taxonomy = 'category';//  e.g. post_tag, category
$param_type = 'category__in'; //  e.g. tag__in, category__in
$term_args=array(
  'include' => '1459',
  'orderby' => 'date',
  'order' => 'DESC'
);
$terms = get_terms($taxonomy,$term_args);

if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      );
    $my_query = null;
    $my_query = new WP_Query($args);

    $i = 1;

    if( $my_query->have_posts() ) {
    echo '<ul>';
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <?php if ($i == 1): ?>

    <div class="article-block-v1">

    <div class="category-label-large category-news-analysis">
        <a href="<?php the_permalink(); ?>"><p><?php echo $term->name; ?></p></a>
    </div>
    <div class="view2 third-effect">
    <?php the_post_thumbnail(); ?>
        <div class="mask">
            <a href="<?php the_permalink() ?>" class="info" ><i class="fa fa-search"></i></a>
        </div>
    </div>
    <ul class="homepagewhitebg">
        <li><h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5></li>
    </ul>

    </div>

    <?php $i = 2; ?>

    <?php endif; ?>

    <li><a href="<?php the_permalink(); ?>"><p><?php the_title(); ?></p></a></li>

    <?php
        $i++;
          endwhile;
          echo '</ul>';
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
?>

1 个答案:

答案 0 :(得分:0)

我最后对它进行了排序,认为我只需要在$ i不再等于1之后再做一个else语句,因此显示剩余的帖子。也增加了每页的帖子数。

<div class="article-block-v1">
<?php
//get terms (category ids 11,2,33,34), then display one post in each term
$taxonomy = 'category';//  e.g. post_tag, category
$param_type = 'category__in'; //  e.g. tag__in, category__in
$term_args=array(
  'include' => '1459',
  'orderby' => 'date',
  'order' => 'DESC'
);
$terms = get_terms($taxonomy,$term_args);

if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 4,
      );
    $my_query = null;
    $my_query = new WP_Query($args);

    $i = 1;

    if( $my_query->have_posts() ) {
    echo '<ul>';
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <?php if ($i == 1): ?>

    <div class="article-block-v1">

    <div class="category-label-large category-news-analysis">
        <a href="<?php the_permalink(); ?>"><p><?php echo $term->name; ?></p></a>
    </div>
    <div class="view2 third-effect">
    <?php the_post_thumbnail(); ?>
        <div class="mask">
            <a href="<?php the_permalink() ?>" class="info" ><i class="fa fa-search"></i></a>
        </div>
    </div>
    <ul class="homepagewhitebg">
        <li><h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5></li>
    </ul>

    </div>

    <?php else: ?>

    <li><a href="<?php the_permalink(); ?>"><p><?php the_title(); ?></p></a></li>

    <?php endif; ?>

    <?php
        $i++;
          endwhile;
          echo '</ul>';
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
?>