Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active April 14, 2023 12:23
  • Star 51 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Shelob9/7832561 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();
@keusta
Copy link

keusta commented Apr 4, 2020

hello, first exemple, error line 3 $param but line 7 its $params :)

@westerdaled
Copy link

westerdaled commented Apr 25, 2020

In terms of getting started, are these snippets to be added to separate templates and then change so I am fairly new to Pods and most examples I have seen are simple magic tags delimited with a paragraph tag - these look way more interesting😀.

Ah I get it, these are destined for page templates ( not Pods Templates).

@akkolad
Copy link

akkolad commented Apr 29, 2021

hello, first exemple, error line 3 $param but line 7 its $params :)

I agree !
But I like this Gist !

@GLWalker
Copy link

GLWalker commented Oct 3, 2022

Great Examples !

@Shashi99rocky
Copy link

Nice Example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment