简体   繁体   English

我的关系循环出了什么问题?

[英]What's wrong with my relationship loop?

I'm having a problem with a relationship loop with Advanced Custom Fields in Wordpress. 我在与Wordpress中的“高级自定义字段”建立关系循环时遇到问题。

The first post is shown perfect, but it should execute one more post, but it doesn't. 第一篇文章显示为完美,但应该再执行一次,但事实并非如此。

Can anyone see what's wrong with my code? 谁能看到我的代码出了什么问题?

<?php $posts = get_field('produkter'); if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>

<div class="produkt">
<?php the_title(); ?>
<?php the_content(); ?>

<?php $posts = get_field('fil'); if( $posts ): ?>

<div id="filer">
<div id="topsection" class="laddahem"><div class="topborder"></div>
<h2>Ladda hem</h2>
</div><!-- #top -->

<div class="filhuvud">
    <div class="filtyp">Filtyp</div>
    <div class="fildatum">Datum</div>
    <div class="filstorlek">Filstorlek</div>
</div><!-- .filhuvud -->

<div class="filholder">

<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>

<?php $attachment_id = get_field('filen');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );

// hämta filstorleken
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 1);

$filetype = strtolower(pathinfo($url, PATHINFO_EXTENSION)); ?>

<div class="fil <?php echo $filetype; ?>">
    <div class="filtyp"><a target="_blank" href="<?php echo $url; ?>" ><?php the_title(); ?></a></div>
    <div class="fildatum"><?php the_time('Y-m-d'); ?></div>
    <div class="filstorlek"><?php echo $filesize; ?></div>
</div><!-- .fil -->

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

</div><!-- .filholder -->

</div><!-- #filer --> 

<?php endif; ?>

</div><!-- .produkt -->

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Your issue is that you're overwriting the $posts variable within the loop, after you've defined it outside of the loop. 您的问题是,在循环外定义$posts变量后,您将在循环内覆盖它。

The solution is to rename your second $posts variable to something else, so that your overarching loops look like this: 解决方案是将第二个$posts变量重命名为其他名称,以便您的总体循环如下所示:

  <?php $posts = get_field('produkter'); ?>

  <?php if( $posts ): ?>
    <?php foreach( $posts as $post): ?>


      <?php $files = get_field('fil'); //RENAMED THIS VARIABLE?>
      <?php if( $files ): ?>

         <?php foreach( $files as $file): ?>

               <?php //Main body of markup and other fun stuff ?>

         <?php endforeach; //end $files foreach ?>

      <?php endif; //end $files if ?>

     <?php endforeach; //end $posts foreach ?>
  <?php endif; //end $posts if ?>

The solution was to change the other foreach loop to this: 解决的办法是将另一个foreach循环更改为此:

<?php $files = get_field('fil'); if( $files ): ?>
<?php foreach( $files as $post): ?>
<?php setup_postdata($post); ?>

That made it work exactly how I wanted it. 这使它完全按照我想要的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM