显示时的ACF中继器字段和Clearfix /样式

时间:2020-07-08 20:28:16

标签: wordpress bootstrap-4 advanced-custom-fields repeater clearfix

第一次发布,虽然将这些论坛用作资源已有一段时间了……希望您能提供帮助。

我对PHP事情不屑一顾,想知道您是否有任何想法

这里是情况,首先是“背景故事”;

我通常在Wordpress&Bootstrap中工作。我在这里找到了一些代码,可以帮助我设置帖子类型的格式,以便我可以将帖子类型用作图库(例如),并在其中运行clearfix来整理行中的每一行,并防止图像彼此崩溃。这就是我一直在使用的

<?php $args=array(
'post_type' => ‘post-name',
'posts_per_page'      => '100',
'orderby'=> 'date', 'order' => 'ASC' );
$my_query = null;
$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
// modified to work with 3 columns
// output an open <div>
if($i % 4 == 0) { ?> 

<div class="row">

<?php }?>

IMAGE / CONTENT THAT REPEATS GOES IN HERE

<?php $i++; if($i != 0 && $i % 4 == 0) { ?>

</div>
<div class="clearfix"></div>

<?php } ?>
<?php endwhile;} wp_reset_query(); ?>

就像我说的那样,当图像可能会破坏页面流时,这会应用clearfix div并在行的结尾处赋予权利。 到现在为止还挺好。问题出在这里。

最近,我一直在使用“高级自定义字段”插件的自定义“中继器”字段(您基本上可以将图像堆积到堆栈中,但它们不再是与以上)是我用来将其拉入的

<?php if(get_field(‘repeater-field-name')): ?>
<?php while(has_sub_field('repeater-field-name')): ?>

IMAGE / CONTENT THAT REPEATS GOES IN HERE

<?php endwhile; ?>
<?php endif; ?> 

这也很好。 因此,我的工作基本上是将重复的字段代码与clearfix代码合并。

是否有任何简单的方法来修改clearix代码以包括转发器字段? 有更好的方法吗?

基本上任何帮助都值得赞赏。

谢谢!

编辑-有关更多信息,这是我要尝试使用的实际代码,在这种情况下,它不是后继类型,只是利用中继字段; 这是我正在尝试的代码。它不是一个post类型,而是一个Repeater字段,因此按照上面的示例(第二个代码块),它将在没有任何样式的情况下进行重复。因此,简而言之,我试图将一个无限的clearfix(如第一个代码块,用于发布类型)应用于转发器字段。我一直试图将两者结合起来。也许我正走错路了?

<div class="container">
 <div class="row">
  <div class="main_cbrand roomy-100 gallery">

<?php if(get_field('artwork')): ?>
<?php while(has_sub_field('artwork')): ?>

<?php if( get_sub_field('image') ): ?>

<div class="col-md-2 col-sm-4 col-xs-6" style="padding-bottom: 20px;">
<a title="<?php the_sub_field('title'); ?>" href="<?php the_sub_field('image');             ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php the_sub_field('title');   ?>" title="<?php the_sub_field('title'); ?>"></a>
  </div>

<?php endif; ?>
<?php endwhile; ?><?php endif; ?> 
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

轻松地,您可以采用组织化的方式;)-示例:

<?php 
$args = array(
    'post_type'     => 'post',
    'posts_per_page'=> '100',
    'orderby'       => 'date', 
    'order'         => 'ASC' 
);
$query = new WP_Query($args);


// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        

        // collect item data
        $pid = get_the_ID();
        $rpt = get_field('some_name', $pid);

        // build repeater data
        $counter = 0;
        $rptData = '';
        if( !empty($rpt) ) {
            foreach($rpt as $item) {

                // 4 condition
                $rtpData .= ($counter % 4 == 0 ? '<div class="row">' : '');
                
                // make what you want with info inside the item here
                $rptData .= '<div class="col-md-3">'.$item['something'].'</div>'; 

                $rtpData .= ($counter % 4 == 0 ? '</div><div class="clearfix"></div>' : '');

                $counter++;
            }
        };


        echo '
        <div>
            <h3>I AM THE WRAPPER</h3>
            <div class="container">
                '.$rptData.'
            </div>
        </div>
        ';
    }
}
/* Restore original Post Data */
wp_reset_postdata();
?>


编辑:
1号您甚至需要clearfix的原因是.row div不是这些列的直接父级。否则,您不需要任何clearfix。

  1. 我不明白这是如何工作的。.'the_sub_field('image')'确定吗?

  2. 如果您真的考虑过,不同的分辨率每行会有不同的列数,那么clearfix将如何提供帮助? (请参阅第1条评论)

尽管如此。.;

<div class="container">
    <div class="row">
        <div class="main_cbrand roomy-100 gallery">

            <?php 
            if(get_field('artwork')){

                $counter = 0;

                while(has_sub_field('artwork')) { 
                    ?>

                    <?php if( get_sub_field('image') ): ?>

                        <div class="col-md-2 col-sm-4 col-xs-6" style="padding-bottom: 20px;">
                            <a title="<?php the_sub_field('title'); ?>" href="<?php the_sub_field('image'); ?>">
                                <img src="<?php the_sub_field('image'); ?>" alt="<?php the_sub_field('title');   ?>" title="<?php the_sub_field('title'); ?>">
                            </a>
                        </div>

                        <?php echo ($counter % 4 == 0 ? '<div class="clearfix"></div>' : ''); ?>


                    <?php endif; ?>
                    
                    <?php 
                    $counter++;
                }
            } 
            ?> 
        </div>
    </div>
</div>

您可以更改它在此行中出现的频率

echo ($counter % 4 == 0 ? '<div class="clearfix"></div>' : '');