Hi All
I'm wondering if someone could provide code for me please to insert into my theme's functions.php file that would enable me to insert the content of a post (that is identified by post title) into any other post or page. Let me explain.
Let's say I create a post called "Copyright". That post contains the terms of a copyright notice. In subsequent posts and pages, I want to be able to type something like "[clause-Copyright]" and have the content of the Copyright post displayed (please note that I only want to show the post's body content, not the post title).
In that subsequent post or page, I may in fact want to replicate the post body content of a number of different posts, like this:
[clause-Copyright]
[clause-Privacy]
etc etc
I know there are some existing "Boilerplate" plugins out there that enable this sort of functionality for specific custom post types but they pose problems for me because, for example, I need more custom fields for the posts than they are set up to allow (as I need to associate other metadata with the post content).
I've been reading up on shortcodes and am thinking this might not be that hard but don't have time to get the bottom of it myself. Any help much appreciated.
Many thanks.
Richard
Maor Barazany answers:
Hi, Please try this code I've just written for you -
* I had some mistakes in my previous version, please use this updated checked working shortcode:
add_shortcode('cluase', 'tc_generate_clause');
function tc_generate_clause($atts) {
extract(shortcode_atts(array(
'slug' => ''
), $atts));
$args = array('numberposts' => 1,
'post_status' => 'publish',
'name' => $slug,
'post_type' => 'page'
);
$p = get_posts($args);
$output = $p[0]->post_content;
return $output ;
}
* I assume that the slug is of a page. If it is a post , you will change the line -
<strong> 'post_type' => 'page'</strong> to <strong>'post_type' => 'post'</strong>
Usage as a shortcode -
[cluase slug='Copyright']
It will bring the content of the post that its slug is Copyright.
Maor Barazany comments:
I just noticed that you want to use title and not slug (I think you better use the slig).
Anyway, for title, you can use this code, based on my first code, but will use title and not slug -
add_shortcode('cluase', 'tc_generate_clause');
function tc_generate_clause($atts) {
extract(shortcode_atts(array(
'title' => '',
'type' => 'page'
), $atts));
$p = get_page_by_title($title, OBJECT,$type);
$output = $p->post_content;
return $output ;
}
Usage as a shortcode, this will bring the content of the page named - test this
[cluase title='test this']
Page will be the default, but if you want a post and not a page, you will use -
[cluase type=post title='test this']
Nathan Rice answers:
@Maor
He wants something that pulls by page title, not slug.
Try this:
add_shortcode( 'get_page_content', 'get_page_content_shortcode' );
/**
* The shortcode function to pull post content from a page.
*
*/
function get_page_content_shortcode( $atts ) {
$atts = shortcode_atts( array(
'title' => '',
'post_type' => 'page',
), $atts );
if ( ! $page = get_page_by_title( $atts['title'], OBJECT, $atts['post_type'] ) )
return;
return apply_filters( 'the_content', $page->post_content );
}
EDIT: Sorry, here's the usage:
[get_page_content title="Page Title"]
Or, if you want to pull content from a post, rather than a page, try this:
[get_page_content title="Post Title" post_type="post"]
Hope that helps!
Richard comments:
Many thanks to you both. I couldn't get Maor's code working but I can get your code working Nathan. Thanks.
Nathan, would you mind tweaking your code slightly please so that:
(1) the default post type is post rather than page
(2) I can reduce the length of the shortcode to this:
[clause="Post Title"]
Could you also let me know please whether this code supports custom post types through use of "post_type" attribute, so that if - in future - I were to create a custom post type called "widgets" I could call the body content of a widget custom post type post in the same way as regular posts?
Many thanks for the super fast help with this. Again it shows me why WPQs is such a great site.
Cheers
Richard
Nathan Rice comments:
@Richard
Here's the updated code:
add_shortcode( 'clause', 'get_page_content_shortcode' );
/**
* The shortcode function to pull post content from a page.
*
*/
function get_page_content_shortcode( $atts ) {
$atts = shortcode_atts( array(
'title' => '',
'post_type' => 'post',
), $atts );
if ( ! $page = get_page_by_title( $atts['title'], OBJECT, $atts['post_type'] ) )
return;
return apply_filters( 'the_content', $page->post_content );
}
And yes, you can use the post_type flag to pull in custom post type content too.
Richard comments:
Thanks Nathan but this code produces blanks when I use this style of shortcode:
[clause="Post Title"]
Do you know why that's happening please?
Nathan Rice comments:
That's not the correct usage. Try this:
[clause title="Post Title"]
Richard comments:
Thanks Nathan. Just spotted that myself. Works great now, thanks.
Richard comments:
Hi All. I just wanted to say a big thank you for all your help. Very much appreciated. I've voted and I guess payment will be made soon. Thanks again.
Travis Smith answers:
@Maor,
Nathan's solution is the correct answer. If you want to create shortcodes for specific posts/pages then, try this:
Say you have a page called "Copyright". Then shortcode would be [copyright]
add_shortcode( 'copyright', 'get_copyright_shortcode' );
/**
* The shortcode function to pull post content from a page.
*
*/
function get_copyright_shortcode( $atts ) {
$atts = shortcode_atts( array(
'title' => 'Copyright',
'post_type' => 'page',
), $atts );
if ( ! $page = get_page_by_title( $atts['title'], OBJECT, $atts['post_type'] ) )
return;
return apply_filters( 'the_content', $page->post_content );
}
Then you have a page called "Privacy". Then shortcode would be [privacy]
add_shortcode( 'privacy', 'get_privacy_shortcode' );
/**
* The shortcode function to pull post content from a page.
*
*/
function get_copyright_shortcode( $atts ) {
$atts = shortcode_atts( array(
'title' => 'Privacy',
'post_type' => 'page',
), $atts );
if ( ! $page = get_page_by_title( $atts['title'], OBJECT, $atts['post_type'] ) )
return;
return apply_filters( 'the_content', $page->post_content );
}
If you want the content to output a header (<h3>), then add:
add_shortcode( 'copyright', 'get_copyright_shortcode' );
/**
* The shortcode function to pull post content from a page.
*
*/
function get_copyright_shortcode( $atts ) {
$atts = shortcode_atts( array(
'title' => 'Copyright',
'post_type' => 'page',
), $atts );
if ( ! $page = get_page_by_title( $atts['title'], OBJECT, $atts['post_type'] ) )
return;
$output = '';
$output .= apply_filters( 'the_title', '<h3>'.$page->post_title.'</h3>' );
$output .= apply_filters( 'the_content', wpautop($page->post_content) );
echo apply_filters( 'the_output', $output );
}