Civ Indus
Civ Indus

Reputation: 27

Conditionally turn change class and rel based on a custom field using a php template in wordpress

This is a plugin theme issue. My plugin works with this template page that I have made.(modified)

The orginal file product-image.php. My modified file product-image-360.php

I have added 3 custom field to the product post.

This bit of code is really clever for allowing you to turn off a feature for a globally assigned class i.e prettyPhoto. This allows me to Enable the prettyPhoto from the featured images and turn it off when I include a class field of 360 yes for post and product images.

    global $post,$woocommerce;
    $threesixty = get_post_meta($post->ID, '360', true);
    if ($threesixty == '') {
    wp_register_script( 'prettyPhoto', get_template_directory_uri() . '/includes/js/jquery.prettyPhoto.js', array( 'jquery' ) ); 
    }

However I now have two product-image.php templates. How do I combine them into a conditional statement that changes the rel and class attributes depending upon a custom field of 360?

Upvotes: 0

Views: 166

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50798

Well, if they don't have a '360' field, then we'll assume that get_post_meta($post->ID, '360', true); will return false.

<a 
  itemprop="image" 
  href="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" 
  class="<?php echo (TRUE == ($threesixty = get_post_meta($post->ID, '360', true))) ? 'Magic360' : 'somethingElse';?>" 
  rel="<?php echo (TRUE == ($threesixty = get_post_meta($post->ID, '360', true))) ? 'filename:'.$split_name.'-{col}.jpg; magnify:true; magnify-filename:'.get_post_meta($post->ID, 'magnified_image_filename', true).'-{col}.jpg' : 'somethingElse';?>"
>
  //img stuff here
</a>

Upvotes: 0

Related Questions