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
Related
I would like to modify a posts content with the "the_content" filter.
Inside this filter I would like to load another posts content.
This is my code:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$module = get_post( 12345 ); // load specific post
echo apply_filters( 'the_content', $module->post_content );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Sadly this creates a 500 Internal Server Error.
I guess because I created an endless loop. Do you have any idea how to get the formatted content of another post inside the "the_content" filter?
Thanks :-)
Jan
EDIT
A bit more details: I created a custom post type called "sidebars" where I edit the content with a page builder. I would like to add this sidebars with PHP.
SOLUTION
With the "Elementor" page builder you can do this:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$elementor = \Elementor\Plugin::instance();
echo $elementor->frontend->get_builder_content( 12345 );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
I have a Wordpress site, and I want to set up a paywall for certain content.
I would like to show some intro of each such article for Google index and users as well, but rest of it would be hidden for guests and available for logged in (in my case paid users).
I successfully implemented is_user_logged_in() PHP statement from Wordpress, but I am not finding PHP code anywhere online about hiding everything after n-characters, or words.
My intended workflow is:
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
echo 'show only 200 characters or words of the content code should be here';
}
?>
You can use WordPress default function wp_trim_words.
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content(); // $content is whatever your content field.
echo wp_trim_words( $content, 200, ' ' );
}
?>
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content();
//If your template has some other way of getting the post content then use that variable.
echo substr($content, 0, strpos($content, ' ', 200));
}
?>
I need to output a custom 'read more' button for every post.
I use the following function that I've made:
function the_excerpt_more_link( $excerpt ){
$post = get_post();
$url = 'https:/www.vierenzestig.nl/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Lees meer!';
return $link;
}
This is working as expected and it outputs the following :
https:/www.vierenzestig.nl/interior-car-wash-and-detailing-service
When I try to wrap the above output inside an element. It also displays the home url from the website. Using this code (see the replacement of $link with $excerpt):
function the_excerpt_more_link( $excerpt ){
$post = get_post();
$url = 'https:/www.vierenzestig.nl/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Lees meer!';
return $excerpt;
}
This outputs the following :
An link with the text: 'Lees Meer'. But the link is navigating to the following :
https://HOMEURLHERE/https:/www.vierenzestig.nl/interior-car-wash-and-detailing-service
Why is the same use of code resulting in a different output of code?
I am using a function that returns the content for specific posts of type 'product.' However, I want to check the length of the content, and if it is under a certain length, I would like to add a class to center the content (I have it left-aligned by default). I understand how to write the IF statement to check the length (so I left it out of the example). But I am having an issue returning the content with the class. Here is the function.
function post_product_description() {
$product_title = get_the_title();
$the_query = new WP_Query( array(
'post_type' => 'product',
'name' => $product_title
) );
while ( $the_query->have_posts() ) : $the_query->the_post();
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
//$content = '<span class="centered">'.$content.'</span>';
endwhile;
wp_reset_postdata();
return $content;
}
add_shortcode('product_description', 'post_product_description');
If I just return the content as
return $content
it outputs the content in a <p> tag just fine like this:
<p>the content text here</p>
However, if the length of the content exceeds a certain number, I want the <span> tag to output around the text like this:
<span class="centered">the content text here</span>
The problem is, if I try to append the <span> tags to my $content variable, as I am doing in the commented line in the original code, wordpress outputs like this:
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>
How do I return $content without it adding all of those extras <p> tags and with it wrapping the <span> tag around the content?
maybe you are looking for something like this:
functions.php
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() ):
$length = strlen($content);
if ($length> 500) //
{
$content = sprintf('<span class="custom-class">' .$content. '</span>');
}
endif;
// Returns the content.
return $content;
}
This code will output the content wrapped inside span class="custom-class" you can check more about it at the Wordpress codex :
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
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.