Viewing 11 reply threads
  • Author
    Posts
    • #2877
      cosmocanuck
      Member

      I’m having trouble displaying my add-more data (in my first attempt to use and display them), and in reading the support article on doing this:

      https://piklist.com/user-guide/docs/field-parameters/add_more/

      …I find I have more questions than answers. Here’s the key questions that have come up as I read it:

      Under “Basic add-more”, the first example of code to grab the data is:

      // Get field data
      $exercise = get_post_meta($post->ID, 'exercise', true);
       
      // Get your custom template part: addmore-workout-template.php
      piklist($path_to_template . '/addmore-workout-template', array('data' => $exercise, 'loop' => 'data'));
      

      With the first line commented as “get field data”.

      I’m comparing this to how I’ve previously captured (non-repeating) metafields, i.e.:

      // Capture all the metadata in $data
      $data = get_post_custom($post->ID);
      
      // Display a field
      echo $data [recipe_description][0];

      Doesn’t the first line here grab all my metafields for the post? Does it ignore add-more fields? Do I use the previous code example only for add-more metafields in particular?

      I also presume that the reference to $path_to_template is not an actual variable but just a placeholder for the methods described farther down – different code depending on whether we’re working in a plugin or a theme.

      The sample code for pulling advanced (i.e. nested) add-more metadata into a theme looks like this:

      // Get the post meta information
      $workout_schedule = get_post_meta($post->ID, 'workout_schedule', true);
       
      // 1. Display the data using the file workout_schedule_template.php.
      // 2. Assign your $workout_schedule parameter (from above) to 'data'.
      // 3. Loop through 'data'.
      piklist($path_to_template . '/workout_schedule_template', array('data' => $workout_schedule, 'loop' => 'data’));

      Here, the comment on the first code line is a little different even though its structure is the same. It says “Get the post meta information”, not “Get field data”. But as I understand it, this code is specifically targetting just specific Group field data, is that correct?

      Finally, I’m working in a child theme, so wouldn’t my “$path_to_template” code be:

      piklist(get_stylesheet_directory_uri() . '/addmore-workout-template', array('data' => $exercise, 'loop' => 'data’));

      not

      piklist(get_stylesheet_directory() . '/addmore-workout-template', array('data' => $exercise, 'loop' => 'data'));

      ?

      (The “_uri” is required, as I understand it, when referring to paths within child themes…)

      Possibly my questions simply relate to how this info is being presented but I am finding it tough going sorting this out. I’m continuing to experiment, but I do hope you can help/advise on these questions.

      Thanks!
      adam

    • #2878
      Steve
      Keymaster

      @cosmocanuck– Hope this helps:

      get_post_meta vs get_post_custom
      They are both standard WordPress functions. However, get_post_custom() returns a serialized array, which can be pretty ugly to deal with. You can take a look at how both are returned by doing something like this:

      // Grab the data two different ways
      $get_post_meta = get_post_meta($post->ID, 'my_field_key', false);
      $get_post_custom = get_post_custom($post->ID);
      
      // Display the data
      print_r($get_post_meta);
      print_r($get_post_custom['my_field_key']);
      

      get_stylesheet_directory_uri() vs get_stylesheet_directory()
      These are also standard WordPress functions. get_stylesheet_directory() returns the PATH to the theme directory, get_stylesheet_directory_uri() returns the URL.

      You can see the difference with this code:

      echo get_stylesheet_directory_uri();
      echo get_stylesheet_directory();
      
    • #2879
      cosmocanuck
      Member

      Thanks Steve.

      Was get_post_custom() previously the recommended way to pull in (non-repeating) fields? Because that’s how I did it on the previous site, the first one I used Piklist with; as per my earlier post, the code looks like this:

      $data = get_post_custom($post->ID);
      echo $data [recipe_description][0];

      I’m sure I got that method from somewhere in the Piklist support section, but now when I look at the page about metaboxes and fields:

      https://piklist.com/user-guide/tutorials/creating-meta-boxes-fields/

      I see that get_post_meta is indeed the method given for pulling in your field data.

      And I’d use it, but for the fact that I can’t for the life of me get it to work!

      I tried putting your sample code in my template, modified to refer to my Recipe field group seen in a prior post:

      // Grab the data two different ways
      $get_post_meta = get_post_meta($post->ID, 'ingredient_section', true);
      $get_post_custom = get_post_custom($post->ID);
      
      // Display the data
      print_r($get_post_meta);
      print_r($get_post_custom['ingredient_section']);

      When I do so, the first print_r returns absolutely nothing, but the second one (using the non-recommended method) at least spits out all the recipe ingredients! (Albeit in that awkward array format).

      I can’t figure out how I am screwing up the use of get_post_meta. Argh!

      You’ve been very patient and helpful so I hate to keep asking, but even having studied the Codex on this, I”m just getting nowhere. Hope you can advise.

      Thanks!

    • #2880
      Steve
      Keymaster

      Try setting get_post_meta to false:

      $get_post_meta = get_post_meta($post->ID, 'ingredient_section', false);
      

      Let me know if that fixes the issue.

    • #2881
      cosmocanuck
      Member
      This reply has been marked as private.
    • #2883
      Steve
      Keymaster

      Can you send login and ftp credentials?

    • #2884
      cosmocanuck
      Member
      This reply has been marked as private.
    • #2887
      Steve
      Keymaster

      It seems to be working now… but I really didn’t do anything. I just downloaded the files and uploaded them again.

      Let me know if you need more help.

    • #2890
      cosmocanuck
      Member

      Thanks so much Steve! That’s so bizarre. Maybe special/nonstandard characters had crept into the file or something…

      At least I know the code was NOT the problem, so that’s helpful. If it reoccurs, I’ll try your “solution”!

      Thanks again,
      adam

    • #2899
      cosmocanuck
      Member
      This reply has been marked as private.
    • #2905
      Steve
      Keymaster

      @cosmocanuckAlways work with WP_Debug on. This will help you when developing. As soon as I turned on WP_Debug, I saw the error which was in single-recipe.php you had a typo: $get_stylesheet_directory() should be get_stylesheet_directory().

    • #2908
      cosmocanuck
      Member

      Yikes! The notorious “missing comma syndrome”… bane of programmers everywhere. Thanks so much, and this should really help me with issues like this in the future.

      It must be one of the perils of piecemeal WP learning, but it never was evident how useful or important WP_Debug was! Though I know how important debugging tools are in principle, I’ve never used that function. That will now change.

      Thanks again.

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