在存档循环中创建WordPress循环

时间:2017-01-30 02:45:32

标签: php wordpress

我正在使用WordPress存档页面。我目前有一个循环,按月分隔我的所有图片帖子,但由于它在循环中生成月份名称的方式,我无法按照我需要的方式控制它并且难以理解如何处理它。

我需要为每个月创建一个围绕所有img的div。理想情况下它会像:

月份名称

(新div)

  • 图像
  • 图像
  • 图像

(/ new div)

月份名称

(新div)

  • 图像
  • 图像
  • 图像

(/ new div)

...等

任何帮助都会非常感激

<?php       
$ref_month = '';
$monthly = new WP_Query(array('posts_per_page' => -1));
if( $monthly->have_posts() ) :
while( $monthly->have_posts() ) : $monthly->the_post(); ?>

    <?php 
    $year_year = get_the_date('Y');
    $year_month = get_the_date('m'); ?>

    <a href="<?php echo get_month_link( $year_year, $year_month ); ?>">
        <?php
        if( get_the_date('mY') != $ref_month ) {
            if( $ref_month ) ?>

                <div class="month_name">
                    <?php echo get_the_date('F'); ?>
                </div>

            <?php
            $ref_month = get_the_date('mY');
        } ?>

        <?php 
        $year_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'year_image' ); ?>
        <img src="<?php echo $year_image[0]; ?>" height="200px" width="200px" style="display:inline"/>
    </a>

<?php
endwhile; ?>

</ul>

<?php
endif; 
?>

2 个答案:

答案 0 :(得分:1)

参考教程:http://callmenick.com/post/create-a-wordpress-custom-archive-page

  <?php

    global $post;

    $archive_args = array(
      'post_type' => 'post', // get only posts
      'posts_per_page'=> -1 // this will display all posts on one page
    );

    $archive_query = new WP_Query( $archive_args );
    $year_year     = get_the_date('Y');
    $year_month    = get_the_date('m');

  ?>

  <div class="loop-archive">

    <?php $date_old = ''; ?>

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

      <?php $date_new = get_the_time("F Y"); ?>

      <?php if ( $date_old != $date_new ) : // run the check on $date_old and $date_new, and output accordingly ?>
        </div><!-- close the div -->
        <h4>
        <a href="<?php echo get_month_link( $year_year, $year_month ); ?>"> 
        <?php echo $date_new; ?>
        </a>
        </h4>
        <div style="border:1px dotted red;margin-bottom: 3%"><!-- open the div -->
      <?php endif; ?>

      <div style="display:inline">
      <a href="<?php echo the_permalink(); ?>"><?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?></a>
      </div>

      <?php $date_old = $date_new; // update $date_old ?>

      <?php endwhile; // end the custom loop ?>

  </div> <!-- /loop-archive -->

  <?php wp_reset_postdata(); // always reset post data after a custom query ?>

enter image description here

答案 1 :(得分:0)

@mikekavouras如果有人想看,请帮我清理解决方案。我能够在每个月内生成帖子数量,这样我就可以根据有多少帖子创建不同的网格模式和所有不同的背景图像

global $post;

$archive_args = array(
    'post_type' => 'post', // get only posts
    'posts_per_page'=> -1 // this will display all posts on one page
);
$archive_query = new WP_Query( $archive_args );
$formatted_data = []; ?>

<?php while ( $archive_query->have_posts() ) : $archive_query->the_post(); 
    $date_new = get_the_time("M Y"); 
    $key = $date_new;

    if (! isset($formatted_data[$date_new])) {
        $formatted_data[$date_new] = [];
    }

    array_push($formatted_data[$date_new], $post); ?>
<?php endwhile; 

// loop through each month
$keys = array_keys($formatted_data);
for ($i = 0; $i < count($keys); $i++): ?>
    <?php 
    $month = $keys[$i]; 
    $posts = $formatted_data[$month]; 
    $num_posts = count($posts);
    $explosion = explode(" ", $month);
    $monthNum = array(
        "Jan" => 1,
        "Feb" => 2, 
        "Mar" => 3,
        "Apr" => 4,
        "May" => 5,
        "Jun" => 6,
        "Jul" => 7,
        "Aug" => 8,
        "Sep" => 9,
        "Oct" => 10,
        "Nov" => 11,
        "Dec" => 12
    );
    $month_abbr = $explosion[0];
    $year_month = $monthNum[$month_abbr];
    $year_year = $explosion[1]; ?>

    <div class="archive-month">
        <a href="<?php echo get_month_link( $year_year, $year_month ); ?>">

            <div class="<?php echo "archivebox archivebox--" . $num_posts; ?>">

                <div class="archive-name">
                    <h2><?php echo $month; ?></h2>
                </div>

                <?php for ($j = 0; $j < count($posts); $j++): ?> 
                    <?php 
                    $p = $posts[$j];
                    $thumb_id = get_post_thumbnail_id($p->ID);
                    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail');

                    <!-- you have the post do whatever you want -->
                    <div class="archive-background" style="background-image:url('<?php echo $thumb_url[0]; ?>');"></div>


                <?php endfor; ?>
            </div>
        </a>
    </div>

<?php 
endfor; ?>