嵌套for循环,每3个项目创建2个HTML行,并使用外部HTML包装器包装每2行

时间:2015-08-12 08:25:47

标签: php html wordpress advanced-custom-fields

所以基本上我正在构建一个wordpress库,我需要将图像输出为2行,每行是3个图像,所以基本上我只是每3分割输出..

我有这个功能使用模数运算符使用for循环,我需要的是每2行我需要将它们包装在外部HTML容器中

<div class="wrappy"> 

但我似乎无法在昨天的大部分时间内完成这项工作,我在PHP上的表现并不是很好但是我理解得足以让所有的帮助都很棒...我正在使用wordpress高级定制用于存储图像等的字段,以便您可以忽略该部分,我正在处理的魔术开始于

<div class="wrappy">

                <?php

                $rows = get_field('staff_slides', 'options');
                $countmax = count($rows) - 1;

                //echo $row_count; 

                $ender = "";
                $mainEnder = "";
                $outer_wrapper = "";

                if( have_rows('staff_slides', 'options') ):

                    // loop through the rows of data
                    while ( have_rows('staff_slides', 'options') ) : the_row();

                        $image_id = get_sub_field('slide_image', 'options');

                        $staff_members_name = get_sub_field('staff_members_name', 'options');

                        $staff_members_position = get_sub_field('staff_members_position', 'options');

                        $staff_image = wp_get_attachment_image_src( $image_id , 'homepage-staff');


                ?>






                                <?php 


                    echo '<div class="wrappy">';


                                    echo '<div class="row">'; 

                                        if( $ender != "script-ended" )  {

                                            for( $i=0; $i <= $countmax; ) 
                                            { 

                                ?>

                                    <div class="staff-img c3">
                                        <div class="staff-caption">
                                            <h3><?php echo $rows[$i]['staff_members_name']; ?></h3>
                                                <span></span>
                                            <h4><?php echo $rows[$i]['staff_members_position']; ?></h4>
                                        </div>
                                            <img src="<?php echo wp_get_attachment_image_src( $rows[$i]['slide_image'], 'homepage-staff')[0]; ?>">
                                    </div>

                                <?php
                                        if( $i % 3 == 2 )  
                                        {


                                                echo '</div><div class="row">';
                                            }

                                        if( $i == $countmax ) 
                                        {
                                                $ender = "script-ended";                
                                            }


                                        if( $i == 6){


                                            $outer_wrapper = "set";

                                        }   





                                        $i++;

                                        }
                                    }

                                ?>

                <?php 

                if( $outer_wrapper == "set" ){

                    echo '</div><div class="wrappy">';

                }


                ?>

1 个答案:

答案 0 :(得分:1)

您应该使用array_chunk函数将数组拆分为较小的数组。 我不熟悉ACF功能,但您可以执行以下操作:

Sub reconwebscrap() ' ' reconwebscrap Macro ' ' Keyboard Shortcut: Ctrl+Shift+R
    Dim requestsearchrange As Range
    Dim cell1 As Range
    Dim cell2 As Range
    Dim entire As Range
    Dim IE As Object
    Dim revocdate As String
    Dim i As Integer
    Dim tags As Object
    Dim tagx As Object
    Dim tags2 As Object
    Dim tagsx As Object

    Application.DisplayStatusBar = True

    i = 0

    With ActiveWorkbook.Sheets(2)
        Set requestsearchrange = .Range(.Range("B2"), .Range("B2").End(xlDown))
    End With

    ActiveWorkbook.Worksheets.Add

    With ActiveWorkbook.Sheets(3)
        Set entire = .Range(.Range("A1"), .Range("A65536").End(xlUp))
    End With
the_start:

    Set IE = New InternetExplorerMedium

    'Set IE = CreateObject("InternetExplorer.Application")
    '-----------------------------------------------------------------------------------------------------------------
    'These attributes decide the position of internet explorer window.
    '-----------------------------------------------------------------------------------------------------------------

    IE.Top = 0
    IE.Left = 0
    IE.Width = 800
    IE.Height = 600

    '-----------------------------------------------------------------------------------------------------------------
    'Disable the viewing of Internet Explorer window.
    '-----------------------------------------------------------------------------------------------------------------

    IE.Visible = True

    '-----------------------------------------------------------------------------------------------------------------
    'Navigate to the website.
    '-----------------------------------------------------------------------------------------------------------------

    IE.Navigate ("https://ibid.abc.com/RMT/MyDashboard")

    '-----------------------------------------------------------------------------------------------------------------
    'Let the website load completely.
    'Error handling in case the website is not available.
    '-----------------------------------------------------------------------------------------------------------------
    Do Until Not IE.Busy
        DoEvents
        Application.StatusBar = " Running"
    Loop

    'Do
        'DoEvents
            'If Err.Number <> 0 Then
                'IE.Quit
                'Set IE = Nothing
                'GoTo the_start:
            'End If
    'Loop Until IE.readystate = 4

    MsgBox "webpage has loaded"

    revocdate = InputBox("enter the last revocation date")

    Set tags = IE.document.getElementsByTagName("img")
    'Set tags2 = IE.document.getElementById("dashboardSelect")

    For Each cell1 In requestsearchrange
        IE.document.getElementById("dashboardSelect").Value = "recipientSid"
        IE.document.getElementById("quickSearchCriteriaVar").Value = cell1.Value
        For Each tagx In tags
            If tagx.alt = "Search Request" Then
                tagx.Click
            End If
        Next tagx

        Do Until Not IE.Busy
            DoEvents
        Loop

        i = i + 1
        Application.StatusBar = i & " Running"

    Next cell1

    Application.StatusBar = ""
End Sub

这样你就可以创建一个多维数组,分成3个元素的块,然后迭代它。