Is there a way to get the content of an entire wordpress page? My problem is, I want to include page content on another page (to create a one-page layout). What I tried was this:
$post = get_post($the_page_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
But the page has its own template and I would like to display the entire page, including whatever is done in the template.php file. Is that possible?
Well, I came up with a solution for this. It might not be the best way but this worked for me. I created a page "home" to represent the one-page page. All subpages of this page will have a certain template which does not include header or footer. My home template looks like this:
//fetch header as usual
get_header();
//fetch all subpages of this page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
$children = get_page_children( get_the_ID(), $all_wp_pages );
//browse these subpages and get the content for each one
foreach($children as $child){
$post = get_post($child->ID);
getOnePageContent($post);
}
//footer
get_footer();
Now in functions.php I defined the getOnePageContent:
function getOnePageContent($page){
global $post;
$post = $page;
$slug = get_page_template_slug( $page->ID );
if($slug){
$pl = get_the_permalink($page);
$content = file_get_contents($pl);
echo $content;
} else {
$content = apply_filters('the_content', $page->post_content);
echo $content;
}
wp_reset_postdata();
}
As long as the subpages do not contain headers or footers this works fine. Of course, I do not think it's an elegant or good solution to fetch the page with file_get_contents but at least it works.
Related
Created a page and want to display all the content on the header.php, but it does not display anything.
code below:
$page = get_page_by_title( 'TEST' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
Is there another way to do it?
Try to do this (just replace 123 with your page ID):
$page_object = get_page( 123 );
$my_content=$page_object->post_content;
echo do_shortcode( $my_content );
Use following code to get:
$page = get_page_by_title('TEST', OBJECT, 'page');
echo $page->post_content;
Use following code if there is shortcode added in page and text added in the page assume Contact page:
$page = get_page_by_title('Contact', OBJECT, 'page');
$page->post_content;
// will match square brackets
if (preg_match('^\[(.*?)\]^', $page->post_content))
{
echo do_shortcode($page->post_content);
}else{
echo $page->post_content;
}
Use if you have text as well as shortcode in the page replace 226 with your page id;
$id=226;
$post = get_page($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
Read: https://codex.wordpress.org/Function_Reference/get_page_by_title
To organize better my code, I would like to split some of my HTML in templates.
I have a loop inside a shortcode to get the posts:
function nh_shortcode_func($atts) {
$posts = get_posts_by_category(.......);
$html .= '';
foreach ($posts as $post) {
$post_id = $post->ID;
$post_title = $post->post_title;
$post_image = get_field('image', $post_id);
$html .= **HERE THE CODE OF HTML WITH DIVS AND CLASSES**
}
return $html
}
add_shortcode('nh-content', 'nh_shortcode_func');
Instead, put my HTML code inside a variable, I would like to use another file with the HTML structure that I'm going to use to build the posts. Also, I would like to pass the dynamic data to it.
Any ideas?
Thanks.
Just use an include file and it will have access to the variables. Something like:
function nh_shortcode_func($atts) {
ob_start();
include dirname( __FILE__ ) . '/templates/nh_shortcode.php';
return ob_get_clean();
}
add_shortcode('nh-content', 'nh_shortcode_func');
I'm trying to create a shortcode to render custom php pages. I'm getting very odd behaviour which i'm not getting if I used a wordpress template.
For example. If i create a template with the code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_article_schema( 'BlogPosting' ); ?>>
<div class="inside-article">
<?PHP $content = apply_filters( 'the_content', get_the_content() );
global $post;
$post_info = get_post($post->ID);
$content = $post_info->post_content;
$ind = strrpos($content, "/");
$page = substr($content, $ind+1);
$path = substr($content, 0, $ind+1);
$oldDir = getcwd();
chdir($_SERVER["DOCUMENT_ROOT"].$path);
include($page);
chdir($oldDir);
?>
</div><!-- .inside-article -->
</article><!-- #post-## -->
Then in the wordpress page i simply enter the location of the file, e.g. /contact/contact_form.php. The contact_form.php page renders on the screen within the wordpress theme.
Now, i recently discovered shortcodes, and thought these would be a better way of rendering the php on the page. Instead of having the content of the page as /contact/contact_form.php I could have [custom_php filepath="/contact/contact_form.php"]
This is where i'm getting problems. The contact_form.php seems to have many require_once() functions all over the place, yet the variables wtihin them don't seem to exist, even though the first method (above) works fine.
For my shortcode code I have:
function subscribe_custom_php_shortcode($atts, $content = null) {
$atts = shortcode_atts(
array(
'filepath' => '/index.php'
), $atts);
$ind = strrpos($atts["file"], "/");
$page = substr($atts["file"], $ind+1);
$path = substr($atts["file"], 0, $ind+1);
$oldDir = getcwd();
chdir($_SERVER["DOCUMENT_ROOT"].$path);
ob_start();
include($page);
$content = ob_get_clean();
chdir($oldDir);
return $content;
}
add_shortcode('custom_php', 'subscribe_custom_php_shortcode');
As you can see, the code is very similar except I added ob_start() and ob_get_clean() to get the results of the PHP based on someones comments. Removing these still doesn't seem to bring back the values of the require_conce within the contact_form.php.
What's interesting is if i change the require_once for include it works.
Any ideas?
Thanks
Assuming filepath is relative to theme directory, this should be the best way to include it...
function subscribe_custom_php_shortcode($atts, $content = null) {
$atts = shortcode_atts(
array(
'filepath' => '/index.php'
), $atts );
ob_start();
include get_stylesheet_directory() . $atts['filepath'];
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_php', 'subscribe_custom_php_shortcode');
As you can see... we use the get_stylesheet_directory function to get the current theme directory.
Hope that helps ๐
I am supporting a website running on Wordpress 3.8 with a wpzoom theme (zenko) on top of that with most of the articles posts running on multiple pages.
For pagination navigation we use the WP-PageNavi plugin http://wordpress.org/extend/plugins/wp-pagenavi/ with the function
<?php wp_pagenavi( array( 'type' => 'multipart' ) ); ?>
inserted in the single.php code after the
<?php the_content(); ?> function.
Recently we added the WordPress Related Posts http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/ . However when activated the related posts appear before the page navigation links- buttons and I would prefer to change it to appear after them.
Looking around in the related post plugin's code I found the corresponding formating code as follows:
global $wp_rp_output;
$wp_rp_output = array();
function wp_rp_add_related_posts_hook($content) {
global $wp_rp_output, $post;
$options = wp_rp_get_options();
if ($content != "" && $post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
if (!isset($wp_rp_output[$post->ID])) {
$wp_rp_output[$post->ID] = wp_rp_get_related_posts();
}
$content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
$content = $content . $wp_rp_output[$post->ID];
}
return $content;
}
add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
My question is how can I modify the code so that the array of related post appear after the page navigation links
I was wondering what is the exact code to be able to get the title on the attachment image page. Here is the code I have right now in my image.php file for the title portion.
<h2><?php echo get_the_title($post->post_parent); ?> <?php echo ' ยป $attachment->post-title'; ?></h2>
So I want it to show the main post title then the double right arrow and then the single image title but the $attachment->post-title is not the correct command for this.
I am trying this on my test site. Here you can see the exactly what I am referring to. http://pandafeed.net/gave-her-a-bath-and-tucked-her-in-she-passed-out-right-away/thumbnail-for-1407/
Thanks in advance
This should fetch the title for each post's first attached image on the front page (or any post that's part of a list):
<?php
if (! is_singular()) {
$image_id = get_the_ID();
// Not necessary, but it's an option:
//$permalink = get_permalink($image_id);
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_parent' => $image_id
);
$attachments = get_posts($args);
if ($attachments) {
$title = get_the_title($attachments[0]->ID);
print $title;
}
}
?>