Check for custom fields - php

Basically, on my website I'll be embedding two different types of objects, one will be a Youtube embed, one will be a soundcloud embed. I'll be doing this by using custom fields on Wordpress and pulling in through the post_meta.
This is the current code that I have (which pulls in the current 'song' field, which is YouTube)
<?php echo get_post_meta($post->ID, 'song', true); ?>
When I try adding that and this:
<?php echo get_post_meta($post->ID, 'soundcloud', true); ?>
Nothing actually happens, is there a way to make it so it will check if the fields are null or not and then display the one that is in fact, not null.
Any help is appreciated, thanks

Something like
<?php
$song = get_post_meta($post->ID, 'song', true);
if (empty($song)) {
echo get_post_meta($post->ID, 'soundcloud', true);
}
else {
echo $song;
}
?>
should do the trick. Though if both are null it won't output anything.

Related

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);

How to add post meta tag inside do_shortcode in wordpress

I'm using Coupon Creator and I like that it comes with the categories and shortcodes. I am using a category shortcode like this example [coupon couponid="loop" category="Brumby's Bakery" couponorderby="date" coupon_align="cctor_alignnone" name="Coupon Loop"].
I wanted to use different category shortcode for each page. Therefore, I kinda created a custom meta box for that (Wordpress Meta Boxes) and called it in the php template page with <?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>. However, the shortcode won't work in php template page unless I use <?php echo do_shortcode('[coupon...]'); ?>
In the end this code which was supposed to be like that wouldn't work because of php in php.
<?php echo do_shortcode(<?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>); ?>
How do I rework this php code? The only thing I could think of is to use something like var but I don't know how to make it work in php template page.
var $store = <?php echo get_post_meta($post->ID, 'couponshortcode', true); ?>
<?php echo do_shortcode($store); ?>
No need to echo get_post_meta(). This should work:
<?php echo do_shortcode(get_post_meta($post->ID, 'couponshortcode', true)); ?>

Get_post_meta Wordpress

Need help with a wordpress get_meta_post.
I need to display a div only if the custom-field promo is found in the get_meta_post. If true this is suppose to echo the :
<?php get_post_meta(get_the_ID('promo', true)
<div class="packagePromoItem">Promotion</div>
?>
Assuming that's your actual code, you have several typos, or major misunderstandings about how PHP works. This should work (using alternative syntax, which I think is a little more readable for this):
<?php $promo = get_post_meta(get_the_ID(), 'promo', true); ?>
<?php if ($promo): ?>
<div class="packagePromoItem">Promotion</div>
<?php endif; ?>
I've also assigned the promo post meta to its own variable so it's easier to follow.
You're using get_the_ID wrong. Get the ID does not accept any parameters and gets the ID of the current post. If you need to check if the post has meta 'promo', then just check if get_post_meta returns null/false.
I'm not sure what you're asking through your example though. If you're trying to echo out the post meta:
<?php if (get_post_meta(get_the_ID(), 'promo', true))) { echo'<div
class="packagePromoItem">' . get_post_meta(get_the_ID(), 'promo', true) .
'</div>';}?>

link to the other website in "the_title()" of the WordPress page

So i have the single-portfolio.php which present one of my project.
This function makes proper title to my projects every time I choose one.
<h1><?php the_title(); ?></h1>
Now whatever project i choose it always transport me to the Angela...
<h1><?php the_title(); ?></h1>
What i want to do is to have proper link to the proper project in the title.
I figured sth like this but it does not work.
<?php
$f = "http://facebook.com/";
$t = "http://twitter.com/";
$l = "http://linkedin.com/";
if (the_title()=='Facebook') {
Echo "<a href=$f> Facebook</a>";
} elseif (the_title()=='Twitter') {
Echo "<a href=$t> Twitter</a>";
} else {
Echo "<a href=$l> Linkedin</a>";
}
?>
What i get on the page is 3 times written Facebook if its Facebook page or 3 times Twitter e.g:
FacebookFacebookFacebook(the only last one "Facebook" is a link)
The problem is that the_title() echos the title, rather than returning it (as documented in the Codex). That means that, for each of your if conditions, the title gets echoed out.
Try using get_the_title() instead - this returns the post title rather than echoing it.
the_title() is a function that returns the title of the current page/post in wordpress. For the FB/Twitter/LinkedIn portion of your code you should just include "Facebook" instead of using the_title()
Use a custom field in your post to store the link in the post. Call the field "url" for example:
now you can read the field in your template and use it:
<?php $url = get_post_meta( get_the_ID(), "url", true ); ?>
<h1><?php the_title(); ?></h1>

Syntax for using echo inside an echo (wordpress slider)

this is the piece of code I'm not sure how to deal with:
<?php echo get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="InsertHere"]'); ?>
I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors.
Can anybody help?
Thank you :)
Don't echo the first value, use it directly inside your second echo.
<?php
echo do_shortcode('[smartslider2 slider="'.get_post_meta($post->ID, 'smartslider2', true).'"]');
?>
Or you can easily put that first value in a variable and use that variable in the second line
<?php $slider = get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="'.$slider.'"]'); ?>

Categories