loop - How do I go about looping through a advanced custom field on a particular page inside of another page

admin2025-06-04  45

I have one page lets call it page-home.php and I also have a page called page-custom-field.php I want to have a section of the first page where it loops through page-custom-field.php page and display it on itself. Here is my code attempting that so far. I am using advanced custom fields on the second page.

My If statements

page-home.php

 <?php
        $args = array(
            'post_type' => array(
                'features'
            ),
            'posts_per_page' => 1,

          );
        $query = new WP_Query( $args );
      ?>

    <div id="homeSpecials" class="section__content small-12 small-centered medium-10 columns">
    <?php  if($query -> have_posts()) : while( $query->have_posts() ) : $query->the_post(); ?>
              <?php include 'page-custom-field.php' ?>
      <?php endwhile; endif;//ends'menu_flexible'?>

page-custom-field.php

   <?php if( have_rows('menu_item') ): ?>
        <?php while( have_rows('menu_item') ): the_row(); ?>
            <!-- loop through the rows of data || DONT CLOSE THE LOOP IN AN 'IF' STATEMENT -->
            <?php if( get_row_layout() == 'simple_menu_item' ): ?>
                <article class="menu-item small-12 medium-6 columns">
                    <h3><?php the_sub_field('item_name') ?><span class="menu-item__price"><?php the_sub_field('item_price') ?></span></h3>
                    <p>
                        <?php the_sub_field('item_description') ?>
                    </p>
                </article>
            <!-- elseif is the last loop -->
            <?php elseif( get_row_layout() == 'line_item_with_modifiers' ):  ?>
                <article class="menu-item small-12 columns">
                <h3><?php the_sub_field('item_name') ?><span class="menu-item__price"><?php the_sub_field('item_price') ?></span></h3>
                <p>
                    <?php the_sub_field('item_description') ?>

                </p>
                <?php if( have_rows('item_modifiers') ): ?>
                    <?php while( have_rows('item_modifiers') ): the_row(); ?>
                        <div class="small-12 medium-3 columns">
                            <h4><?php the_sub_field('modifier_title') ?></h4>
                            <ul>
                                <?php if( have_rows('modifier_column') ): ?>
                                    <?php while( have_rows('modifier_column') ): the_row(); ?>
                                        <li><?php the_sub_field('modifier_name') ?></li>
                                    <?php endwhile;?>
                                <?php endif;?>
                            </ul>

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


                </article>


            <!-- final 'endif' closes the flexible content loop -->
            <?php  endif; ?>
        <?php endwhile; endif;//ends'menu_flexible'?>

I have one page lets call it page-home.php and I also have a page called page-custom-field.php I want to have a section of the first page where it loops through page-custom-field.php page and display it on itself. Here is my code attempting that so far. I am using advanced custom fields on the second page.

My If statements

page-home.php

 <?php
        $args = array(
            'post_type' => array(
                'features'
            ),
            'posts_per_page' => 1,

          );
        $query = new WP_Query( $args );
      ?>

    <div id="homeSpecials" class="section__content small-12 small-centered medium-10 columns">
    <?php  if($query -> have_posts()) : while( $query->have_posts() ) : $query->the_post(); ?>
              <?php include 'page-custom-field.php' ?>
      <?php endwhile; endif;//ends'menu_flexible'?>

page-custom-field.php

   <?php if( have_rows('menu_item') ): ?>
        <?php while( have_rows('menu_item') ): the_row(); ?>
            <!-- loop through the rows of data || DONT CLOSE THE LOOP IN AN 'IF' STATEMENT -->
            <?php if( get_row_layout() == 'simple_menu_item' ): ?>
                <article class="menu-item small-12 medium-6 columns">
                    <h3><?php the_sub_field('item_name') ?><span class="menu-item__price"><?php the_sub_field('item_price') ?></span></h3>
                    <p>
                        <?php the_sub_field('item_description') ?>
                    </p>
                </article>
            <!-- elseif is the last loop -->
            <?php elseif( get_row_layout() == 'line_item_with_modifiers' ):  ?>
                <article class="menu-item small-12 columns">
                <h3><?php the_sub_field('item_name') ?><span class="menu-item__price"><?php the_sub_field('item_price') ?></span></h3>
                <p>
                    <?php the_sub_field('item_description') ?>

                </p>
                <?php if( have_rows('item_modifiers') ): ?>
                    <?php while( have_rows('item_modifiers') ): the_row(); ?>
                        <div class="small-12 medium-3 columns">
                            <h4><?php the_sub_field('modifier_title') ?></h4>
                            <ul>
                                <?php if( have_rows('modifier_column') ): ?>
                                    <?php while( have_rows('modifier_column') ): the_row(); ?>
                                        <li><?php the_sub_field('modifier_name') ?></li>
                                    <?php endwhile;?>
                                <?php endif;?>
                            </ul>

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


                </article>


            <!-- final 'endif' closes the flexible content loop -->
            <?php  endif; ?>
        <?php endwhile; endif;//ends'menu_flexible'?>
Share Improve this question asked Jan 8, 2019 at 22:11 Anders KitsonAnders Kitson 1571 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Figured it out just had to do this , the page-custom-field.php is included to shorten the code.

<?php  $query = new WP_Query( array( 'pagename' => 'features' ) ); ?>  
    <?php  while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php include page-custom-field.php' ?>

    <?php endwhile; ?>

<?php  wp_reset_postdata(); ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749030623a315771.html

最新回复(0)