• Resolved czery

    (@czery)


    Hi there, absolutely love the Pods plugin and it took a lot of work from my table. Thanks for that!

    At the moment I am stuck with finding if and how I could use multiple conditions in a conditional tag in a Pods template.

    Basically I want to show a div only if at least one of several custom fields has a value. Something like:

    [if custom_field_1 OR custom_field_2]
       <div>
          <h1>Block Headline</h1>
          {@custom_field_1}
          {@custom_field_2}
       </div>
    [/if]
    

    Is there a way to achieve this?

    • This topic was modified 11 months, 3 weeks ago by czery.
    • This topic was modified 11 months, 3 weeks ago by czery.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @czery

    As far as I know we currently do not support multiple conditions in a single statement. You could try to nest these statements ??

    Cheers, Jory

    Plugin Support Paul Clark

    (@pdclark)

    • Shortcodes, like HTML, are a markup language, not a programming language.
    • This means they do not support all logical operators, as shortcodes are strings processed with regular expressions, not a tokenizer.
    • It’s a pedantic difference, but in a Pods context, this means no: if you need behaviors of a programming language, use a programming language. The easiest API for this in WordPress is defining a custom shortcode, which is one function, has existed since WordPress 1.2, and is supported by all templating engines and WP page builders.
    • As far as I know, Jory’s suggestion will not work both because Pods does not process shortcodes recursively (they cannot be nested), and because an if within an if would constitute a logical AND, not a logical OR.

    The easiest solution, defining a custom shortcode for use in your template, would look like this:

    <?php
    /**
     * @see https://codex.www.ads-software.com/Shortcode_API
     * @see https://en.wikipedia.org/wiki/Ward_Cunningham
     */
    add_shortcode(
    	'cunninghams-law',
    	function( $atts, $content, $tag ) {
    
    		if (
    			empty( get_post_meta( get_the_ID(), 'custom_field_1' ) )
    			|| empty( get_post_meta( get_the_ID(), 'custom_field_2' ) )
    		) {
    			return '';
    		}
    
    		$pod = pods(); // Defaults to current post type and post ID.
    
    		ob_start();
    
    		?>
    		<div>
    			<h1>Headline</h1>
    			<?php $pod->display( 'custom_field_1' ); ?>
    			<?php $pod->display( 'custom_field_2' ); ?>
    		</div>
    		<?php
    
    		return ob_get_clean();
    	}
    );

    It is worth noting that this approach has used both Pods and the WordPress core functions for accessing field values. Some general notes:

    • The above example would output the desired HTML or nothing (a blank string) for the shortcode [cunninghams-law]
    • A shortcode callback (the function) must always RETURN output, not print or echo. Both return and an output buffer (ob_...) have been used to illustrate various ways to do this.
    • Pods fields will usually be stored in post meta. For illustration purposes, get_post_meta() has been used, while output is still ->display(). This is because your example used magic tags, which would use Pods default output formatting, which would be the same as ->display() in PHP. The way to get a raw field value with Pods is ->field(). The Pods functions will run convenience connectors for various field types, while WordPress core functions may vary between functions related to post meta, taxonomy term meta, taxonomy term objects, user objects, and user meta etc.
    • || means “OR”.
    • While it is technically possible to jump from PHP back into a Pods template syntax for use of magic tags, it really does not make sense to do so. If one understands enough code to know how to do that, one also then generally understands that it’s better to output templates from PHP, especially if one has already jumped into a PHP context. So it’s possible, but generally not advisable.
    • In the above example, I have output the template by capturing output into a buffer and escaping from PHP into HTML several times. https://www.php.net/manual/en/function.sprintf.php without output buffering or https://www.php.net/manual/en/function.printf.php with output buffering is generally a better way.
    Plugin Author Jory Hogeveen

    (@keraweb)

    As far as I know, Jory’s suggestion will not work both because Pods does not process shortcodes recursively (they cannot be nested), and because an if within an if would constitute a logical AND, not a logical OR.

    You are absolutely right about it being and AND statement, and not OR @pdclark , good one!

    Also in general, while Paul’s solution is definitely much better and compatible with other systems. It does require more technical knowledge of PHP.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional Tag: Multiple Conditions’ is closed to new replies.