quasel
8/18/2015 - 10:23 AM

Pods Looping Examples.

Pods Looping Examples.

<?php
    /**
     * Template Name: Pods List
     *
     * List all items of a Pod using Pods Pages
     */

    get_header(); ?>

<div id="main-content" class="main-content">

    <?php
        <div id="primary" class="content-area">
		    <div id="content" class="site-content" role="main">
		    <?php
			 	//Setup Pod object
			 	//Presuming permalink structure of example.com/pod-name/item-name
                 //See http://pods.io/code/pods/find
			 	
			 	//Set $params to get 5 items
			 	$params = array(
                    'limit' => 5,
                );
                //get current pod name
                $pod_name = pods_v( 0, 'url');
                //get pods object
                $pods = pods( $pod_name, $params );
                
                //check that total values (given limit) returned is greater than zero
                if ( $pods->total() > 0 ) {
                    //loop through items using pods::fetch
                    while ($pods->fetch() ) {
                        //Put title/ permalink into variables
                        $title = $pods->display('name');
                        $permalink = site_url( trailingslashit( $pod_name ) . $pods->field('permalink') );
                ?>
                        <article>
                        <header class="entry-header">
                        <h1 class="entry-title">
                        <a href="<?php echo esc_url( $permalink); ?>" rel="bookmark"><?php _e( $title , 'text-domain' ); ?></a>
                        </h1>
                        </header><!-- .entry-header -->
                        <div class="entry-content">
                        <!--@todo output some fields here.-->
                        <a href="<?php echo esc_url( $permalink); ?>" rel="bookmark" class="button primary"><?php _e( 'Read More', 'text-domain' ); ?></a>
                        </div><!-- .entry-content -->
                        </article><!-- #post -->
             	<?php
                    } //endwhile;
                } //endif;
                
               		// Output Pagination
               		//see http://pods.io/docs/code/pods/pagination
                	echo $pods->pagination( );
            	?>
        </div><!-- #content -->
    </div><!-- #primary -->
 </div><!-- #main-content -->

<?php
    get_sidebar();
    get_footer();
<?php
/**
 *
 * Template Name: Pods Single
 *
 * Use this as a page template for Pods Pages single item, to show a Pods template of the same name as the pod
 */
get_header();
?>

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">
	<?php
			//setup Pod object presuming permalink structure of example.com/pod-name/item-name
			//get current item name
			$slug = pods_v( 'last', 'url' );

			//get current pod name
			$pod_name = pods_( 0, 'url');

			//get pods object
			$pods = pods( $pod_name, $slug );

		?>
			<article>
				<?php
					//Output template of the same name as Pod, if such a template exists.
					$temp = $pods->template($pod_name);
					if ( isset($temp)  ) {
						echo $temp;
					}
				?>
			</article><!-- #post -->
		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php 
    	//get Pods object for current post
    	$pod = pods( 'pod_name', get_the_id() );
	//get the value for the relationship field
	$related = $pod->field( 'relationship_field' );
	//loop through related field, creating links to their own pages
	//only if there is anything to loop through
	if ( ! empty( $related ) ) {
		foreach ( $related as $rel ) { 
			//get id for related post and put in ID
			//for advanced content types use $id = $rel[ 'id' ];
			$id = $rel[ 'ID' ];
			//show the related post name as link
			echo '<a href="'.get_permalink($id).'">'.get_the_title( $id ).'</a>';
			//get the value for some_field in related post and echo it
			$someField = get_post_meta( $id, 'some_field', true );
			echo $someField;
		} //end of foreach
	} //endif ! empty ( $related )
?>
<?php
    //set up pods::find parameters to limit to 5 items
    $param = array(
        'limit' => 5,
    );
    //create pods object
    $pods = pods('pod_name', $params );

    //check that total values (given limit) returned is greater than zero
    if ( $pods->total() > 0 ) {
        //loop through items pods:fetch acts like the_post()
       while ($pods->fetch() ) {

           //get the raw value of a field
           //in this case it's an array of data for an image
           $picture = $pods->field('picture');
           //pass ID of image to a WordPress image function and output it
           echo wp_get_attachment_image( $picture['ID'] );

           //get the value of a field, ready to be outputted
           $text = $pods->display('field_name');
           _e( $text, 'text-domain' );

           //check if a specific field ($field) is set to a specific specific value ($value)
           $field = 'field_name';
           $value = 'desired_value';
           if ( $pods->is( $field, $value ) ) {
               //display field if true
               _e( $pods->display( $field )), 'text-domain' );
           }

           //check if a specific field ($field) has a specific value ($value) anywhere in it.
           $field = 'field_name';
           $value = 'desired_value';
           if ( $pods->has( $field, $value ) ) {
               //display field if true
               _e( $pods->display( $field )), 'text-domain' );
           }

           //get an entire row of data for current item
           $row = $pods->row();
           //check that it isn't empty before continuing
           if ( !empty( $row ) ) {
                //do something with
           }
           //Fetch the nth state (in this case third) value of the row
           $value = $pods->nth(3);
           
           //fetch odd numbered states
           //equivalent to nth( 'odd' );
           $odd = $pods->zebra();


       } //endwhile
    } //endif
?>