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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算