This is how you print Pods field in WordPress template files. When using pods there are some snippets that I use very often I thought I would make a collection of them so they are all in one place and open for anyone to use them. Here they are:
Print Pods field in WordPress template
In the following Example I am looking for event_date
field in a POD called named pods_cpt_event
. Since This code is in a template file that gets called for any content type i am doing a few if statements to be sure to only print it in the right place.
If you have a special template file set up for it the TEST1 and TEST2 is not necessary.
<?php
global $post;
$pod = pods($post->post_type);
?>
<!-- TEST 1: if it's POD generated custom content type-->
<?php if ((!empty($pod)) && is_pod($pod)): ?>
<!-- YES this is a POD -->
<!-- TEST 2: If it's the actual content type that we want -->
<?php if (($post->post_type == 'pods_cpt_event')): ?>
<?php
// initialize field as empty
$event_date = "";
// Try to get content of the field
$event_date = get_post_meta( get_the_ID(), 'event_date', true );
?>
<!-- 3. TEST if not empty and print in TPL file -->
<?php if (!empty($event_date)): ?>
<div>
<?php echo $event_date; ?>
</div>
<?php endif ?>
<?php else: ?>
<!-- END TEST 2: it's a POD but not the 'pods_cpt_event' content type -->
<?php endif ?>
<?php else: ?>
<!-- END TEST 1: not a POD, could be a POST or a PAGE -->
<!-- Continue as usual. -->
<?php endif ?>
Same implementation inside your theme’s functions.php
Following the same logic in the theme’s function.php would look like this.
<?php
function your_custom_function(){
global $post;
// LM: Initialize ALL POD fields as empty
$event_date = "";
// LM: Get the POD
$pod = pods($post->post_type);
// LM: Chek if POD
if ((!empty($pod)) && is_pod($pod)) {
// LM: Check if right pod
if ($post->post_type == 'pods_cpt_event') {
// LM: Get the field
$event_date = get_post_meta( get_the_ID(), 'event_date', true );
}
}
// LM: Print it!
if ( !empty($project_website_link_field) ){
echo $event_date;
}
}
?>
Hang tight. More POD snippets coming soon.
Check out all posts tagged with pods.
To find out more about pods check out their documentation page.
Cheers! Please Like ♥ the page if you found this useful!