Search code examples
wordpresspodscms

The correct way to display Pods 2.0 items using PHP code in a Pods template


I'm almost tearing my hair off! I believe you are supposed to be able to use PHP, javacript etc other than HTML in PODS 2.0 template right? If so why is it that I get my items to display in a pods template using magic tags but I cannot echo stuff out using PHP code?

I set up a Pods template to display my stuff using the code below:

<h5><a href="{@permalink}">{@post_title}</a></h5>
<div>
<?php 
$mypod = pods( 'post' );
echo $mypod->field( 'post_title' ); 
?>
</div>

It outputs the post_title in a heading (h5) as expected but nothing shows u in the div. What am I doing wrong. Let me mention that I am not using a Pod's page but I am using a the Pod's list widget to display this in my WP sidebar.


Solution

  • You aren't giving pods() the ID of the post you want to use. But your main issue is that you don't need to setup a new pods() object at all, you just need to use this:

    <?php echo $obj->display( 'post_title' ); ?>
    

    $obj references the current Pods object used, when in a template.

    Also, I'm using display here, because it will then run any filters that the_title() would normally run, when in Pods.