Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Center area post/pages (http://forum.bytesforall.com/forumdisplay.php?f=32)
-   -   Show custom field in kicker - only IF field value exists (http://forum.bytesforall.com/showthread.php?t=6702)

jaxon Apr 13, 2010 09:38 AM

Show custom field in kicker - only IF field value exists
 
Hi folks,

I'm using title images images for some page titles. To make these images appear, I'm using the technique detailed in this post: http://forum.bytesforall.com/showthread.php?t=1542 It put's the following code in the kicker page info item.

<a href="%permalink%"><img id="postheadingimg-%post-id%" class="postheadingimg" src="/path/to/images/%meta('postheadingimg')%" alt="%post-title%" /></a>

The basic concept is to add a custom field for header images. Then give that field a value corresponding to each page's header image. This works great, as long as a header image exists. If the custom field is not assigned to a page, I just get a "text output" of the page title.

So, the problem is that I need the custom field to ONLY display if it has a value. aka, if I assigned a header to the page. This would be easy peazy put it in a pie if I could pass custom fields to the loop, but.... Atahualpa doesn't support that. If it did, I could do something like this:

<? if (get_post_meta($post->ID, 'postheadingimg', true)) { ?>
<img src="<? bloginfo('url'); ?>/wp-content/page_titles/<? echo get_post_meta($post->ID, 'postheadingimg', true) ?>" alt="<? bloginfo('name'); ?>" />
<? } else { ?>
<?php bfa_post_headline('<div class="post-headline">','</div>'); ?>
<? } ?>

So.... The obvious solution seems to be using a if/else statement in the Page info items area of ATA. This is where I could use help. What would that code look like? Logically it would be...

Kicker:
If the header custom field has any value
show the value
Else
Show the current page title

Byline:
If the header has any value
show the current page title (this is for the search engines - so that there will always be H1 text but would then be move off screen with CSS)
Else
do nothing

I would then, just eliminate bfa_post_headline from the loop all together...

So, any ideas folks?
Thanks a ton for any suggestions!

jaxon Apr 15, 2010 04:33 PM

So, I've made a bit of progress with this. I am now able to test whether or not my headerimage custom field has a value. This is very good. Now, if there is not a value - the default bfa headline is displayed.

The next step is the ELSE. If there IS a value - the value needs to be appended onto the root address for my title/header images. I am trying to achieve this by creating another variable $image that gets the default value of the root url + the value from the custom field. (note: the custom field values are file names like this: aboutus.gif)

I don't actually know my way around php at all, so any pointers would be really helpful. My two sticking points are:
-Creating an else statement that displays an image with a url taken from $image (I can't seem to add an else statement at all :o )
-Passing the $postheading variable to the $image variable. (I don't get how to pass the variable into the url intact)

Any advice - even guesses - would be enthusiastically appreciated.

Here's what I have.

<?php
$headerimage = get_post_custom_values("postheadingimg");
$image = "http://mydomain.net/wp-content/page_titles/".postheadingimg."";

if($headerimage == '') : ?>

<?php bfa_post_headline('<div class="post-headline">','</div>'); ?>

<?php endif; ?>

juggledad Apr 15, 2010 06:10 PM

HTML Code:

<?php
$headerimage = get_post_custom_values("postheadingimg");
$image = "http://reneweb.net/wp-content/page_titles/".postheadingimg."";

if ($headerimage == '')  {
    bfa_post_headline('<div class="post-headline">','</div>');
    }
else { 
    ...else code goes here...
    }
?>


jaxon Apr 15, 2010 08:55 PM

Beautiful, perfect, excellent juggledad! You are so stupidly helpful! Thank you thank you! I really don't know php semantics at all - so that was exactly what I needed.

If you or anyone else can spare a few more minutes... I still need help with passing the variable and actually displaying the image.

First - is the last part of the following line the correct way to pull this variable? With the surrounding periods and quotes ."stuff." ?

$image = "http://mydomain/wp-content/page_titles/".postheadingimg."";

Second, for the ELSE statement... This was my guess...

<img src="<?php echo $image; ?>" alt="" />


but it returns:
Parse error: syntax error, unexpected '<' in /wp-content/themes/atahualpa/functions.php(476) : eval()'d code on line 21

Thanks a ton. Once this is working, I'll write a nice overview as a new post for anyone trying to do something similar. Thanks again!

juggledad Apr 16, 2010 05:34 AM

Quote:

First - is the last part of the following line the correct way to pull this variable? With the surrounding periods and quotes ."stuff." ?

$image = "http://mydomain/wp-content/page_titles/".postheadingimg."";
use single quotes and you have set $headerimage so use it instead, so you have
HTML Code:

$image = 'http://mydomain/wp-content/page_titles/' . $headerimage;
Quote:

Second, for the ELSE statement... This was my guess...

<img src="<?php echo $image; ?>" alt="" />

but it returns:
Parse error: syntax error, unexpected '<' in /wp-content/themes/atahualpa/functions.php(476) : eval()'d code on line 21
you have to be careful mixing HTML and PHP to escape things ar the right time. In this code
HTML Code:

if ($headerimage == '')  {
    bfa_post_headline('<div class="post-headline">','</div>');
    }
else { 
    ...else code goes here...
    }

the code that goes into the else is in PHP mode, so if you wanted to create HTML there you would need it to look like this
HTML Code:

if ($headerimage == '')  {
    bfa_post_headline('<div class="post-headline">','</div>');
    }
else { 
    ?> <img src=" <?php echo $image; ?>" alt="" /> <?php
    }


jaxon Apr 16, 2010 09:06 AM

You make this all seem so darn logical! Thank you juggledad!
The if else statement itself is perfectly functional now but there is some missing link for displaying the image. Currently the ELSE case just doesn't display anything. The code I'm using is below. It seems like the only two potential trouble areas are the way I'm calling the image url for $image or the ELSE statement itself. For reference, an actual image url for one of these images is: http://www.reneweb.net/wp-content/pa...es/contact.gif and the custom field gets the value contact.gif

This is the code I'm trying - based entirely on your excellent work:)



<?php
$headerimage = get_post_custom_values("postheadingimg");
$image = 'http://www.reneweb.net/wp-content/page_titles/'.$headerimage;

if ($headerimage == '') {
bfa_post_headline('<div class="post-headline">','</div>');
}
else {
?> <img src=" <?php echo $image; ?>" alt="" /> <?php
}
?>

Thanks for sticking with me on this.

juggledad Apr 16, 2010 01:36 PM

Try this
HTML Code:

<?php
$headerimage = get_post_custom_values("postheadingimg");
$image = 'http://www.reneweb.net/wp-content/page_titles/'.$headerimage;

if ($headerimage == '') {
bfa_post_headline('<div class="post-headline">','</div>');
}
else {
?>
<img src=" <?php $image ?>" alt="" /> <?php
}
?>

What you want is to output a line of HTML that has location of the image. The ECHO was
Just printing it's contents on the screen. With this, it should create the HTML using the contents of $image as part of the HTML statement

jaxon Apr 16, 2010 02:06 PM

So, the echo thing makes sense except that it wasn't printing anything to the screen before... With the new code, there is still nothing displayed where the image should be. I checked the source and this is the output for that space:

<img src=" " alt="" />

here's a page that should have an image if it's helpful at all: http://reneweb.net/projects/

Thank you so much for the quick replies:) Your help is really appreciated.

juggledad Apr 16, 2010 03:44 PM

try it like this
HTML Code:

<?php
$headerimage = get_post_custom_values("postheadingimg");
$image = 'http://www.reneweb.net/wp-content/page_titles/'.$headerimage;

if ($headerimage == '') {
bfa_post_headline('<div class="post-headline">','</div>');
}
else {
      echo '<img src="'.$image.'" alt="" />';
}
?>

after src= you have a double quote, single quote, a period, $image, a period, a single quote and a double quote

jaxon Apr 16, 2010 03:54 PM

No go. The source is showing this for the output... A lot better than "" though ;)

<img src="http://www.reneweb.net/wp-content/page_titles/Array" alt="" />

Thank you!

juggledad Apr 16, 2010 07:01 PM

well your problem is that get_post_custom_values() is returning an ARRAY not a single value, that's why the word ARRAY is showing up at the end of the line.

rbiasiutti Feb 6, 2013 10:42 AM

Hi,
i have the same request

1) I would like to insert a custom field named 'photo' in all posts and setting it with OK if a photo gallery is present and NO if not.

2) In the multipost page (created with the JUGGLEDAD's multicolumn/custom query template) the Kicker area must appear with an image if the custom field are setted to OK or blank (if disappear ... is better) if the field are setted to NO.

but :confused: i read ( and i read, and i re-read) this post but i don't understand where i must inserting the IF/ELSE code.

Do you explain me ?

Thank you

juggledad Feb 6, 2013 12:35 PM

1) this thread is from 2010
2) you can no longer add PHP in the theme options as this poster did back in 2010
3) You will have to edit the template and put your code there.

rbiasiutti Feb 6, 2013 01:11 PM

for this did not understand ... I was hoping it was still possible.

Quote:

Originally Posted by juggledad (Post 96193)
3) You will have to edit the template and put your code there.

ok for this but ... where?
You may indicate me, please, a most precise part of template to edit?

Thank you
Riccardo


All times are GMT -6. The time now is 08:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.