Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save uschmelzer/c0f65a4cab401b66aeb531ef6e033360 to your computer and use it in GitHub Desktop.
Save uschmelzer/c0f65a4cab401b66aeb531ef6e033360 to your computer and use it in GitHub Desktop.
Pods Looping Examples.
<?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
?>
<?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
/**
* 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment