Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently using Advanced Custom Fields to create a custom field has_the_portrait_sold which will be indicated on my Prettyphoto / Lightbox item.

However, when I use PHP from the example (it is true or false):

if(get_field('has_the_portrait_sold')){
echo "The item has been sold.";
}else{
echo "The item has not been sold";
}

In header.php:

<script type text/javascript>
jQuery(document).ready(function($) {
$("a[href$='.jpg'], a[href$='.jpeg'], 
a[href$='.gif'], a[href$='.png']").prettyPhoto({
theme: 'dark_square',
show_title: true, /* true/false */
social_tools: '<?php the_title(); ?> <?php
if(get_field('has_the_portrait_sold')){
echo "do something";
}else{
echo "do something else";
}
?>'
});
});
</script>

However, while this may include the custom field in the lightbox as needed, it apparently only displays custom field data for the current page. I am needing custom field data to print for the current lightbox item / WordPress' image attachment.

I therefore tried including the custom field call in post-template.php. Please note my PHP skills are limited, I am VERY determined to make this work.

return apply_filters( 'wp_get_attachment_link', "<a title='$post_title'
href='$url'>$link_text</a><?php
if(get_field('has_the_portrait_sold')){
echo "do something";
}else{
echo "do something else";
}
?>", $id, $size, $permalink, $icon, $text );
}

I feel like I am on the right path in including the custom field call in post-template.php . However, I just do not have the PHP skills necessary to create the correct call. I'm wondering if someone here can help. I am very desperate, but not willing to give up. Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

First things first - YOU ARE NOT ON THE RIGHT PATH IF YOU ARE MODIFYING CORE FILES. If it's not in wp-content, DO NOT MODIFY IT, especially if you can honestly say "I just do not have the PHP skills necessary to XXXX". STAY OUT OF CORE FILES, PERIOD. I'm not being mean, I'm saving you the hassle of losing your changes with each WordPress update, plus the ire of WordPress developers as a whole.

You're going to have a problem the way you're going about it. From the sound of it, you're trying to call get_field() in the header outside the loop, but somehow wanting it to know where in the loop it is.

Some explanation that might help: custom fields are attached to specific pages/posts (as you already know). This means that get_field() will return values according to the current object in $post - in the header, that's the current page. Not sure what the object in $post is at any given point? Use this code chunk where you want to know:

<pre>
<?php print_r($post); ?>
</pre>

You can also swap $post with $post->ID to see which post it's working on (especially useful for seeing what posts are pulling up in a loop).

So, you'll need to register whether or not a piece is sold by checking in the loop, then perhaps attach a CSS class to the output in the loop, and use CSS to show/hide that bit.

If you're trying to do it by hooking into a function (for attachment override, for example), then put that hook into functions.php. You can modify or override core functions in functions.php with filters and hooks - check the Codex for more info.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.