在网格中捕获第 4 和第 8 篇博客文章 Wordpress 和分页友好


Catch 4th and 8th blog post in a grid Wordpress & pagination friendly

我正在尝试实现一个分页友好功能,以捕获帖子编号等于最近帖子列表中的第 4 个和第 8 个帖子,并显示带有时事通讯订阅表单的div。

            $count == 0;
    ob_start();
    query_posts( $args );
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            $count++;
            if ($count === 3 || $count === 7) {
            echo "Success!";
            }
            $post_format = et_pb_post_format();
            $thumb = '';
            $width = 'on' === $fullwidth ? 1080 : 400;
            $width = (int) apply_filters( 'et_pb_blog_image_width', $width );
            $height = 'on' === $fullwidth ? 675 : 250;
            $height = (int) apply_filters( 'et_pb_blog_image_height', $height );
            $classtext = 'on' === $fullwidth ? 'et_pb_post_main_image' : '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
            $thumb = $thumbnail["thumb"];
            $no_thumb_class = '' === $thumb || 'off' === $show_thumbnail ? ' et_pb_no_thumb' : '';
            if ( in_array( $post_format, array( 'video', 'gallery' ) ) ) {
                $no_thumb_class = '';
            } 

            ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' . $no_thumb_class . $overlay_class  ); ?>>
        <?php
            et_divi_post_format_content();
            if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
                if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
                    printf(
                        '<div class="et_main_video_container">
                            %1$s
                        </div>',
                        $first_video
                    );
                elseif ( 'gallery' === $post_format ) :
                    et_pb_gallery_images( 'slider' );
                elseif ( '' !== $thumb && 'on' === $show_thumbnail ) :
                    if ( 'on' !== $fullwidth ) echo '<div class="et_pb_image_container">'; ?>
                        <a href="<?php esc_url( the_permalink() ); ?>" class="entry-featured-image-url">
                            <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                            <?php if ( 'on' === $use_overlay ) {
                                echo $overlay_output;
                            } ?>
                        </a>
                <?php
                    if ( 'on' !== $fullwidth ) echo '</div> <!-- .et_pb_image_container -->';
                endif;
            } ?>
        <?php if ( 'off' === $fullwidth || ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) { ?>
            <?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) { ?>
                <h2 class="entry-title"><a href="<?php esc_url( the_permalink() ); ?>"><?php the_title(); ?></a></h2>
            <?php } ?>
            <?php
                if ( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories || 'on' === $show_comments ) {
                    printf( '<p class="post-meta">%1$s %2$s %3$s %4$s %5$s %6$s <span class="comments">%7$s</span></p>',
                        (
                            'on' === $show_author
                                ? et_get_safe_localization( sprintf( __( 'by %s', 'et_builder' ), '<span class="author vcard">' .  et_pb_get_the_author_posts_link() . '</span>' ) )
                                : ''
                        ),
                        (
                            ( 'on' === $show_author && 'on' === $show_date )
                                ? '  '
                                : ''
                        ),
                        (
                            'on' === $show_date
                                ? et_get_safe_localization( sprintf( __( '%s', 'et_builder' ), '<span class="published">' . esc_html( get_the_date( $meta_date ) ) . '</span>' ) )
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date ) && 'on' === $show_categories)
                                ? '  '
                                : ''
                        ),
                        (
                            'on' === $show_categories
                                ? get_the_category_list(', ')
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories ) && 'on' === $show_comments)
                                ? ' | '
                                : ' '
                        ),
                        (
                            'on' === $show_comments
                                ? sprintf( esc_html( _nx( '1 Comment', '%s Comments', get_comments_number(), 'number of comments', 'et_builder' ) ), number_format_i18n( get_comments_number() ) )
                                : ''
                        )
                    );
                }
                echo '<div class="post-content">';
                $post_content = get_the_content();
                // do not display the content if it contains Blog, Post Slider, Fullwidth Post Slider, or Portfolio modules to avoid infinite loops
                if ( ! has_shortcode( $post_content, 'et_pb_blog' ) && ! has_shortcode( $post_content, 'et_pb_portfolio' ) && ! has_shortcode( $post_content, 'et_pb_post_slider' ) && ! has_shortcode( $post_content, 'et_pb_fullwidth_post_slider' ) ) {
                    if ( 'on' === $show_content ) {
                        global $more;
                        // page builder doesn't support more tag, so display the_content() in case of post made with page builder
                        if ( et_pb_is_pagebuilder_used( get_the_ID() ) ) {
                            $more = 1;
                            the_content();
                        } else {
                            $more = null;
                            the_content( esc_html__( 'read more...', 'et_builder' ) );
                        }
                    } else {
                        if ( has_excerpt() ) {
                            the_excerpt();
                        } else {
                            echo wpautop( truncate_post( 270, false ) );
                        }
                    }
                } else if ( has_excerpt() ) {
                    the_excerpt();
                }
                if ( 'on' !== $show_content ) {
                    $more = 'on' == $show_more ? sprintf( ' <a href="%1$s" class="more-link" >%2$s</a>' , esc_url( get_permalink() ), esc_html__( 'read more', 'et_builder' ) )  : '';
                    echo $more;
                }

                echo '</div>';
                ?>
        <?php } // 'off' === $fullwidth || ! in_array( $post_format, array( 'link', 'audio', 'quote', 'gallery' ?>
        </article> <!-- .et_pb_post -->
<?php
        } // endwhile

我设法在每个帖子上显示一个测试跨度,但是,它完全忽略了我的 if 语句。我应该在此循环中的哪个位置包含 if 语句?

我使用以下方法解决了这个问题:

if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            $count++;
            $post_format = et_pb_post_format();
            $thumb = '';
            $width = 'on' === $fullwidth ? 1080 : 400;
            $width = (int) apply_filters( 'et_pb_blog_image_width', $width );
            $height = 'on' === $fullwidth ? 675 : 250;
            $height = (int) apply_filters( 'et_pb_blog_image_height', $height );
            $classtext = 'on' === $fullwidth ? 'et_pb_post_main_image' : '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
            $thumb = $thumbnail["thumb"];
            $no_thumb_class = '' === $thumb || 'off' === $show_thumbnail ? ' et_pb_no_thumb' : '';
            if ( in_array( $post_format, array( 'video', 'gallery' ) ) ) {
                $no_thumb_class = '';
            } 

            ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' . $no_thumb_class . $overlay_class  ); ?>>
        <?php
            et_divi_post_format_content();
            if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
                if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
                    printf(
                        '<div class="et_main_video_container">
                            %1$s
                        </div>',
                        $first_video
                    );
                elseif ( 'gallery' === $post_format ) :
                    et_pb_gallery_images( 'slider' );
                elseif ( '' !== $thumb && 'on' === $show_thumbnail ) :
                    if ( 'on' !== $fullwidth ) echo '<div class="et_pb_image_container">'; ?>
                        <a href="<?php esc_url( the_permalink() ); ?>" class="entry-featured-image-url">
                            <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                            <?php if ( 'on' === $use_overlay ) {
                                echo $overlay_output;
                            } ?>
                        </a>
                <?php
                    if ( 'on' !== $fullwidth ) echo '</div> <!-- .et_pb_image_container -->';
                endif;
            } ?>
        <?php if ( 'off' === $fullwidth || ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) { ?>
            <?php
                if ( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories || 'on' === $show_comments ) {
                    printf( '<div class="post-wrapper"><p class="post-meta">%1$s %2$s %3$s %4$s %5$s <span class="comments">%6$s</span></p></div>',
                        (
                            'on' === $show_author
                                ? et_get_safe_localization( sprintf( __( 'By: %s', 'et_builder' ), '<span class="vcard2"></span><span class="author">' .  et_pb_get_the_author_posts_link() . '</span>' ) )
                                : ''
                        ),
                        (
                            ( 'on' === $show_author && 'on' === $show_date )
                                ? '  '
                                : ''
                        ),
                        (
                            'on' === $show_date
                                ? et_get_safe_localization( sprintf( __( '%s', 'et_builder' ), '<span class="published">' . esc_html( get_the_date( $meta_date ) ) . '</span>' ) )
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date ) && 'on' === $show_categories)
                                ? '  '
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories ) && 'on' === $show_comments)
                                ? ' | '
                                : ' '
                        ),
                        (
                            'on' === $show_comments
                                ? sprintf( esc_html( _nx( '1 Comment', '%s Comments', get_comments_number(), 'number of comments', 'et_builder' ) ), number_format_i18n( get_comments_number() ) )
                                : ''
                        )
                    );
                } 
                ?>
            <?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) { ?>
                <h2 class="entry-title"><a href="<?php esc_url( the_permalink() ); ?>"><?php the_title(); ?></a></h2>
            <?php 
                printf( '<span class="categories">%1$s</span>',
                        ('on' === $show_categories
                            ? get_the_category_list(', ')
                            : ''
                        )
                      );
                } 
            ?>

                <?php
                echo '<div class="post-content">';
                $post_content = get_the_content();
                // do not display the content if it contains Blog, Post Slider, Fullwidth Post Slider, or Portfolio modules to avoid infinite loops
                if ( ! has_shortcode( $post_content, 'et_pb_blog' ) && ! has_shortcode( $post_content, 'et_pb_portfolio' ) && ! has_shortcode( $post_content, 'et_pb_post_slider' ) && ! has_shortcode( $post_content, 'et_pb_fullwidth_post_slider' ) ) {
                    if ( 'on' === $show_content ) {
                        global $more;
                        // page builder doesn't support more tag, so display the_content() in case of post made with page builder
                        if ( et_pb_is_pagebuilder_used( get_the_ID() ) ) {
                            $more = 1;
                            the_content();
                        } else {
                            $more = null;
                            the_content( esc_html__( 'read this post', 'et_builder' ) );
                        }
                    } else {
                        if ( has_excerpt() ) {
                            the_excerpt();
                        } else {
                            echo wpautop( truncate_post( 270, false ) );
                        }
                    }
                } else if ( has_excerpt() ) {
                    the_excerpt();
                }
                if ( 'on' !== $show_content ) {
                    $more = 'on' == $show_more ? sprintf( ' <a href="%1$s" class="more-link" >%2$s</a>' , esc_url( get_permalink() ), esc_html__( 'Read this post', 'et_builder' ) )  : '';
                    echo $more;
                }
                echo '</div>';
                ?>
        <?php } // 'off' === $fullwidth || ! in_array( $post_format, array( 'link', 'audio', 'quote', 'gallery' ?>
        </article> <!-- .et_pb_post -->
        <?php
                if ($count == 4 || $count == 8) {
                    dynamic_sidebar('newsletter-box');
                }
        ?>
<?php
        } // endwhile

需要进行一些研究,但结果正是我需要的。