How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.
Related
A lot of articles here seem to deal with the "read more" link not displaying. Mine is displaying fine, it's just not actually taking us to a page that displays the full blog post. You can see how it's displaying here (Read More >>):
Right now when I click on "Read More >>" I get this:
In my code, in home.php, I'm displaying the blog posts like:
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : '1';
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'order' => 'ASC', 'orderby' => 'title', 'paged' => $paged);
$posts = new WP_Query($args);
if ($posts->have_posts()): while ($posts->have_posts()) : $posts->the_post();
get_template_part( 'content', get_post_format());
echo "<div class='thepost'><p class='post-date'>".get_the_date()."</p>";
echo "<h2 class='post-title'>".get_the_title()."</h2>";
//echo "<span class='post-image'>".the_post_thumbnail()."</span>";
the_post_thumbnail('post-thumbnail', ['class' => 'post-image']);
echo "<p class='post-paragraph'>".get_the_excerpt()."</p>";
echo "<p><span class='read-more'>Read More >></span></p> </div>";
endwhile;?>
So what do I need to change about this line: echo "<p><span class='read-more'>Read More >></span></p></div>"; so that it links to the complete post that the user clicks on?
I've looked through the codex on how to customize the read more link, but I'm just finding
"Users can then continue reading more as you have enticed them with your summary introduction, by clicking on a link to the full article. Themes usually include this link in the title and the above methods will generate it by default trailing your teaser."
I must be missing something as I'm reading through the codex. Thanks!
EDIT: Inspecting the link:
EDIT 2 Inspecting a broken page:
EDIT 3: full CSS (style.css): https://pastebin.com/TaUnemz9
EDIT 4 full screenshot for individual blog post page:
Three things first of all I can't told you exact what is problem without any proper link.but as i was going through closely of your screenshot.I found that
you get link like http://localhost/fourth-blog-post
When you click on read more button.
but it should be like this http://localhost/blog/fourth-blog-post
Because its single post of all your posts and its comes from single.php file,
now you can do two things first check url of your post via admin panel where your post is appear. second if it matches the second url which i mentioned on above link. then you should try the_permalink() function at the place of get_permalink() for more information you can visit.
https://codex.wordpress.org/Function_Reference/the_permalink
because get_permalink() should not be use with in loop. and it should be the_permalink() with in the loop.
I hope it will help you:)
First of all, the key was that single post pages display via single.php according to the WordPress Hierarchy.
So I needed to create a single.php file - most of the code for this file will still be the same as the code for home.php - it will still be displaying the header, the hero image, the nav menu, the footer, and the content will still be contained in a <div>.
What's different is that I need to take this out:
$paged = ( get_query_var('page') ) ? get_query_var('page') : '1';
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'order' => 'ASC', 'orderby' => 'title', 'paged' => $paged);
$posts = new WP_Query($args);
And change the WordPress loop from this: if ($posts->have_posts()): while ($posts->have_posts()) : $posts->the_post(); to this: if (have_posts()): while (have_posts()) : the_post();. We already have the posts loaded, now we just need to display them.
Whatever WP functions I needed to get / echo changes depending on how displaying a single post is different from displaying multiple posts. I can take out:
get_template_part( 'content', get_post_format());
echo "<p class='post-paragraph'>".get_the_excerpt()."</p>";
echo "<p><span class='read-more'>Read More >></span></p> </div>";
Since I don't need them anymore.
The key is that links work differently in WordPress than basic HTML. It's not like in HTML where if you have Read More, you'll be taken to post-page.html when you click on Read More. Since it's a WP site, it's using PHP and you wrap the permalink in an <a> tag using the WP function .get_permalink().. Then the permalink will take you to single.php.
I'm having trouble setting up my Wordpress template. I'm not sure how to achieve the following. My website setup is the following:
I have a Custom Post Type "Chapter". A "Chapter" is parent to other CPT's. I have a few post types such as: reviews, interviews, blogposts, ...
On a chapter page I do a different WP_Query for every post type within this chapter.
page template single-chapter.php
Now I want to be able to click on "Blogpost (2)" and open an archive page of all blogpost within the current chapter. How can I achieve this? I assume I should create a template page "archive-blogpost.php". I already found that I can link to this page using:
<?php echo get_post_type_archive_link( 'blogpost' ); ?>
However I can't see how this page could know in what chapter i'm currently in?
I never used the WP Types plugin so far, but here is what you could do: add a chapter parameter to the archive link (with a rewrite rule for it), that you'll get in your blogposts archive template and display the posts conditionally to this parameter.
First we change the archive link to send the chapter slug:
<?php echo get_post_type_archive_link( 'blogpost' ) . '/' . $post->post_name; ?>
Then we define in functions.php this new rewrite tag:
function custom_rewrite_tag() {
add_rewrite_tag('%chapter%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
To make the URL looks good, we'll add a rewrite rule (so you don't have a url like /archive/?chapter=chapter-1, but /archive/chapter-1) - this still go to functions.php:
function custom_rewrite_rule($rules) {
add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
}
add_filter('init', 'custom_rewrite_rule', 10, 0);
You may have to change the URL / post type name depending of your configuration.
And last, you can get this query arg in your blogposts archive template with $wp_query->query_vars array:
$wp_query->query_vars['chapter']
Since I don't know much about WP Types, I'm not really sure about what follows, but it seems you could query the childs posts of the chapter with this:
if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
$childargs = array(
'post_type' => 'blogposts',
'numberposts' => -1,
'meta_query' => array(array('
key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
))
);
$child_posts = get_posts($childargs);
} else {
// default template : display all posts
}
I used a WP types functions to achieve what I wanted:
$child_posts = types_child_posts("blogpost", array('post_id' => $_GET['wpv-pr-child-of']));
With this I can query all the child posts from the custom post type on a page.
I also use a session variable to keep track of the current chapter I'm in.
I'm using this code to show recent posts with a shortcode that I found at smashingmagazine site. It's not working the correct way, I mean when I specify the number of posts to show, it just shows one post with every number I specify.
Here's the code:
function recent_posts_function() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string = ''.get_the_title().'';
endwhile;
endif;
wp_reset_query();
return $return_string;
}
function register_shortcodes(){
add_shortcode('recent-posts', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
I've changed the showposts number, but nothing happens. What's wrong?
Any suggestions?
Just before I start, never use query_posts to construct or modify any type of query. It outright fails in many scenarios, specially pagination, and breaks the main query, which you should never do.
If you need to construct custom queries, rather use WP_Query
Also, showposts have been deprerciated long time ago and have been replaced with posts_per_page
You should read the Shortcode API, this should give you an overview of what is happening and how shortcodes should be used and created. One important thing to remember here, shortcode content should be returned, not echo'ed. Here is also a tutorial that help me a lot.
Just a quick tip here, shortcodes should always go into a plugin. If you haven't yet created one, go and read MU-Plugin (must-use-plugin)
The correct way of constructing your shortcode would be as follows: (Your shortcode will be [my-shortcode]) This is untested though
add_shortcode( 'my-shortcode', 'my_custom_query_shortcode' );
function my_custom_query_shortcode( $atts ) {
ob_start();
$query = new WP_Query(array('orderby' => 'date', 'order' => 'DESC' , 'posts_per_page' => 1));
if ( $query->have_posts() ) :
while($query->have_posts()) : $query->the_post();
//YOUR LOOP ELEMENTS
<?php
endwhile;
$myvariable = ob_get_clean();
return $myvariable;
endif;
}
Just replace your code with mine, and make sure that you add your loop elements.
Just one more thing, remember, if you run any custom query on a page, and you have not reset that query, it will influence any other query on that page, even this shortcode, so always make sure that you reset all custom queries once you've done
I want the CMS to have the different pages (e.g. "Careers", "Jobs", "Team") with each having its own template, but then to combine them into one big scrollable page (e.g. "Our Company") that would have a template. How would I do this?
I know there used to be a function get_page but that's been deprecated (and replaced with get_post which is not the same thing), but that doesn't retrieve the page's template.
I want both the page and the template so I can output both into the main page.
I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page
Is this possible?
First for main page template choose default template and write you global elements there. And in this template use get_template part to include pages
<!--custom query for pages-->
<?php
$args= array('post_type'=>'page');
$query= new WP_Query($args);
$query->while(have_posts()):query->the_post();
$temp_name= get_page_template_slug( $post->ID );
$temp_name_exp =explode('.',$temp_name);
get_template_part($temp_name_exp[0]);
endwhile;
endif;
?>
and in career, blog etc pages
<?php
/*
Template name: Career or blog or something else
*/
?>
<?php the_tiele();
the_content();
?>
for
"I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page"
assign each pages wrapper to page slug example <section class="<?php echo $post->post_name; ?>"> and write a function to redirect your view page link to http://yoursiteurl/#page-slug
EDIT
In order to get one page's content into another use the following function:
function show_post($path){
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
and then create a template for the "Our company" page (like template-our_company.php) in which you will make a call to the function (e.g. <?php show_post('careers'); /* Shows the content of the "Careers" page using the slug. */ ?>).
So the template file should include something like this:
<?php
show_post('careers');
show_post('jobs');
show_post('team');
?>
For your 2nd question, you need to adjust the template-our_company.php file like this:
<?php
<div id="careers"></div>
show_post('careers');
<div id="jobs"></div>
show_post('jobs');
<div id="team"></div>
show_post('team');
?>
and then in the Menu dashboard, just adjust the navigation link to something like "/our-company/#careers" etc.
EDIT 2
In order to retrieve the content of pages with specified templates in another template, you can do the following:
Create the templates (files careers.php and jobs.php) and the posts that will be using those templates
/*
Template Name: Careers
*/
...
/*
Template Name: Jobs
*/
Then in the "parent" template, you can query the posts that have the above specified templates selected
untested code
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'value' => 'careers.php',
'compare' => '='
),
array(
'key' => '_wp_page_template',
'value' => 'jobs.php',
'compare' => '='
)
)
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_content();
// or add anything else
endforeach;
wp_reset_postdata();
#user3418748's answer was a good start for me, but in my case I needed to load specific pages, and I found that just using get_template_part() by itself wasn't loading any content because I was doing it outside a loop. In order to get this to work you need to first set the global $post variable to the page/post you want to display. Here's the function I used (replace mytemplate with the name of your tempalte):
function mytemplate_show_page($path) {
global $post;
$post = get_page_by_path($path);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month.
I'm using pages because I don't want to re-post a product on every new newsletter so I junt wanna display the posts inside the page.
In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.
Right now I'm using this code:
<?php
$productos = new WP_Query(array(
'post__in'=> array(81, 83),
'orderby'=>'title',
'order'=>'ASC'
)
); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
?>
It display the posts with the id of 81 and 83, I would like to show post by slug using 'name' as the codex says because is going to take some time to be checking the ids of the new post, instead of using the name of every new product but It doesn't work in array or I'm doing something wrong.
Now I will love to make something like this work
$names = get_post_meta($post->ID, "names", $single = true);
$productos = new WP_Query(array(
'name'=> array($names),
'orderby'=>'title',
'order'=>'ASC'
)
);
So every time I publish a new page I just write the slugs of the posts that I want to include in the page in a custom field, as you can see I'm not very good with php but I trying to learn and I search a lot for something that could work before asking in here.
I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don't like to depend to much on plugins.
Update:
So I'm now looking if I can make this using shortcodes, right now I have this:
function producto_func($atts) {
extract(shortcode_atts(array(
'nombre' => ''
), $atts));
global $post;
$pieza = get_page_by_title($nombre,OBJECT, 'post');
echo '<h1>'. $pieza->ID . '</h1>';
}
add_shortcode('producto', 'producto_func');
enter code here
So I just enter the shortcode [producto nombre="ff 244"] in the page and it show its ID, and I can add any number of shortcodes depending on the number of post I need.
But how can I show the entire content of the post.
Any idea?
I find I solution using Shortcodes.
So I put this on my functions.php page
function productos($atts, $content = null) {
extract(shortcode_atts(array(
"slug" => '',
"query" => ''
), $atts));
global $wp_query,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'name'=> $slug,
));
if(!empty($slug)){
$query .= '&name='.$slug;
}
if(!empty($query)){
$query .= $query;
}
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content() ?></div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("producto", "productos");
And in my page template I just write [producto slug="MY-SLUG"] and that way I can display multiple post just with the slugs. Hope someone find this useful.
From the Wordpress Codex:
Display post by slug:
$query = new WP_Query( 'name=about-my-life' );
Display page by slug:
$query = new WP_Query( 'pagename=contact' );
UPDATE
Try changing this:
'name'=> array($names),
To this:
'name'=> $names,
The 'name' - and 'pagename' - parameter does not take in an array. Only a string. A comma delimited list SHOULD give you what you need from within your Custom Fields titled "names", though I haven't tested this approach.
Also, thank you for using WP_Query instead of query_posts.