WordPress.org

Ready to get started?Download WordPress

Forums

Pods - Custom Content Types and Fields
[resolved] Template conditional stopped working on last update (11 posts)

  1. maiki
    Member
    Posted 2 months ago #

    I have a template that shows a book CPT (how generic is that?!). We generate a list of buy links based on ISBNs, both print and digital. We have two sections that have conditional statements to list blocks based on if the field has a non 0 value (since they are Plain Number fields, and default to 0), for both ISBNs.

    Here is the first one:

    <?php if ($obj->field('isbn_print') != '0') { ?>
        ISBN: {@isbn_print}<br />
    <?php } ?>
    <?php if ($obj->field('isbn_digital') != '0') { ?>
        ebook ISBN: {@isbn_digital}<br />
    <?php } ?>

    That works fine. Then we have another section to list some links to various URLs:

    <?php if ($obj->field('isbn_print') != '0') { ?>
     <p>Order this book in print:</p>
     <ul>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=az">Amazon</a></li>
     <li><a href="http://search.barnesandnoble.com/books/product.aspx?r=1&EAN={@isbn_print}">Barnes and Noble</a></li>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=bamm">Books-A-Million</a></li>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=buycom">Rakuten.com Shopping</a></li>
     <li><a href="http://www.indiebound.org/product?isbn={@isbn_print}">IndieBound</a></li>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=ch">Indigo (Canada)</a></li>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=pw">Powell's Books</a></li>
     <li><a href="http://www.perseusbookspromos.com/processors/retail.php?isbn13={@isbn_print}&r=wal">Walmart</a></li>
     </ul>
    <?php } ?>
    
    <?php if ($obj->field('isbn_digital') != '0') { ?>
     <p>Order this book in digital format:</p>
     <ul>
     <li><a href="http://www.amazon.com/s/field-keywords={@isbn_digital}">Amazon (Kindle)</a></li>
     <li><a href="http://itunes.apple.com/us/book/isbn{@isbn_digital}">Apple iTunes</a></li>
     <li><a href="http://www.kobobooks.com/search/search.html?q={@isbn_digital}">Kobo</a></li>
     <li><a href="http://search.barnesandnoble.com/books/product.aspx?r=1&EAN={@isbn_digital}">Barnes & Noble (Nook)</a></li>
     </ul>
    <?php } ?>

    Those were working, but someone noticed that books without digital ISBNs were showing links. Then I verified by deleting the print ISBN that those links are still showing, too, which they shouldn't.

    Poking around the forums I found issue #1991, but from that thread I don't know how those conditional tags work, or if they are only part of the Frontier plugin.

    Any ideas on what that suddenly stopped working, or a better way to template that? ^_^

    https://wordpress.org/plugins/pods/

  2. maiki
    Member
    Posted 2 months ago #

    Huh, I just removed the first set of conditional tags, and the URL lists work correctly. What's up with that?

  3. Josh Pollock
    Member
    Plugin Author

    Posted 2 months ago #

    Maiki-

    Can you show me your complete code with both sets of conditionals together? I want to make sure this is a minor error on your part that I can correct for you. Based on what I've seen this should work, and that's a problem.

    Take care,
    Josh

  4. maiki
    Member
    Posted 2 months ago #

    Thanks for looking at it for me. Pasted at https://bin.jpope.org/siyutelubu.xml.

  5. Josh Pollock
    Member
    Plugin Author

    Posted 2 months ago #

    Maiki-

    Looks good. Here's the thing, we don't know exactly what the field value is when there is no value set. Can you add var_dump($obj->field('isbn_print')); and var_dump($obj->field('isbn_digital)); to the template and see what that produces on items that you know do not have values in those fields?

    That way you know exactly what a negative response looks like, and you can create your conditional, using !== instead of != to test against it.

    Take care,
    Josh

  6. maiki
    Member
    Posted 2 months ago #

    The var_dump example for http://moon.com/books/moon-new-england-biking/ is: string(13) "9781598800265" string(1) "0"

    ...

    Okay, it occurred to me to put that in after the first set is loaded, and it then returns: array(1) { [0]=> string(13) "9781598800265" } array(1) { [0]=> string(1) "0" }

    Does that mean there isn't a false or null being returned?

  7. Scott Kingsley Clark
    Member
    Plugin Author

    Posted 2 months ago #

    Relationships return false if there is no value set. Number fields will always return a zero if no value is set.

  8. maiki
    Member
    Posted 2 months ago #

    I updated the conditionals to show if they don't equal 0 (https://bin.jpope.org/rayixibubu.xml), but the original problem persists, subsequent conditionals don't work. If I remove the first one, the second conditional works.

  9. Josh Pollock
    Member
    Plugin Author

    Posted 2 months ago #

    I would try !== '0' and != false. Think the second one is more likely to work, but it would be better if it was the first, as that is more precise

    This is a good reference for !=, !==, == and === operators in PHP:
    http://stackoverflow.com/questions/589549/php-vs-operator

  10. maiki
    Member
    Posted 2 months ago #

    I wanted to reproduce this issue with a more simpler example. I made a CPT with two fields, text_field and number_field. The Pods export is at https://bin.jpope.org/pecinobocu.tex (and the functions snippet I use to show the fields is at https://bin.jpope.org/pubicasiya.coffee). And for easy reference, the Pods template is at https://bin.jpope.org/suxemosama.xml.

    I made a series of posts with the possible combinations, named appropriately:

    Okay, as I would expect, on the two posts that do not have a number, neither of the conditionals should show. The first one doesn't, but the second one does. If I remove the first one from the template, then the second conditional works as expected.

    I hope this is simple enough that you can test it locally. I appreciate your suggestions and am not looking for free work, but this worked prior to the last update, and now I can't get it to work. I don't know enough PHP to determine if this is a bug or if I am doing something incorrectly.

  11. Josh Pollock
    Member
    Plugin Author

    Posted 2 months ago #

    We resolved this in #pods by testing the value of the first key in the array returned by the number field, using: $number = $obj->field('number_field'); if ( $number[0] != '0' ) {

    And we opened this issue:
    https://github.com/pods-framework/pods/issues/2166

Reply

You must log in to post.

About this Plugin

About this Topic

Tags