Почему мой WP-цикл повторяет содержимое после его закрытия? - PullRequest
0 голосов
/ 07 октября 2018

Я проверил свой код на предмет того, почему мой цикл повторяет содержимое, отображаемое на странице.Я знаю, что это как-то связано с моим завершающим циклом while, но мне не удалось выяснить, что является причиной проблемы.Я был бы признателен за любые свежие взгляды, которые смотрят на это и говорят мне, что я сделал неправильно.Заранее спасибо!

<?php
/*
Template Name: Blog Page Template
*/
?>

<?php get_header(); ?>
<section class="component" role="main">
<header class="header">
<h1 class="entry-title">Blog</h1>
</header>
<section class="component">

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$args = array(
   'posts_per_page' => 7,
   'paged' => $paged
);

$custom_query = new WP_Query( $args );
$post_number = 0;


if ( have_posts() ) : while($custom_query->have_posts()) : 
    $custom_query->the_post();

    $modone_qry = new WP_Query( $args );
    $post_number = 0;

    if ( $modone_qry->have_posts() ) : while ( $modone_qry->have_posts() ) : $modone_qry->the_post();

    $post_number++;

    if ($post_number === 1): 

    if ( has_post_thumbnail()) {
    // do stuff if there is a post thumbnail
    ?>

            <div class="recent_articles_hero" style="background: url('<?php the_post_thumbnail_url(); ?>'); background-size: cover;">
                <div class="block_item_overlay"></div>
                <div class="recent_articles_content">
                    <h4 class="section_label"><?php echo get_the_date('F j, Y'); ?></h4>
                    <h1><?php the_title(); ?></h1>
                    <a href="<?php the_permalink(); ?>" class="button_styling">
                        Read More
                    </a>
                </div>
            </div>
        </section>

    <?php } else {
    // do stuff if there is not a post thumbnail
    ?>

            <div class="recent_articles_hero default_post_preview">
                <div class="block_item_overlay"></div>
                <div class="recent_articles_content">
                    <h4 class="section_label"><?php echo get_the_date('F j, Y'); ?></h4>
                    <h1><?php the_title(); ?></h1>
                    <a href="<?php the_permalink(); ?>" class="button_styling">
                        Read More
                    </a>
                </div>
            </div>
        </section>

    <?php } ?>

    <section id="articles_wrapper" class="block_items_container">
    <?php else: 

        if ( has_post_thumbnail()) {
        // do stuff if there is a post thumbnail
        ?>  

            <div class="block_item article" style="background: url('<?php the_post_thumbnail_url(); ?>'); background-size: cover; background-position: 50%;">
                <div class="block_item_overlay"></div>
                <h4 class="section_label"><?php echo get_the_date('F j, Y'); ?></h4>
                <div class="block_item_content">
                    <h3><?php the_title(); ?></h3>
                    <p><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 12 ) ); ?></p>
                    <a href="<?php the_permalink(); ?>" class="button_styling">Read More</a>
                </div>
            </div>


        <?php } else {
        // do stuff if there is not a post thumbnail
        ?>

            <div class="block_item article default_post_preview">
                <h4 class="section_label"><?php echo get_the_date('F j, Y');?></h4>
                <div class="block_item_content">
                    <h3><?php the_title(); ?></h3>
                    <p><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 12 ) ); ?></p>
                    <a href="<?php the_permalink(); ?>" class="button_styling">Read More</a>
                </div>
            </div>

        <?php } ?>
        <?php endif; ?>

    <?php endwhile; wp_reset_query(); else : ?>
<?php endif; ?>
    <div class="clear"></div>
    <?php if (function_exists("pagination")) {
        pagination($custom_query->max_num_pages);
    } ?>
    </section>
</section>

1 Ответ

0 голосов
/ 07 октября 2018

Вы повторяете свой запрос, и в то время как цикл два раза также меняет вашу первую строку

<?php
$paged = get_query_var(  'page' ,1 );
$args = array(
'posts_per_page' => 7,
'paged' => $paged
 );
 $custom_query = new WP_Query( $args );
 $post_number = 0;
 if ( have_posts() ) : while($custom_query->have_posts()) : 
     $custom_query->the_post();
      if ($post_number === 1): 
      if ( has_post_thumbnail()) {
     // do stuff if there is a post thumbnail
  ?>
...