WordPress查询内部查询的奇怪行为

WordPress query inside query weird behaviour

提问人:COBNETCKNN 提问时间:11/14/2023 最后编辑:COBNETCKNN 更新时间:11/14/2023 访问量:31

问:

所以我正在尝试在循环中的第二个帖子后为广告添加卡片,它加载正常,但不知何故,主查询正在从不同的帖子类型为同一帖子制作卡片......

主要查询:

<?php 

//query to load selected featured post on front page
$postsPerPage = 12;
$args = array(
    'post_type' => 'post',
    'posts_per_page' => $postsPerPage,
);

$blogPostQuery = new WP_Query($args);

$i = 1;
while($blogPostQuery->have_posts()){
    $blogPostQuery->the_post(); 
    
    if($i == 3) {

     get_template_part('partials/ads', 'post'); 

    }
    
    ?>

    <?php $post_id = get_the_ID(); ?>

    <div class="blogCardBlackOverlay post-<?php echo $post_id; ?>">
        <div class="col-span-1 shadow-2xl">
            <?php echo $i; ?>
        <?php 
            $thumb = get_the_post_thumbnail_url(); 
            
        ?>
            <div class="relative blogPostCard rounded-xl" style="background-image: linear-gradient(rgba(66,32,6,0.7) 0%, rgb(134, 191, 255,0.3) 130%), url('<?php echo $thumb;?>')">
            <h1 class="blogPostCard_title font-sans text-white font-bold text-start"><?php the_title(); ?></h1>
            <!-- Gettng custom taxonomies associate with teh post -->
            <?php
                $terms = get_the_terms($post_id, 'acquisition');

                if ($terms && !is_wp_error($terms)) {
                    $first_term = reset($terms); ?>
                    <span class="blogCard_taxonomy__item py-1 px-4 text-sm rounded-2xl absolute bottom-4 right-4 font-medium item-<?php echo $first_term->slug; ?>"><?php echo $first_term->name; ?></span>
                <?php
                }
            ?>
            <!-- Reading time -->
            <div class="blogPost_readingTime__wrapper">
                <?php 
                $readingTime = get_field('reading_time');
                ?>
                <div class="blogPost_readingTime text-white text-avenir absolute bottom-4 left-4">
                    <i class="fa-regular fa-lightbulb"></i>
                    <span><?php echo $readingTime; ?></span>
                </div>
            </div>
            <a href="#" type="button" class="view-post" data-postid="<?php the_ID(); ?>" data-slug="<?php echo get_post_field('post_name', $post_id); ?>"></a>
            </div>
        </div>
    </div>
<?php
$i++;
wp_reset_postdata();
}
?>

子查询模板:

<?php 

// Query to load ads on front-page inside while loop for posts
$adArgs = array(
    'post_type'      => 'ads', // Replace 'your_custom_post_type' with the actual name of your custom post type
    'posts_per_page' => 1,
    'orderby'        => 'rand', // Order by random
);

$adsQuery = new WP_Query($adArgs);

while($adsQuery->have_posts()){
    $adsQuery->the_post(); ?>

        <div class="col-span-1 my-auto">
        <?php

        if( have_rows('ads_group') ):

        while( have_rows('ads_group') ): the_row();

        $adsLogo = get_sub_field('logo');
        $adsLogoSize = 'full';
        $adsText = get_sub_field('ad_description');
        $adsButtonText = get_sub_field('button_text');
        $adsButtonLink = get_sub_field('button_link'); 
        $adsButtonBackgroundColor = get_sub_field('button_background_color');
        ?>
        
        <div class="ads_logo__wrapper flex justify-center my-2 max-height-[65px]">
            <?php
            if( $adsLogo ) {
                echo wp_get_attachment_image( $adsLogo, $adsLogoSize );
            }
            ?>
        </div>
        <div class="ads_text__wrapper flex justify-center font-medium text-base text-center font-avenir tetxt-base text-avenir my-2">
            <?php echo $adsText; ?>
        </div>
        <div class="ads_button__wrapper flex justify-center my-2">
            <a class="text-white font-avenir font-bold py-1.5 px-4 text-sm rounded-lg" href="<?php echo $adsButtonLink ?>" style="background-color:<?php echo $adsButtonBackgroundColor; ?>"><?php echo $adsButtonText; ?></a>
        </div>

        <?php 
        endwhile;
        endif;
        ?>
        
        </div>
    <?php
    }
    wp_reset_postdata();
    ?>

为了更好地理解,图片:https://prnt.sc/ioPNwLnR4be0

以绿色突出显示的是自定义帖子类型和子查询的正确输出,但随后主查询以某种方式从自定义帖子类型为同一帖子制作相同的明信片。

PHP wordpress 帖子

评论

0赞 Snuffy 11/14/2023
在末尾添加 if($i == 3) { get_template_part('partials/ads', 'post');
0赞 Snuffy 11/14/2023
wp_reset_postdata();应该不在 while($blogPostQuery->have_posts()){}

答: 暂无答案