duanquan4451 2014-11-12 17:21
浏览 41
已采纳

我的关系循环有什么问题?

I'm having a problem with a relationship loop with Advanced Custom Fields in 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; ?>
  • 写回答

2条回答 默认 最新

  • doulongti5932 2014-11-13 09:51
    关注

    The solution was to change the other foreach loop to this:

    <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题