Support

Account

Home Forums Front-end Issues Retrieving cell values from options table on specific page template

Solved

Retrieving cell values from options table on specific page template

  • Hello. I outsourced this task to a contractor over the past week as i’m up against it timewise. The contractor has been unable to deliver it, so i’m looking to try and get this solved myself asap with a little help!. PHP is not my skill, but after looking at some examples i’ve tried to produce something (very crudely). The problem is I need an editable table for the client to amend, from the Options page in WordPress. This is the main hub where the prices of the packages will be updated.

    A quick example – It’s a tour website, with individual pages of destinations/types of tour. A page is setup to list destinations, prices etc – what I need is then to be able to pull individual prices from the table cells to display on the relevant destination page. This then updates whenever it is edited by the client in the options table. Jungle Survival tour is £1600 > so when user is on jungle-survival page it displays that price. On the Jungle Combat page it outputs that price and so on. (it currently has no output as I could not get it to output anything, so I have placed in a hard code line in that div “Dynamic Trip price displayed here”. If there is no relevant price relating to that page then show nothing. The options page is a repeater, with all fields set as text input.

    I’m sure this is easily done, maybe i’m overcomplicating or having trouble articiculating (I’ve found this when I try to search Google for a solution!). I have supplied the dev url, and the existing code that is in place, hopefully this helps. I would massively appreciate any kind of help.

    Page where the table exists (displaying the Options table)
    Dates & Costs page

    Example page template where I need the relevant prices outputting (4×4 Venture in this example – all of the pages underneath ‘Our Trips’ in navigation use this template
    4×4 Venture page

    Code section

    dates-costs.php page code (Dates & Costs page)

    <section class="service wrapper">
    
      <div class="wrap">
        <div class="servicebox">
    
          <!-- 2016 price table -->
          <?php if(get_field('dates_costs_2016', 'options')) { ?>
          <h2>2016 Prices</h2>
          <table class="maintenance">
            <thead>
              <tr>
              <th>Trip</th>
                <th class="center">Dates</th>
                <th class="center">Season</th>
                <th class="center">Price</th>
                <th class="center">Availability</th>
              </tr>
            </thead>
            <?php while(has_sub_field('dates_costs_2016', 'options')) {
              $trip = get_sub_field('trip');
              $date = get_sub_field('date');
              $season = get_sub_field('season');
              $packageprice = get_sub_field('package_price');
              $availability = get_sub_field('availability'); ?>
              <tr>
                <td><?php echo $trip; ?></td>
                <td><?php echo $date; ?></td>
                <td><?php echo $season; ?></td>
                <td><?php echo $packageprice; ?></td>
                <td><?php echo $availability; ?></td>
              </tr>
              <?php } // end while ?>
            </table>
            <?php  } // end if ?>
    
            <!-- 2017 price table -->
            <?php if(get_field('dates_costs_2017', 'options')) { ?>
            <h2>2017 Prices</h2>
            <table class="maintenance">
              <thead>
                <tr>
                <th>Trip</th>
                  <th class="center">Dates</th>
                  <th class="center">Season</th>
                  <th class="center">Price</th>
                  <th class="center">Availability</th>
                </tr>
              </thead>
              <?php while(has_sub_field('dates_costs_2017', 'options')) {
                $trip = get_sub_field('trip');
                $date = get_sub_field('date');
                $season = get_sub_field('season');
                $packageprice = get_sub_field('package_price');
                $availability = get_sub_field('availability'); ?>
                <tr>
                  <td><?php echo $trip; ?></td>
                  <td><?php echo $date; ?></td>
                  <td><?php echo $season; ?></td>
                  <td><?php echo $packageprice; ?></td>
                  <td><?php echo $availability; ?></td>
                </tr>
                <?php } // end while ?>
              </table>
              <?php  } // end if ?>
    
          </div>
        </div>
    
      </section>

    Page Template where I need output – 4×4-venture

    get_header(); ?>
    
    <section class="main-wrapper wrapper">
      <div id="primary" class="content-area wrap">
        <main id="main" class="site-main no-sidebar" role="main">
        <?php get_template_part('inc/service-usps'); ?>
    
          <div class="dynamic-price">
            Dynamic Trip price displayed here
          </div>
    
          <?php while ( have_posts() ) : the_post();
    	  	get_template_part( 'content', 'page' );
    	  endwhile; // end of the loop. ?>
        </main>
        <!-- #main -->
        <?php /* ?>
        <div class="sidebar">
          <?php get_sidebar( 'internal' ); ?>
        </div>
        </php */ ?>
      </div>
      <!-- #primary -->
    </section>
    <?php get_footer(); ?>

    Thanks for any assistance.

  • Hi @cjg79

    I think it will be better if you set the ‘trip’ subfield as a post object field. That way, the options page and the trip page has a relation which you can use it later. After that, you can check the current price like this (it’s important to put it inside the loop):

    <?php while ( have_posts() ) : the_post(); ?>
        <div class="dynamic-price">
            <?php
            $costs = get_field('dates_costs_2016', 'options');
    
            foreach( $costs as $cost ) {
                if( $cost['trip']->ID == get_the_ID() ){
                    echo $cost['package_price'];
                }
            }
            ?>
        </div>
        <?php
        get_template_part( 'content', 'page' );
    endwhile; // end of the loop. ?>

    I hope this helps 🙂

  • James, thank you so much, that is exactly what I needed. Thanks a lot for your time and for pointing me in the right direction.

  • Hi James, if you are still around, it pulls out the data perfectly where I need it on the relevant page template, but I get this error on the options table page above.

    CATCHABLE FATAL ERROR: Object of class WP_Post could not be converted to string in /BUSHMASTERS/INC/DATES-COSTS.PHP on line 27

    I presume that’s before where I was echoing out a text/field string, I now need to echo out the ‘trip’ for the table but in the correct syntax but I don’t know how. Thanks, appreciate if you do get chance to help (again).

  • Hi @cjg79

    A post object has ‘post_title’ variable which you can get like this:

    $trip = get_sub_field('trip');
    echo $trip->post_title;

    This page should give you more idea about it: https://codex.wordpress.org/Class_Reference/WP_Post.

    To learn more about post object, please take a look at this page: https://www.advancedcustomfields.com/resources/post-object/.

    Hope this helps 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Retrieving cell values from options table on specific page template’ is closed to new replies.