Get_field value to show in shortcode - php

Im trying to use do_shortcode to display data from an advanced custom field which is in the admin cms.
I have an advanced customed field called 'meta_slider_shortcode'. I would like the do_shortcode to display the data that is in the meta_slider_shortcode' field.
Here is my php:
<?php
$meta = get_field( "meta_slider_shortcode" );
echo do_shortcode('$meta');
?>

Check the value of $meta. If it does not contain square brackets then add it in do_shortcode like do_shortcode("[$meta]");

The short code cant have ' ' wrapped around the variable.
Here is a working example:
<?php
$meta = get_field( "meta_slider_shortcode" );
echo do_shortcode($meta);
?>

if you have short code then use it some thing like this
$yourvar = do_shortcode( '[your-short-code]' );
echo $yourvar;
or
echo do_shortcode( '[your-short-code]' );
in your case it would
echo do_shortcode(get_field( "meta_slider_shortcode" ));

Related

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']" );
?>

Nesting a Wordpress function within a shortcode within a function

I need to use a plugin shortcode within a template file, but have one of the variables be set by the value of a custom field in the post. Here's what I have so far. This just breaks the entire page.
My effort (not working):
<?php echo do_shortcode('[wpsp pls_id="get_post_meta($post->ID, 'playlist', true)"]'); ?>
The shortcode:
[wpsp pls_id="NEED VALUE SET HERE"]
The custom field function:
<?php echo get_post_meta($post->ID, 'playlist', true); ?>
Thanks.
Try this:
<?php echo do_shortcode('[wpsp pls_id="' . get_post_meta($post->ID, 'playlist', 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)); ?>

How do I get my custom fields to only show when something is written in them - Wordpress?

Im working on customizing a Wordpress theme with some custom fields. For this I'm using the plugin "Advanced Custom Fields", but I want it to display these fields ONLY IF something is written in them. This is the code I'm using to display the custom fields:
<p class="tittelboks">Artikkelforfatter:</p>
<?php if( get_field('artikkelforfatter') )
{
echo '<p>' . get_field('artikkelforfatter') . '</p>';
} ?>
How do I change the code so that it only echos the information AND the label (in this case .tittelboks) if something is written in the meta boxes?
Michael
<?php if( $field = get_field('artikkelforfatter') ): ?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $field; ?></p>
<?php endif; ?>
This is a different way of doing an if statement, so you don't have to enclose your HTML in quotes and worry about escaping. The two lines in the middle will only print out if get_field('artikkelforfatter') returns a value or true. That value will be assigned to the $field variable.
Something like this should work:
<?php
$artikkel = get_field( 'artikkelforfatter' );
if ( ! empty( $artikkel ) ) {
?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $artikkel; ?></p>
<?php
}
?>
To use and display custom fields need you may try to put this in your loop.
<?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>
<?php echo $what_name_you_want; ?>// This call the value of custom field
Only replace
what_name_you_want with your favorite name
and
Your Custom Field Name with the name of custom field
If the value of custom field is empty the echo will be empty to.
Tell me if it's work

Wordpress stripping out custom field tags

I am using wordpress get post meta to store the value of a custom field into a variable. In this particular instance, It is actually grabbing the post's parent's custom field due to the $post->post_parent Here is the code:
<?php $cast_members = get_post_meta($post->post_parent, 'cast_members', true); ?>
<p><?php echo $cast_members; ?></p>
<?php endif; ?>
The custom field cast_members is a series of cast members, each in its own paragraph. For some reason wordpress is stripping out the paragraph tags and displaying all of the cast members in a continuous string. I need to retain those paragraph tags so each cast member is on its own line. Any idea why get_post_meta strips out tags and how to fix it?
Why not do something like this,
<?php
$mykey_values = get_post_custom_values('cast_members',$post->post_parent);
foreach ( $mykey_values as $key => $value ) {
echo "</p>". $value ."</p>";
}
?>
Or you ca use your original query but just add a foreach to echo out the value wrapped in <p> tags.
Thanks to #Vinod Dalvi the answer involves using wpautop like so:
<p><?php echo wpautop($cast_members); ?></p>

Categories