【问题标题】:Conditional date option for php function - wordpress (ACF) pluginphp 函数的条件日期选项 - wordpress (ACF) 插件
【发布时间】:2019-03-14 09:36:05
【问题描述】:

这是我第一次在 Stack Overflow 上发布内容,我的 php 编程知识确实存在漏洞。尽管如此,如果有人可以帮助我解决以下问题,我将不胜感激。

我创建了一个项目列表,每个项目都有一个 1) 标题、2) 链接、3) 日期和 4) 描述,这些项目是通过 wordpress 网站上的 ACF 插件输入的。我在下面的网站前端包含了显示此列表的代码 - 请注意,有一系列列表意味着在主列表(称为父转发器)中有嵌套列表(称为子转发器)。

但是。 . .我希望在嵌套列表上方创建一个附加列表,其中包含 30 天更新的任何条目(输入到子转发器)。所以在(父转发器)列表中创建 2 个嵌套(子转发器)列表。

所以。 . .我需要创建一个有条件的“if”语句,分离出 30 天以上的“项目列表详细信息”(例如)并将它们放在(子转发器)列表中,然后是一个“else”,其中所有“项目列表详细信息” " 超过 30 天的时间将显示在单独的(子转发器)列表中。

请有人解释我如何在下面的代码中生成此条件 - 仅供参考,此代码当前成功运行并在(父中继器)列表中生成嵌套(子中继器)列表,但我需要拆分(子中继器)将列表分为 2 个单独的列表 - 一个包含 30 天后的项目,另一个包含所有其他项目(超过 30 天)。

<?php 
    // check for rows (parent repeater)
    if( have_rows('membership_lists_links') ): ?>
        <?php 
        // loop through rows (parent repeater)
        while( have_rows('membership_lists_links') ): the_row(); ?>
           <div>
                <h2 class="acf-admin"><?php the_sub_field('item_list_title'); ?></h2>
                <?php 
                // check for rows (sub repeater)
                if( have_rows('item_list_details') ): ?>
                    <ul class="admin">
                    <?php
                    // loop through rows (sub repeater)
                    while( have_rows('item_list_details') ): the_row() 
                        // display each item as a list
                        ?>
                           <li><div class='itemTitle'>
                                    <?php $link = get_sub_field('link_url'); if( $link ): ?>
                                    <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="<?php echo $link['title']; ?>">
                                    <?php endif; ?>
                                    <?php the_sub_field('link_name'); ?>
                                    <?php $link = get_sub_field('link_url'); if( $link ): ?>
                                    </a>
                                    <?php endif; ?>
                                </div>
                                <div class="memberListDate">
                                    <?php the_sub_field('date'); ?>
                                </div>
                                <br/>
                                <div class="itemDescription">
                                    <?php the_sub_field('link_description'); ?>
                                </div>
                            </li>   
                    <?php endwhile; ?>
                    </ul>
                <?php endif; //if( get_sub_field('section_details') ): ?>
            </div>
        <?php endwhile; // while( has_sub_field('business_sections') ): ?>

我认为开始的代码部分是 . . .

<?php // check for rows (sub repeater)
if( have_rows('item_list_details') ): ?>

是日期年龄条件需要发生的地方。然后我可以创建一个超过 30 天的项目列表,然后是一个“else”,其中包含一个超过 30 天的所有项目的列表。

我希望我已经对自己进行了充分的解释,并且非常感谢为实现这一目标提供的任何帮助。正如我之前所说,我的 php 知识确实存在漏洞,但希望通过与 Stack Overflow 社区互动,我可以学到更多知识,并希望能帮助其他人。

谢谢

菲尔

【问题讨论】:

    标签: php wordpress date conditional advanced-custom-fields


    【解决方案1】:

    中继器值存储在数组中,因此我将在中继器上使用 foreach 循环,而不是使用 ACF have_rows()the_row() 函数 - 我认为这样更容易理解。

    此外,此答案将取决于您如何设置日期字段。如果您使用内置的日期选择器字段,请在字段设置中选择 Ymd 作为返回格式。如果您以相同的格式提取当前日期,则可以比较这些值:

    <?php 
    $membershipListLinks = get_field('membership_lists_links');
    
    $lastMonth = date('Ymd', strtotime('-30 days'));
    
    // check for rows (parent repeater)    
    if( $membershipListLinks ): ?>
        <?php 
        // loop through rows (parent repeater)
        foreach( $membershipListLinks as $membershipListLink ): 
            $title = $membershipListLink['item_list_title'];
            $details = $membershipListLink['item_list_details'];
            ?>
            <div>
                <h2 class="acf-admin"><?php echo $title; ?></h2>
                <?php 
                // check for rows (sub repeater)
                if( $details ): 
                    // create some arrays
                    $older = array();
                    $newer = array();
                    foreach( $details as $detail ): 
                        $date = $detail['date']; // make sure to return in Ymd format
                        // add the items to one of the empty arrays
                        if( $date < $lastMonth ) {
                            $older[] = $detail;
                        } else {
                            $newer[] = $detail;
                        }
                        $arrays = array($older, $newer);
                        foreach( $arrays as $array ):
                            if( !empty($array) ):
                            ?>
                            <ul>
                                <?php
                                foreach( $array as $detail ):
                                    $link = $detail['link_url'];
                                    $name = $detail['link_name'];
                                    $date = $detail['date'];
                                    $description = $detail['link_description'];
                                    // display each item as a list
                                    ?>
                                    <li>
                                        <div class="itemTitle">
                                            <?php if( $link ): ?>
                                            <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="<?php echo $link['title']; ?>">
                                            <?php endif; ?>
                                            <?php echo $name; ?>
                                            <?php if( $link ): ?>
                                            </a>
                                            <?php endif; ?>
                                        </div>
                                        <div class="memberListDate">
                                            <?php echo $date; ?>
                                        </div>
                                        <br/>
                                        <div class="itemDescription">
                                            <?php echo $description; ?>
                                        </div>
                                    </li>   
                                <?php endforeach; ?>
                            </ul>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
    

    编辑:

    <?php 
    $membershipListLinks = get_field('membership_lists_links');
    
    $lastMonth = date('Ymd', strtotime('-30 days'));
    
    // check for rows (parent repeater)    
    if( $membershipListLinks ): ?>
        <?php 
        // loop through rows (parent repeater)
        foreach( $membershipListLinks as $membershipListLink ): 
            $title = $membershipListLink['item_list_title'];
            $details = $membershipListLink['item_list_details'];
            ?>
            <div>
                <h2 class="acf-admin"><?php echo $title; ?></h2>
                <?php 
                // check for rows (sub repeater)
                if( $details ): 
                    // create some arrays
                    $older = array();
                    $newer = array();
                    foreach( $details as $detail ) { 
                        $date = $detail['date']; // make sure to return in Ymd format
                        // add the items to one of the empty arrays
                        if( $date < $lastMonth ) {
                            $older[] = $detail;
                        } else {
                            $newer[] = $detail;
                        }
                    }    
                    $arrays = array('older' => $older, 'newer' => $newer);
                    foreach( $arrays as $key => $value ):
                        if( !empty($value) ):
                        ?>
                        <ul class="<?php echo ($key == 'older') ? 'older' : 'newer'; // change as needed ?>">
                            <?php
                            foreach( $value as $detail ):
                                $link = $detail['link_url'];
                                $name = $detail['link_name'];
                                $date = $detail['date'];
                                $description = $detail['link_description'];
                                // display each item as a list
                                ?>
                                <li>
                                    <div class="itemTitle">
                                        <?php if( $link ): ?>
                                        <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="<?php echo $link['title']; ?>">
                                        <?php endif; ?>
                                        <?php echo $name; ?>
                                        <?php if( $link ): ?>
                                        </a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="memberListDate">
                                        <?php echo $date; ?>
                                    </div>
                                    <br/>
                                    <div class="itemDescription">
                                        <?php echo $description; ?>
                                    </div>
                                </li>   
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
    

    【讨论】:

    • 嗨,约翰尼。非常感谢您的代码。有几个问题可以吗?
    • @PhilLegg 拍摄
    • 嗨,约翰尼。非常感谢您的代码。有几个问题可以吗? 1) 就日期格式而言 - 我返回 j M Y - 这是一个问题吗? - 2) 我想在它交换到另一个列表之前的天数可以调整 - 因此如果我要让一个名为“latest_additions”的 ACF 字段的天数我可以去:$lastMonth = date('Ymd', strtotime( get_sub_field('latest_additions') ) ); - 和 3)如果我想以不同的方式设置新列表的样式。如何使用您的编码方法为新列表创建不同的类?谢谢菲尔
    • 1) 使用 Ymd 作为返回值,以便您进行比较。当需要输出日期时,您可以将其更改为您想要的任何日期(此处的高级示例:advancedcustomfields.com/resources/date-picker)2)是的,如果值为“-30 天”,那应该可以工作 3)简单的方法是在每个数组上运行 foreach 循环,而不是创建 $arrays 数组
    • 嗨,Johnny 正试图让这个工作。 RE 问题 3)关于“最新”列表的样式:我可以不保留您提供的代码编辑,而是在
        $arrays = array($older, $newer); foreach( $arrays as $array ): if( !empty($array) ): ?> <ul <?php if ( $date < $lastMonth ) { echo "class='latest_Addition'";} ?>>
    猜你喜欢
    • 2014-06-12
    • 2020-10-13
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2020-11-23
    • 2017-03-16
    • 1970-01-01
    • 2015-11-21
    相关资源
    最近更新 更多