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
Related
I'm making a WordPress theme by myself since I'm working for the first time in Wordpress I've watched some tutorials about it.
I have page.php header and footer and ofc an index. I insert the content from the pages with this:
<?php echo get_post_field('post_content', $post->ID); ?>
but I tried the get_post in a while loop with same result..
Everything is fine but when I want to use a plugin I can't add to my page... When I insert the shortcode of it it shows only the shortcode string... There are some plugins where I can click a "view" option and it would show a page with my plugin (for example a calendar) but that page is empty...
When I activate an original theme it works instantly... So I'm sure something is missing from my theme something which can load the plugins but I couldn't find solution for it.
Any ideas?
Did you add the <?php wp_head(); ?> function before the head area of the html document is closing? It imports important scripts and styles from wordpress itself (and probably also from the plugins).
See here:
https://developer.wordpress.org/reference/functions/wp_head/
Before closing the body area, the template should also include
<?php wp_footer();?>
See here:
https://developer.wordpress.org/reference/functions/wp_footer/
I have a php script that reads a text file of 10000 urls one in each line. The script displays all the urls in a blog post. I want the page to be divided into 10 small pages to facilitate comfortabe browsing. So how to add a pagebreak using <?php wp_link_pages(); ?> function inside a post? Something like this:
<?php
echo "Hi, This is First Page";
wp_link_pages();
echo "Hi, This is Second Page";
?>
That isn't how wp_link_pages(); works. It only displays your the_content in pages if your the_content contains this tag <!--nextpage-->
sudo code
<?php
echo apply_filter ("the_content" , "Hi, This is First Page<!--nextpage-->Hi, This is second Page");
wp_link_pages();
?>
You might want to check out this question as to how to retain the page data if you are creating your own page content and not using the standard content area.
Wordpress PHP - need to retain nextpage paginating tag in custom query
When already in a php block, you can execute a function without opening another php block. According to the documentation, wp_link_pages(); should also be called without echo.
wp_link_pages();
Documentation:
https://codex.wordpress.org/Function_Reference/wp_link_pages
This works just fine:
global $post;
$total = substr_count($post->post_content, '<!--nextpage-->') + 1;
In the midst of Customizing my new Wordpress site I would like to add next and previous portfolio post buttons to the single portfolio post page, and I've found that I can easily do so by pasting the following code:
<div><?php previous_post_link('%link', 'PREV'); ?> | <?php next_post_link('%link', 'NEXT'); ?></div>
Somewhere in the single-flv_portfolio.php page.
The only problem with this solution though is that I'm only able to place the links/buttons in the "portfolio frame" so to speak, and not in the actual content of every single post, which I would like to, for layout purposes.
I've tried pasting the same code within the contents of a portfolio post, using the backend editor, but to no avail. Wordpress, or my specific template (Wizard) seams not allow me using php at all - not even for something small like echoing out the current year in a dynamic copyright function.
Is it possible to maybe create the next and previous portfolio post functions in a custom .php-file, and then call them with an html anchor tag, and if so, how would I go about doing that?
Try something like this in your functions.php file:
function my_next_previous_link($content) {
$content .= 'some html here';
return $content;
}
add_action('the_content','my_next_previous_link');
I have wordpress page with the following shortcodes
eg
page1
[myshortcode id='1']
[myshortcode id='2']
[myshortcode id='3']
... etc
Another has
page2
[myshortcode id='4']
and another has
page3
[myshortcode id='5']
What I want to do is get a php list of all the shortcodes on any selected page, eg page2, with the paramaters, i.e. the id.
The reason for this is I am writing a plugin wrapper to take an existing shortcode off a page and 'ajax' it, as a link in the currently generated code refreshes the whole page.
So my question is, how do I get a list of used shortcodes with paramaters from a page, using php or jquery?
You should check this out: http://codex.wordpress.org/Shortcode_API
Apparently all shortcodes are listed inside the $shortcode_tags:
<?php
global $shortcode_tags;
echo "<pre>"; print_r($shortcode_tags); echo "</pre>";
?>
You should be able to use this to find the active shortcodes on a PHP page, I guess.
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));