Tagged: ,

Viewing 3 reply threads
  • Author
    Posts
    • #2131
      ianmuscat
      Member

      Hi,

      I am trying to display the contents of an advanced ‘add-more’ using a Piklist plugin. This add-more does not contain any further nested add-mores, just a few fields. I read the docs, and I think I do understand the concept of having a separate template file for each ‘add-more level’. However, I’m still running into issues and errors.

      The code that generates the metabox is the following.

      <?php
      /*
      Title: My Piklist Test
      Description: My Description
      Post Type: piklist_test
      Order: 1
      Lock: true
      Collapse: true
      */
      
      piklist('field', array(
         'type' => 'group'
        ,'field' => 'piklist_test_addmore'
        ,'add_more' => true
        ,'fields' => array(
      
      array(
         'type' => 'text'
        ,'field' => 'piklist_test_title'
        ,'label' => __('Title','piklist')
        ,'description' => __('Enter a title for this test.','piklist')
        ,'help' => __('Enter a title for this test.','piklist')
        ,'attributes' => array(
          'class' => 'large-text'
          ,'placeholder' => __('Enter title here','piklist')
        )
        ,'on_post_status' => array(
          'value' => 'lock'
        )
        ,'sanitize' => array(
        array(
          'type' => 'title'
          ,'context' => 'save'
        )
      )
      )
      ,array(
        'type' => 'editor'
        ,'scope' => 'post'
        ,'field' => 'piklist_test__content'
        ,'label' => __('Test Content','piklist')
        ,'description' => __('Enter content for this test.','piklist')
        ,'help' => __('Enter content for this test','piklist')
        ,'value' => '<h1>Headline.</h1><p>Some text here.</p>'
        ,'options' => array (
          'wpautop' => false
          ,'media_buttons' => true
          ,'tinymce' => true
          ,'quicktags' => true
        )
      )
      
      ,array(
        'type' => 'file'
        ,'field' => 'piklist_test_media'
        ,'scope' => 'post_meta'
        ,'label' => __('Add Media','piklist')
        ,'description' => __('Add media for this test.','piklist')
        ,'help' => __('Add media for this test','piklist')
        ,'options' => array(
          'modal_title' => __('Add Test Media','piklist')
          ,'button' => __('Add Media','piklist')
        )
        ,'validate' => array(
          array(
            'type' => 'limit'
            ,'options' => array(
              'min' => 1
              ,'max' => 1
            )
          )
        )
      )
      
      ,array(
        'type' => 'colorpicker'
        ,'scope' => 'post_meta'
        ,'field' => 'piklist_test_color'
        ,'label' => __('Test Color','piklist')
        ,'description' => __('Select a color for this test','piklist')
        ,'help' => __('Select a background color for this test','piklist')
        ,'attributes' => array(
          'class' => 'text'
        )
      )
      
      ,array(
        'type' => 'select'
        ,'field' => 'piklist_test_option'
        ,'value' => 'option-1' // Sets default to option-1
        ,'label' => __('Test Layout','piklist')
        ,'description' => __('Choose an option for this test','piklist')
        ,'attributes' => array(
          'multiple'
        )
        ,'choices' => array(
          'option-1' => __('Option 1','piklist')
          ,'option-2' => __('Option 2','piklist')
        )
      )
      
      )
      ));

      Next, I created a template file called test_template.php which I placed in the parts directory of my plugin which for now simply includes the following.

      <?php echo $data['piklist_test_title']; ?>

      I then opened up my theme’s folder, created a file called single-test.php and I placed the following content in the WP loop (just before the_content()).

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

      I’m not sure what I’m doing incorrectly, but I’m getting the following errors in the front-end (when viewing a single test).

      Warning: array_keys() expects parameter 1 to be array, string given in C:\xampp\htdocs\wp-content\plugins\piklist\includes\class-piklist.php on line 827

      Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\plugins\piklist\includes\class-piklist.php on line 256

      Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\plugins\piklist\includes\class-piklist.php on line 264

      Notice: Undefined index: slide-name in C:\xampp\htdocs\wp-content\plugins\piklist-test\parts\test_template.php on line 1

      Any ideas as to what I may be doing incorrectly?

      Many thanks,
      Ian

    • #2132
      Steve
      Keymaster

      @ianmuscat

      First, we need to get the data to save. You should remove scope => post. This tells Piklist to save the data to the wp_posts table. However, you are setting the field name to “piklist_test__content”, which doesn’t exist on the posts table. The best thing to do 99% of the time is to leave out the scope parameter and let Piklist figure it out automatically.

      As for the template part we need to tell Piklist where it is located. I suggest using piklist::paths to help with this.

      $piklist_test_addmore = get_post_meta($post->ID, 'piklist_test_addmore', true);
      		 
      // 1. Display the data using the file test_template.php.
      // 2. Assign your $piklist_test_addmore parameter (from above) to 'data'.
      // 3. Loop through 'data'.
      piklist(piklist::$paths['your-plugin-slug'] . '/parts/test_template', array('data' => $piklist_test_addmore, 'loop' => 'data'));
      

      Now it should work.

      I updated the documentation to be clearer.

      Let me know if this works for you.

    • #2133
      ianmuscat
      Member

      This worked – I can’t thank you enough! You may mark this issue as resolved.

    • #2134
      Steve
      Keymaster

      You’re very welcome!

Viewing 3 reply threads
  • The topic ‘Displaying Content from Advanced Add-Mores’ is closed to new replies.