Viewing 7 reply threads
  • Author
    Posts
    • #1226
      cosmocanuck
      Member

      Hi guys! OK, I’m stumped. I’m just trying to add one new field… it shows up on the backend just fine… it’s added to the database – I checked… but when I try to access/display it, I get nothing!

      The code I added the metabox with:

      piklist('field', array(
      'type' => 'text',
      'scope' => 'post_meta',
      'field' => 'rs_ID',
      'label' => __('Slider ID'),
      'position' => 'wrap',
      'attributes' => array(
      'class' => 'text',
      'columns' => 3
      )
      ));

      The code I’m (in this case) trying to just test my display of the “rs_ID” field:


      <?php echo 'ID = ' . $resortData [$rs_ID][0]; ?>

      But, like all other attempts to reference the field, I get nothing at all displayed. It just shows on the site as “ID =” with nothing after it.

      I’m alling the $resortData variable first, and all the other piklist metadata later on the page still displays fine.

      I’ve racked by brains and re-checked the code a dozen times. Is there something I’m not seeing?

      Thanks!
      Adam

    • #1227
      Marcus
      Member

      To get a piece of meta data from the backend try this:

      $datavar = get_post_meta(get_the_ID(), 'rs_ID', false);
      

      So for the example above, on the back end, I have a meta box on all pages.
      When I set the rs_ID field to something, I can retrieve it on the front end page, by using get_post_meta();
      By passing the post id ‘get_the_ID()’ (in this case the page I’m on) and the field I want ‘rs_ID’ and finally whether or not its an array (in this case false, just one piece of data)

      Now when I use:

      echo($datavar);
      

      I would see only the saved data inside rs_ID from my meta box.

      Make sense?

      Marcus

    • #1228
      Kevin
      Keymaster

      Marcus nailed it. When you save something in the scope of ‘post_meta’ it is saved on the meta table against the post in context and that meta needs to be pulled with a built in helper functions from either WordPress or Piklist.

      Thanks,

      Kevin

    • #1231
      cosmocanuck
      Member

      Thanks guys, I will try that approach. But I have to ask: why does the following kind of code, which I have been successfully using to display tons of Piklist metadata for posts, not work for that one variable but DOES work for the rest?

      
      <h3>Mountain Terrain</h3>
      <p><strong>Total Trails:</strong>
      <?php if ($resortData [total_trails][0]) {
      	echo $resortData [total_trails][0];
      } else {echo '0';} ?>
      </p>
      

      I have dozens of custom fields that I'm displaying with variations on the above. Before any of it, I have:

      
      <?php $resortData = get_post_custom($post->ID); ?>;
      

      This latest request for metadata display is constructed the same way, but is failing, and I don't see what's different:

      
      <?php $resortData = get_post_custom($post->ID); ?>
      <?php echo '<p>The slider ID for this listing is ' . $resortData [$royalSlider_ID][0] . ' and the average snowfall in inches is ' . $resortData [average_snowfall_in][0] .  '.</p>'; ?>
      

      The second variable shows the value I expect, the first one returns nothing.

      At any rate, I will try the above, clearly more robust method, for that particular bit of data – but I will leave the rest of my code alone, as it is working for me!

      Thanks so much,
      adam

    • #1233
      Kevin
      Keymaster

      Hi Adam-

      get_post_custom should pull it just fine actually…

      1. You can output an object to the screen for inspection by using ‘piklist::pre($object);’ , this would be handy to look at the meta fetched.
      2. Is this correct $resortData[$royalSlider_ID][0]? Or should it be $resortData[‘rs_ID’][0]

      Thanks,

      Kevin

    • #1234
      cosmocanuck
      Member

      It worked, but I had to refer to my variable in the array style, i.e. though I call it “$sliderNumber”, I had to reference it as “$sliderNumber[0]” in the echo statement:

      
      <?php $resortData = get_post_custom($post->ID); ?>
      <?php $sliderNumber = get_post_meta(get_the_ID(), 'rs_ID', false); ?>
      	    	                	
      <?php if ($sliderNumber[0] > '') {
      	echo get_new_royalslider($sliderNumber[0]);
      }
      ?>
      

      But it does work! And now my client can just enter in a single digit into one of my custom fields, instead of pasting in some arcane code to his post, to display the sliders he’ll be creating with the “Royal Slider” plugin. It works perfectly… hooray! And thanks to you both.

    • #1235
      Marcus
      Member

      Hey Adam, my bad, that should have read:
      get_post_meta(get_the_ID(), 'rs_ID', true);

      To get just a single var, false is for an array.

      Sorry about that.
      Too quick with the fingers, too fast for my brain.

      Marcus

    • #1237
      cosmocanuck
      Member

      ACK!!! I see what my original mistake was.

      I had a “$” before my reference to the Piklist variable, which was incorrect!

      It’s right there in my first post:

      
      <?php echo 'ID = ' . $resortData [$rs_ID][0]; ?>
      

      Should have been…

      
      <?php echo 'ID = ' . $resortData [rs_ID][0]; ?>
      

      I fixed that… and it worked fine.

      I should have known to take an even closer look when my “same method as the others” approach didn’t work.

      Somewhat vindicated that nobody else noticed it either though! 8^)

      But sorry to take up time with something that just needed more attention to my (broken) syntax.

      Thanks Markus and Kevin!
      Adam

Viewing 7 reply threads
  • You must be logged in to reply to this topic.