【问题标题】:ACF Relatioshionship doesn't appear if i don't put the code before all others wordpress loop如果我不将代码放在所有其他 wordpress 循环之前,则不会出现 ACF 关系
【发布时间】:2019-03-15 17:56:00
【问题描述】:

我尝试在我的 wordpress 主页上显示 ACF 关系字段。 在我的主页上有多个查询循环来显示来自特定类别的帖子。

如果我将代码放在页面顶部,它会起作用。但是当我尝试在我的页面中间插入我的代码时,在一个查询循环之后,什么都没有出现。我想我的查询循环有冲突。我在每个循环上使用重置发布数据。

有什么线索吗?

这是我的代码:

<?php $posts = get_field('relationnel');

if($posts): 
?>
<h3>Articles Similaires</h3>

<div class="container">
<div class="row">   
<?php foreach( $posts as $post): // ne pas changer $post IMPORTANT 
setup_postdata($post); 
?>
<div class="col-lg-4 col-md-6 col-sm-6"> 
<div class="col-lg-12 wrapper-archives ">
<h2>        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<br></h2>
        <h3>        <?php the_field('date'); ?>
<br></h3>
<div class="sommaire">
<?php // check if the repeater field has rows of data
if( have_rows('sommaire') ): while ( have_rows('sommaire') ) : the_row(); ?>
<?php  // display a sub field value the_sub_field('titre'); ?> </br>  
<?php  endwhile; else : // no rows found
endif;?>
</div>  </div> </div>
<?php endforeach; ?>

</div>
</div>
<?php 
    wp_reset_postdata(); // IMPORTANT - réinitialiser l'objet $post sur la 
requête principale
endif; 
?>

如果我在查询循环之后发布上面的代码,则不会出现任何内容。这是我的查询循环

<div id="owl-example" class="owl-carousel">
<?php query_posts( array( 'posts_per_page' => 3,) ); ?>

<?php while ( have_posts() ) : the_post();?>
<?php if (has_post_thumbnail( $post->ID ) ): ?> 
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID 
), 'single-post-thumbnail' ); ?>

<div class="owl-slide" style="background-image: url('<?php echo $image[0]; 
>');">
<div class="owl--text">
<span class="categorie-home"><?php the_category (); ?></span>   
<?php the_title (); ?>  </div>
</div>          <?php endif; ?>

 <?php endwhile;  // 4. On réinitialise à la requête principale (important)
 wp_reset_postdata();
 ?>
 </div>

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    我通过在我的查询循环中放置 wp_reset_query 代替 wp_reset_postdata 来解决问题

     <?php wp_reset_query(); ?>
    

    我现在的查询循环:

    <div id="owl-example" class="owl-carousel">
    <?php query_posts( array( 'posts_per_page' => 3,) ); ?>
    
    <?php while ( have_posts() ) : the_post();?>
    <?php if (has_post_thumbnail( $post->ID ) ): ?> 
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID 
    ), 'single-post-thumbnail' ); ?>
    
    <div class="owl-slide" style="background-image: url('<?php echo $image[0]; 
    >');">
    <div class="owl--text">
    <span class="categorie-home"><?php the_category (); ?></span>   
    <?php the_title (); ?>  </div>
    </div>          <?php endif; ?>
    
    <?php endwhile;  // 4. On réinitialise à la requête principale (important)
    
    ?>
    <?php wp_reset_query(); ?>
    
    </div>
    

    效果很好!

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多