I am trying to incorporate an image slider shortcode into a custom WordPress theme template, so that the client will be able to paste the shortcode into the custom field and the template will read this and display it correctly. I do not want the client to paste the shortcode in the body of the post, as the slider needs to be displayed outside of the post wrapper (at full browser width).
I don't know much about php, so would really appreciate any help with this!
The code I have so far to display the slider via the template is:
<?php echo do_shortcode("[metaslider id=27]"); ?>
And to display the output of custom fields I have:
<?php echo get_post_meta($post->ID, 'slider', true); ?>
Each of these works on its own, but I need to combine them, so that the client doesn't have to edit the template just to add the shortcode. So I am thinking something like this should work:
<?php echo do_shortcode("[<?php echo get_post_meta($post->ID, 'slider', true); ?>]"); ?>
... but it doesn't.
Many thanks in advance for any help with this.
C
Simply pass the return value of get_post_meta (which would contain the shortcode as far as I understood) to the do_shortcode function as an argument:
do_shortcode(get_post_meta($post->ID, 'slider', true));
Related
G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)
I am developing a custom site and I am pulling in data from a wordpress backend to display on my page. The page that I am developing is completely outside of the wordpress directory. So far I have figured out a way to get the content to display on my page, however if the content has a gallery in it, the gallery doesn't display correctly on my page. What I see is the shortcode text [gallery ids="35,29"]. I am assuming that I need to include some functions or some other files in order to get my page to render the shortcode correctly? Here is what I have in my file so far:
At the top of my file:
<?php
define('WP_USE_THEMES', false);
require('../wp/wp-config.php');
//get_header();
function get_content($id) {
$post = get_page($id);
$content = apply_filters('get_the_content', $post->post_content);
echo $content;
}
?>
then in the page I have the code to display the content:
<?php get_content(25); ?>
Can someone please help?
Use do_shortcode to allow the shortcode to filter through it's own handler's and functions. This will return the formatted content.
Replace 'get_the_content' with 'the_content' and running that through do_shortcode.
I want meta description of currently executing page in the header.php file in wordpress.
Wordpress function "get_post_meta" is not useful for this. It only returns custom meta tags.
can anyone help me out.
I'm going out on a limb and assuming that you're generating meta description tags dynamically. If that's the case, this should get you what you need...
<?php
echo get_post_meta($post->ID, 'description', true);
?>
If, on the other hand, that's not what you're doing... that method will not work.
I have a custom wordpress post.php, all posts within a specific category will use this template. In nearly every post there will be scrolling testimonials, to handle this I am using a layer slider.
Within the page I have a custom field of testimonial that is filled with something like [layreslider id="2"]
In my php file I have:
<div class="trips-testimonials">
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
</div>
This seems to do nothing. If I add echo to the PHP:
<?php echo do_shortcode(get_post_meta($post-ID, 'testimonial', true)); ?>
I get the output of [layreslider id="2"]
Here is a sample page, the blue box under the photo is where the slider should show up.
http://www.ct-social.com/ctsdev/aff/capri/
Thank you very much for your help.
looks like you miss the > from post Id so it should read
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
Also you may need to call global $post; if outside the loop.
i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php
i have created a page called home in the wordpress admin-paned and inserted into my code the following:
<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
<?php $home_id = 43;
$home_page = get_page( $home_id );
?>
<?php echo $home_page->post_content; ?>
</div> <!-- end #home_page -->
and non of the shortcodes that i have in the page work.
i installed a php in post or page and tried useing php and it doesnt work.
when i insert
echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');
directly into the code it works.
does anyone know y this happens?
thank you.
I got an answer in wordpress.stackexchange.com
I Quote:
You need to apply the filter the_content e.g.:
<?php echo apply_filters('the_content',$home_page->post_content); ?>
Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.
Thank You Tom J Nowell
The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.
You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.
If you don't want to use a Loop, you could output the content using
echo do_shortcode($home_page->post_content);
This will run the post content through the shortcode processor, giving the shortcodes a chance to run.
For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content