• Resolved usedbooksblog

    (@usedbooksblog)


    Okay, I’m not a php guru by any stretch of the imagination. However, I’ve hacked out a few things but can’t get this last bit to work. Here’s the background:

    I’m a book blogger, so I enter a custom ISBN field (like a SKU) for each of my book review posts. At the bottom of each post I’ve used the custom ISBN field and the echo get_post_meta to create dynamic URLs to retailers for that specific ISBN. This part works like a charm.

    But I want posts that don’t have an ISBN (non-book review posts) to NOT have this line of retailer links.

    My logic – if $ISBN is null then do nothing, else use the retailer links.

    So here’s what I have:

    <?php $ISBN=get_post_meta($post->ID, ‘ISBN’, $single = true);
    if ($ISBN = null) {
    echo “”;
    } else {echo “[retailer links]”;
    }
    ?>

    Here is the retailer link code which works perfectly fine on it’s own.

    Buy at: <a href="https://www.dpbolvw.net/click-2492922-7134924?url=https://www.abebooks.com/servlet/SearchResults?isbn=<?php echo get_post_meta($post->ID, 'ISBN', $single = true); ?>">Abebooks</a>, <a href="https://click.linksynergy.com/fs-bin/stat?id=mpEZ5C07uXI&offerid=99238&type=3&subid=0&tmpid=939&RD_PARM1=https://www.alibris.com/search/search.cfm?qisbn=<?php echo get_post_meta($post->ID, 'ISBN', $single = true); ?>">Alibris</a>, <a href="https://www.amazon.com/exec/obidos/redirect?tag=usedbooksblog-20&path=tg/stores/offering/list/-/<?php echo get_post_meta($post->ID, 'ISBN', $single = true); ?>/used">Amazon</a>, <a href="https://search.barnesandnoble.com/used/productMatches.asp?PRC=&x=29&PEAN=<?php echo get_post_meta($post->ID, 'ISBN', $single = true); ?>&lkid=J16280994&pubid=K128472&byo=1">Barnes & Noble</a>, <a href="https://www.jdoqocy.com/click-2492922-10470209?url=https://www.biblio.com/isbn/<?php echo get_post_meta($post->ID, 'ISBN', $single = true); ?>.html">Biblio</a>

    I figured I could just move this into the ” ” of the else and be done with it. But that returns a white page of nothing.

    Why won’t the retailer links code work in my else statement? Help!

    See code here: https://usedbooksblog.com/blog/the-partly-cloudy-patriot-by-sarah-vowell/

Viewing 10 replies - 1 through 10 (of 10 total)
  • I believe you could test if $ISBN got returned as an array:
    if ( is_array($ISBN) )

    You might consider getting something like WinGrep and searching the wordpress code for get_post_meta to see how developers do it…

    Thread Starter usedbooksblog

    (@usedbooksblog)

    Yikes – I hope I don’t need to do that.

    I’ve tested the $ISBN in a more mundane code:

    <?php $ISBN=get_post_meta($post->ID, 'ISBN', $single = true);
    if ($ISBN = null) {
    echo "Nothing";
    } else {echo "Something";
    }
    ?>

    That works, I don’t think it has anything to do with the $ISBN but instead something in the retail links code that isn’t friendly with the else {echo ” “;

    Oops sorry, thought you were saying the if ($ISBN = null) wasn’t working.

    Maybe post all your code at https://wordpress.pastebin.ca/ and report that link back here.

    Thread Starter usedbooksblog

    (@usedbooksblog)

    Okay – here’s the pastebin link on how I thought it should work. Again, all this does is return the white page of death.

    https://wordpress.pastebin.ca/616304

    Here’s what I use:

    if(get_post_custom_values('author')) :
    foreach(get_post_custom_values('author') as $author) {} ?>
    				<li>Author: <?php echo $author; ?></li>
    <?php endif; ?>

    If the ISBN is in a custom field, I think get_post_custom_values is more your friend than get_post_meta. With the above code, if I leave the Author custom field blank, it just doesn’t display anything. That’s what you want, right?

    Thread Starter usedbooksblog

    (@usedbooksblog)

    Humm, let me give that a try. ISBN is the custom field.

    And yes, if I’ve left the custom field blank I don’t want it to execute my retail link code string.

    So for my purposes:

    if(get_post_custom_calues('ISBN')):
    foreach(get_post_custom_values('ISBN') as $ISBN) {} ?>
    <?php echo [Retail Link code]; ?>
    <?php endif; ?>

    I think my only concern is that the echo in my Retail link code contains other php calls, maybe I can try using the get post custom values there and see if that makes a difference. I’ll play and come back and let you know. Thanks!!

    if(get_post_custom_calues('ISBN')):
    foreach(get_post_custom_values('ISBN') as $ISBN) {} ?>
    <a href="https://www.dpbolvw.net/click-2492922-7134924?url=https://www.abebooks.com/servlet/SearchResults?isbn=<?php echo $ISBN; ?>">Abebooks</a>
    ... and so on ...
    <?php endif; ?>

    The only PHP you have in your list of retail links is for WP to display the appropriate ISBN. Shouldn’t be a problem. I’m not sure if it would be ISBN or isbn but a quick test should answer that.

    Thread Starter usedbooksblog

    (@usedbooksblog)

    Well, actually the ISBN isn’t displayed as such, it’s the dynamic part of an href. Essentially, you click on say, Amazon, and it will send you not to the home page, but to the exact page of that book/ISBN. So what ultimately shows is:

    Buy at: Abebooks, Alibris, Amazon, Barnes & Noble, Biblio

    I’ll have some time tonight to test this out and will return with results – hopefully success!!! Thanks so much for your help.

    Yeah, I saw that. That’s how I suggested it in the code above, too. You’re not going to put <?php echo $ISBN; ?> anywhere but at the end of your URLs.

    Thread Starter usedbooksblog

    (@usedbooksblog)

    Yeee-haw. Worked like a charm!! Thank you SO much, please let me know if I can do anything to help you in the future.

    Finally got around to trying this out and what was taking me hours wound up taking about 10 minutes. Many thanks again.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘echo get_post_meta breaking else { echo ” “;}’ is closed to new replies.