PHP/WP custom field - php

I have a custom field in WP that isn't always required and can therefore be left blank. However, the custom field has a word preceding it in the HTML that I'd like to not show if the custom field is left blank.
I think there should be some sort of if/else statement, but am unsure of the exact syntax to get it working.
Code:
<li><span>DATE:</span>
<?php $meta_value = get_post_meta($post->ID, 'date', true);
if (!empty($meta_value)) {
echo '<li>'. $meta_value .'</li>';
} ?>
</li>
Much appreciated

You could put in your if statement what the pre-pended value would be. Let's say it was a $ and a space character.
if (!empty($meta_value) || $meta_value != '$ ') {

You just need to move your if statement out wider a bit:
$meta_value = get_post_meta($post->ID, 'date', true);
if (!empty($meta_value)) {
echo '<li><span>DATE:</span>'
echo '<li>'. $meta_value .'</li>';
echo '</li>';
}
Though, your HTML looks a little astray (you have a list item inside a list item)

Related

Variable with an escaped value as an if statement

I have the following if statement:
<?php
$title = esc_html( get_the_title() );
$sub_title = esc_html( get_field('sub_title') );
if ( get_field('sub_title') ) {
echo '<h1>'.$title.'</h1>';
echo '<h2>'.$sub_title.'</h2>';
}
else {
echo '<h1 class="nm">'.$title.'</h1>';
}
?>
Instead of this line:
<?php
if ( get_field('sub_title') ) {
echo '<h1>'.$title.'</h1>';
echo '<h2>'.$sub_title.'</h2>';
}
?>
I would like to use:
<?php
if ( $sub_title ) {
echo '<h1>'.$title.'</h1>';
echo '<h2>'.$sub_title.'</h2>';
}
?>
As the $sub_title variable has an escaped value, is it okay to use within an if statement?
As a manner of general principle, try to avoid using escaped data except at the point where you actually need the escapes. For instance, don't add HTML escaping until you get to the code that actually adds it to HTML. So I would write it as:
<?php
$title = esc_html( get_the_title() );
$sub_title = get_field('sub_title');
if ( $sub_title ) {
echo '<h1>'.$title.'</h1>';
echo '<h2>'.esc_html($sub_title).'</h2>';
}
else {
echo '<h1 class="nm">'.$title.'</h1>';
}
?>
In some cases, as in your code, it may not matter. Your if statement just cares whether the string is empty or not, and escaping an empty string doesn't add anything to it, so the result of the if will be the same.
But there are more complex cases where you might compare the string to another string, and depending on what you're comparing with it could change the result.
Similar advice applies when storing data in databases. Store the raw data, don't escape it then. Escape it after you retrieve it, when you're showing it on an HTML page. That way, when you perform queries that don't involve displaying the result in HTML (e.g. creating a CSV file, displaying in plain text, etc.), you don't have to contend with the escapes that might have been added.

Pass PHP Variable into a shortcode

I'm trying to do something that seems really simple but I can't figure it out.
In my category template, I have this shortcode:
<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>
I also have this to show the title of the category:
<?php single_cat_title(); ?>
I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:
<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>
but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?
Many thanks!!
If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.
What this means, is that just using single_cat_title(); will print Category Title to the document.
If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:
$category = single_cat_title( '', false );
This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):
echo do_shortcode( '[jobs categories="' . $category . '"]' );
You can make it a bit more succinct as well:
<?php
$category = single_cat_title( '', false );
echo do_shortcode( "[jobs categories='$category']" );
?>

Retrieve value of custom meta box page

I have added a custom meta box, once i update the data, it saved successfully. But now i am trying to fetch using this code, but it is not getting content of meta box saved. Please help me out.
<?php
global $post;
$code = get_post_meta( $post->ID, 'caption_code_footer', true );
if($code != ''){
echo $code;
}
else { ?>
I am lorem Ipusm Text
<? } ?>
This code appears okey, so you need debug a bit....It's sound like $post->ID has wrong value... try echo it. echo $post->id; or all the object var_dump($post);
Also Check If you arent in right context. For example in category context you need to use the loop...
I think something error on update your meta box please try this..
$footer=$_POST['caption_code_footer'];
update_post_meta($post_id,'caption_code_footer',$footer);
for echo the value updated..
get_post_meta($post->ID, 'caption_code_footer', true);

wordpress php targeting multiple ids to change colour

Okay, so i've got a request for this update which I thought might be simple enough but is turing out a bit tricky than I though.
basically the site has these sub ids for each property 'Vest' and 'Verve' they want each one to be a different colour.
The css that's link to them is <span class="proptery-type-grid"> so obviously I'd add in a second css for each colour, but the id's in the php are being called like this: <span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>
so what would be the best way (i'm thinking like a if or else statement) to go about targeting the individual ids to change the colours of them?
Thanks
Maybe you can use a ternary statement? http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Psuedocode:
echo ($post->ID('1') ? '<span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>' : '<?php echo inspiry_get_property_types( $post->ID );?>');
I've solved it with
<span class="proptery-type-grid"><?php $post_terms = get_the_terms( $post->ID, 'property-type' );
foreach( $post_terms as $term) {
if( $term->name == 'Verve') {
echo '<span class="proptery-verve">Verve</span>';
} elseif( $term->name == 'Zest') {
echo '<span class="proptery-zest">Zest</span>';
} else {
echo '<span class="proptery-type-grid type-none"></span>';
}
}?></span>

Separate multiple meta values with slashes within a $list (wordpress)

I want to list some custom posts (wordpress) and their meta values. Everything works fine so far, but I have to work with empty values too. And I want to use slashes to separate multiple values. So here's what I have so far:
$list = '<div class="clearfix vlist">
<div class="vlist-content">' ;
while($wpex_query->have_posts()) : $wpex_query->the_post();
global $post;
$list .= '<div class="vlist-item">
<div class="title">'.get_the_title().'</div>
<div class="info">'
.get_post_meta( $post->ID, 'wpex_vita_genre', true ).' / '
.get_post_meta( $post->ID, 'wpex_vita_director', true ).' / '
.get_post_meta( $post->ID, 'wpex_vita_role', true ).'</div>
</div>';
endwhile;
wp_reset_query();
return $list . '</div></div>';
Problem comes within div.info, where I want to display 3 values, separated by slashes (/). Soon as one or two values are empty I get stupid double slashes // because these characters are not conditional. I don't know how to make them appear only when there are values to separate because I can't just put an if statement into my $list. I know I know now you people who know php are just shaking heads or doing facepalms because this for sure is easy pie... could you still tell me how to solve this problem?
Thank you!!
I just came across the solution with the right use of conditional statements when defining the meta-values as variables, for example:
if((get_post_meta( $post->ID, 'wpex_vita_genre', true )) != '') $genre = '<span>' . get_post_meta( $post->ID, 'wpex_vita_genre', true ) . ' / </span>';
Simple. Still took me years to get it right. Maybe s.o. will save some seconds reading my solution :D

Categories