• lownice

    (@lownice)


    I would like to add links to a featured image on some of my single posts. These links need to be unique in a way that it adds my Amazon Affiliate ID.

    I made a custom field called “az-post” which I populate when making the post (or mass importing via CSV). To test things out I added the following code to my post-head.php.

    <div class=”single-post-thumb”>
    <?php if(get_field(‘az-post’)): ?>
    <a href=”http://www.amazon.com/dp/<?php the_field(‘az-post’); ?>?tag=myamazonid-20″><?php tie_thumb( $size ); ?></a>
    <?php endif; ?>
    </div>

    With my affiliate ID entered in the “az-post” custom field…everything works as intended.

    The problem is when I have a post with no affiliate id in “az-post”. The featured image does not display in the single post.

    How would I get the following logic:

    If I make a post that uses my affiliate id via custom fields, then it should append a URL to the featured image with my amazon affiliate id. (like the example code above)

    IF a post does NOT have my affiliate ID entered THEN use the original theme code below:

    <div class=”single-post-thumb”>
    <?php tie_thumb( $size ); ?>
    </div>

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter lownice

    (@lownice)

    After a bit of messing around I believe I have gotten this to work. I am sure it is messy and I am not sure if it is interfering with anything else.

    <div class="single-post-thumb">
    	<?php if (get_post_meta($post->ID, 'az-post', true) ) { ?>
    					<a href="http://www.amazon.com/dp/<?php echo get_post_meta($post->ID, "az-post", $single = true); ?>?tag=myamazonid-20" target="_blank" rel="noindex, nofollow" ><?php tie_thumb( $size ); ?></a>
    					<?php } else { ?>
    	                <?php tie_thumb( $size ); ?>
    					<?php } ?>
    </div>
    • If there is an amazon id, then go ahead and append a URL + Amazon ID from the az-post custom filed.

      If there is no amazon id, then go ahead and include the featured image with no link.

    • Any thoughts?

Viewing 1 replies (of 1 total)
  • The topic ‘Adding URL Conditions to Featured Image via Custom Fields’ is closed to new replies.