Tagged: 

Viewing 2 reply threads
  • Author
    Posts
    • #3594
      liquidws
      Member

      Been working on this for about 6-7 hours… Got all piklist admin to work.. Hopefully this may help others on this forum, as i see there have been questions about front end 🙂

      
      piklist('field', array(
      	'type' 			=> 'group'
          // ,'field' 		=> 'slide1_group'
      	,'label' 		=> __('Slide #1', 'piklist-slider')
          ,'list' 		=> false
      	,'description' 	=> __('Slide #1 Title, Description and Link', 'piklist-slider-admin-description')
      	,'help' => __('Enter data and files to display on homepage slider')
      	,'fields' 		=> array(
      		///// Slide #1 Title /////
      		array(
      			'type' 		=> 'text'
      			,'field' 	=> 'slide1_title'
      			,'label' 	=> __('Slide #1 Title', 'piklist-slider-title')
      			,'columns' 	=> 6
      		)
      		///// Slide #1 Link to Existing Page /////
      		,array(
      			'type' 			=> 'select'
      			,'field' 		=> 'slide1_select_page_link'
      			,'label' 	=> __('Slide #1 Link to Page', 'piklist-slider-link-to-page')
      			,'columns' 		=> 6
      			,'choices' 		=>  piklist(
      				get_posts(
      					array(
      						'post_type'	=> 'page'
      						,'orderby'        => 'post_title'
      						,'order'          => 'DESC'
      					)
      					,'objects'
      				)
      				,array(
      					'ID'
      					,'post_title'
      				)
      			)
      		)
      		///// Slide #1 Description /////
      		,array(
      			'type' 		=> 'textarea'
      			,'field' 	=> 'slide1_description'
      			,'label' 	=> __('Slide #1 Description', 'piklist-slider-description')
      			,'attributes' => array(
      				'rows' 		=> 2
      				,'cols' 	=> 50
      			)
      		)
      		///// Slide #1 Image /////
      		,array(
      			'type' 		=> 'file'
      			,'field' 	=> 'slide1_image'
      			,'label' 	=> __('Slide #1 Image', 'piklist-slider-img')
      			,'columns' 	=> 12
      		)
      	)
      	,'on_post_status' => array(
      	    'value' => 'lock'
      	)
      ));

      I got this to work on another site i was working on last week, using same code and the advice i was given on this page.. https://piklist.com/support/topic/display-group-fields-on-frontend-template/

      Here is the front end code i am trying to use.. what am i ding wrong :$

      <?php $posts = get_posts(array(
      		'post_type'  	=> 'page',
      		'page_id' 		=> 2
      	));
      
      	  foreach($posts as $post) {
      	  $url = get_post_permalink($post->ID); ?>
      
      	  	<h2><?php the_title(); ?></h2>
      		<img class="img-responsive" src="http://placehold.it/1280x250/16a085/ffffff&text=Page"/>
      
      		<h3 style="color:#fff"><?php echo $post->slide1_title; ?></h3>
      		<img class="img-responsive" src="<?php echo $post->slide1_image; ?>"/>
      		<p><?php echo $post->slide1_description; ?></p>
      		<a href="<?php echo $post->slide1_select_page_link; ?>" target="_blank">website</a>
      
      <?php } ?>

      This displays the hard coded html but not the piklist fields.. so i have the specific id part working..

    • #3642
      liquidws
      Member

      Is there anyone that can help me with this? 🙂

    • #3645
      Steve
      Keymaster

      @liquidws– Sorry for the delayed response.

      IMPORTANT: Piklist does everything the WordPress way. You don’t have to rely on Piklist documentation to display post meta. Any WordPress tutorial on post meta would help you.

      1) You’re using get_posts to retrieve your post data which is fine, except this function does not retrieve post_meta, which you need. So $post->slide1_title does not exist.

      2) You can easily see what is being returned by a function by using the standard PHP function print_r, or the Piklist function piklist::pre(). The Piklist function is just a prettier version of print_r.
      Either one of these would show you what get_posts was returning:

      $posts = get_posts(array(
        'post_type' => 'page',
        'page_id' => 2
      ));
      
      print_r($posts);
      piklist::pre($posts);

      3) To retrieve post_meta, just use the standard WordPress function get_post_meta.

      4) When using the Piklist file field, the data that is saved is the Attachment ID, which WordPress automatically creates. Then you need to use this ID to retrieve the data you need. Since you can upload multiple files, Piklist saves all the IDs, and you need to loop through them to get the information you want. This tutorial explains it.

      Here is your new code based on what I wrote above. Please let me know if something does not make sense.

      foreach($posts as $post) {
      
        $url = get_post_permalink($post->ID); ?>
      
        <h2><?php the_title(); ?></h2>
        <img class="img-responsive" src="http://placehold.it/1280x250/16a085/ffffff&text=Page"/>
        <h3 style="color:#fff"><?php echo get_post_meta($post->ID, 'slide1_title', true); ?></h3>
      
          <?php // Get all the slide1_image IDs and then loop through them
          $slide1_image_ids = get_post_meta($post->ID, 'slide1_image', true); ?>
      
            <?php foreach ($slide1_image_ids as $image_id) : ?>
      
              <img class="img-responsive" src="<?php echo wp_get_attachment_url($image_id); ?>"/>
      
            <?php endforeach; ?>
      
            <?php echo get_post_meta( $post->ID, 'slide1_description', true ); ?>
      
            <?php // Get all the slide1_select_page_links IDs and then loop through them
            $slide1_select_page_links = get_post_meta($post->ID, 'slide1_select_page_link', false); ?>
      
            <?php foreach ($slide1_select_page_links as $page_link) : ?>
      
              <a href="<?php echo get_permalink($page_link); ?>" target="_blank">website</a>
      
            <?php endforeach;
      }
Viewing 2 reply threads
  • You must be logged in to reply to this topic.