680 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Provided great contribution to our forums
  • Has been a beta tester for an Envato feature
+4 more
Gareth_Gillman says

Having issues displaying the contents from a metabox in a loop

Code:

<?php

$args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'menu_order',
    'hierarchical' => 1,
    'exclude' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
 );
 $pages = get_pages($args);
 foreach ($pages as $page_data) {
  $postid = get_the_ID();
  $content = apply_filters('the_content', $page_data->post_content);
  $title = get_post_meta($postid, '_cmb_feat_title', true);
  $slug = get_post_meta($postid, '_cmb_feat_subt', true);
  ?>

   <a id="<?php echo $slug; ?>"></a>
    <div class="row-fluid PageHead">
     <div class="span12">
      <h1><?php echo $title; ?></h1>
      <h3><?php echo $slug; ?></h3>
     </div>
    </div>

    <?php echo $content; ?>

  <?php
 }

I think it’s to do with the postID but can’t figure it out, any help is appreciated

1485 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

Why not simply:

$postid = $page_data->ID;

By calling get_the_ID(), you are retrieving an id of the post/page in the main query (you will get back id of the post/page you are running this query on).

680 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Provided great contribution to our forums
  • Has been a beta tester for an Envato feature
+4 more
Gareth_Gillman says

That worked, thanks

I wasn’t aware of the page_data function, I had tried everything from get_the_ID to $wp_query->get_queried_object_id()

1485 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

page_data is not a function. Let me explain.

You have this:
 $pages = get_pages($args);

Your $pages variable now contains an array of objects, and each object represents one page that matched your request.

Then, you are doing foreach:
foreach ($pages as $page_data) {

which iterates over every member of your $pages array, and stores each member (page object) into a variable you named $page_data.

$page_data variable now contains all the data you pulled from the database in form of an object, and you access each property of the object using an “arrow” ->, unlike in array where you would use [’’]

So when you call
$page_data->ID
, you are accessing object property named “ID”, which actually holds ID of that page object.

I hope I made it clearer.

680 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Provided great contribution to our forums
  • Has been a beta tester for an Envato feature
+4 more
Gareth_Gillman says

ah right I get ya …. I was basically there but calling the wrong variable

by
by
by
by
by
by