wordpress ACF if have_rows()不返回任何内容

9njqaruj  于 2023-06-21  发布在  WordPress
关注(0)|答案(3)|浏览(122)

我试着用一个中继场,但似乎不能让它工作。我认为这是我的if语句的问题,因为如果我删除while循环并尝试从<?php if( have_rows($aboutInfo['cards']): ?>中回显任何内容,我什么也得不到。我试过不带ID,用硬编码的ID作为第二个参数。另外,为了测试,我做了<?php if( !have_rows($aboutInfo['cards']): ?>,并能够得到一些东西来回显。
if语句上面的print_r显示数组。

<?
 /*
 Template Name: 01-Homepage
 */
 get_header(); ?>
 <? $aboutInfo = get_field( 'about' ) ?>
 <?$postid = get_the_ID(); ?>
 <div class="row">
   <div class="columns small-12 medium-7">
     <h2>
       <?= $aboutInfo['title'] ?>
     </h2>
     <p class="lead"> <?= $aboutInfo['content'] ?></p>
     <pre><?php print_r($aboutInfo['cards']) ?></pre>
     <?php if( have_rows($aboutInfo['cards'], $postid) ): ?>
       <?php while(have_rows($aboutInfo['cards'])) : the_row(); ?>
       <?php $image = get_sub_field('image') ?>
       <p><?= $image['url'] ?></p>
       <?php endwhile; ?>
     <?php endif; ?>
   </div>
 </div>
 <?php get_footer(); ?>

下面是我的ACF的外观

iyfjxgzm

iyfjxgzm1#

我认为你做错了。你的代码里有很多bug。检查https://www.advancedcustomfields.com/resources/group/have_rows(),第一个参数需要是选择器。检查下面的代码。

<?php 

/* Template Name: 01-Homepage */

get_header(); 

$aboutInfo = get_field( 'about' );
$postid    = get_the_ID();

if( have_rows('about') ): 

    $title   = get_sub_field('title');
    $content = get_sub_field('content');

    ?>

    <div class="row">

        <div class="columns small-12 medium-7">

            <?php while( have_rows( 'about' ) ): the_row(); ?>

                <h2><?php echo $title; ?></h2>
                <p class="lead"><?php echo $content; ?></p>
                
                <?php if( have_rows( 'cards' ) ): while( have_rows( 'cards' ) ) : the_row(); ?>

                        <?php $image = get_sub_field( 'image' ); ?>

                        <img src="<?php echo $image['url']; ?>" />

                <?php endwhile; endif;

            endwhile; ?>    

        </div>

    </div>

<?php endif;

get_footer(); ?>
hujrc8aj

hujrc8aj2#

问题是我创建了一个名为“about”的组,“cards”嵌套在该组中,要访问该字段,我需要使用“about_cards”。

<?
 /*
 Template Name: 01-Homepage
 */
 get_header(); ?>
​
 <?php while ( have_posts() ) : the_post();
​
    // group field
    $about = get_field( 'about' ); 
    
    if ( !empty( $about ) ) { ?>
​
        <div class="row">
            <div class="columns small-12 medium-7">
​
            <?php if ( !empty( $about['title'] ) ) { ?>
                <h2><?php echo esc_html( $about['title'] ); ?></h2>
            <?php }
            
            if ( !empty( $about['content'] ) ) { ?>
                <p class="lead"><?php echo wp_kses_post( $about['content'] ); ?></p>
            <?php }
​
            if( have_rows( 'about_cards' ) ) : // repeater
​
                while ( have_rows( 'about_cards' ) ) : the_row(); 
​
                    $about_card_image   = get_sub_field('image');
                    $about_card_title   = get_sub_field('title');
                    $about_card_content = get_sub_field('content');
​
                    if ( !empty( $about_card_image ) ) {
                        echo wp_get_attachment_image( $about_card_image, 'medium' );
                    }
​
                    if ( !empty( $about_card_title ) ) {
                        echo '<h3>' . esc_html( $about_card_title ) . '</h3>';
                    }
​
                    if ( !empty( $about_card_content ) ) {
                        echo '<p>' . esc_html( $about_card_content ) . '</p>';
                    } ?>
​
                <?php endwhile;
            endif; ?>
            </div>
        </div>
​
    <?php } // about field not empty ?>
​
<?php endwhile; // End of the loop. ?>
​
<?php get_footer(); ?>
brccelvz

brccelvz3#

<?php 

/* Template Name: 01-Homepage */

get_header(); 

$aboutInfo = get_field( 'about' );
$postid    = get_the_ID();

if( have_rows('about') ): 

    $title   = get_sub_field('title',$postid);
    $content = get_sub_field('content',$postid);

    ?>

    <div class="row">

        <div class="columns small-12 medium-7">

            <?php while( have_rows( 'about',$postid) ): the_row(); ?>

                <h2><?php echo $title; ?></h2>
                <p class="lead"><?php echo $content; ?></p>
                
                <?php if( have_rows( 'cards' ) ): while( have_rows( 'cards',$postid ) ) : the_row(); ?>

                        <?php $image = get_sub_field( 'image' ); ?>

                        <img src="<?php echo $image['url']; ?>" />

                <?php endwhile; endif;

            endwhile; ?>    

        </div>

    </div>

<?php endif;

get_footer(); ?>

相关问题