循环通过ACF字段时,Foreach循环中的块错误消息

时间:2021-04-20 作者:DCEXC

我有2种自定义帖子类型(使用CPT UI和ACF创建)“;“全球”;和;“个人”;。其目的是让用户参考;“全球”;通过将该全球帖子的图像(链接)显示在;“个人”;他们正在创建的帖子。

循环工作,当它到达包含图像信息的字段时,将显示图像。但是,代码继续运行并显示错误,如;foreach()无效;等等。我基本上需要一个;“如果”;语句maybe或指示循环在找到要查找的信息时停止的内容。或任何其他建议,将不胜感激!

示例-“全球”帖子可能完全不同”;比萨饼基地;然后,用户想要创建自己的比萨饼,因此他们从显示在ACF关系字段中的“global”中选择某个比萨饼基地。

他们通过创建一个“个人”帖子来实现这一点,在这里他们可以将所有浇头添加到所选的基础上(通过其他acf字段),然后一旦创建,它就成为他们的“个人”比萨饼,存储为“个人”帖子类型。

希望这是有意义的!

代码如下

function show_global_pics_in_personal_library(){
    $variablename = get_field(\'dcfg_global_item_select\'); ?>
    <div class="container">
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'dchdl_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'dcstd_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'dcthdl_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'drcfg_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'drchdl_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field(\'drcstd_global_item_select\'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
    </div>
    <?php
}

add_shortcode(\'show_global_pics_in_personal\', \'show_global_pics_in_personal_library\');

1 个回复
最合适的回答,由SO网友:DCEXC 整理而成

对于那些正在寻找答案的人,经过反复试验,我发现;“如果”;我一直在寻找的声明。完整代码如下:

    
    $variablename = get_field(\'dcfg_global_item_select\');

        if ( !empty( $variablename ) ) {?>

            <div class="container">

                <?php foreach($variablename as $variablename2):?>

                        <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                            <center>
                            <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                            </center>
                        </a>

                <?php endforeach;
                                       }?>
        
        <?php $variablename = get_field(\'dchdl_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>
                
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>
        
                        <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                            <center>
                            <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                            </center>
                        </a>

                    <?php endforeach;
                                            }?>
        
        <?php $variablename = get_field(\'dcstd_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field(\'dcthdl_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>

                <div class="container">

                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field(\'drcfg_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field(\'drchdl_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field(\'drcstd_global_item_select\');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, \'medium\');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>


    </div>

<?php }?>

<?php add_shortcode(\'show_global_pics_in_personal\', \'show_global_pics_in_personal_library\'); 

相关推荐

<?php the_post_thbumnail_url(‘Medium’);?>不输出中等大小的图像URL

我目前正在尝试输出具有不同图像大小的帖子缩略图URL;在ssrcset中使用。我当前的代码是:<img class="productimage lazyload" data-src="<?php the_post_thumbnail_url(\'medium\'); ?>" /> 但是,这总是输出全尺寸图像,而不是“中等”。此外,我还尝试使用第一个字符串“null”或post ID;但这仍然不起作用。是否有任何方法可以输出带有特定图