21

I have the following front-page template made:

enter image description here

In place of those large Lorem Ipsum blocks, I need to show an "excerpt" from a specific page to fill that box (a certain number of characters).

How do I get a pages content in String format so that I can echo it out and trim down to a certain number of characters?

9 Answers 9

33
<?php

// would echo post 7's content up until the <!--more--> tag
$post_7 = get_post(7); 
$excerpt = $post_7->post_excerpt;
echo $excerpt;

// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12); 
$trim_me = $post_12->post_content;
my_trim_function( $trim_me );

?>
1
  • based on your code that did not work for me I got this working if it helps anyone - $post_info = get_post( 24 ); $content = $post_info->post_content; echo $content;
    – Jon
    Apr 23, 2021 at 12:33
24

Here you go !

<?php
$my_id = 5369;
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
5
  • 5
    Please, explain what the code does and how it answers the question. Some users may not understand the code without a little explanation.
    – cybmeta
    Nov 16, 2015 at 8:16
  • 1
    I really like the way you added the_content filter. +1 for that. May 4, 2016 at 7:48
  • Works beautiful! Jul 19, 2019 at 21:11
  • What does the str_replace do?
    – netAction
    Apr 30, 2020 at 16:34
  • What is $content = str_replace(']]>', ']]>', $content); for? Oct 22, 2020 at 0:34
9
$post   = get_post( 42 );

$output =  apply_filters( 'the_content', $post->post_content );

echo $output;

from https://developer.wordpress.org/reference/functions/get_post/

1
  • This is the least verbose answer. Love it. Jul 6, 2021 at 2:57
4

you can use this code it is work fine change page_id=19 with your page number:

<?php $the_query = new WP_Query( 'page_id=19' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post();  ?>

                       <?php the_excerpt(); ?>


     <?php endwhile;?>
1
  • 1
    Wellcome to this site. It seems that thisis your first answer. A explanation of why and how your answer solves the problem is always good.
    – cybmeta
    Dec 27, 2014 at 17:07
1

If you're in the loop do this:

<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page

Or if you have an ID, get the post then sue the post_excerpt member var

e.g.

$post = get_post( $post_id );
echo $post->post_excerpt;
1

Try this code and just change your page_id:

<?php $my_query = new WP_Query('page_id=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
 <h3><?php the_title(); ?></h3>
    <div class="text">

        <?php echo wp_trim_words( get_the_content(), 15, '...' ); ?>
 <a href="<?php echo get_page_link(); ?>" class="read-more">Read More</a>
    </div>

 <?php endwhile; ?>
1

For one liner addicts like me. Change 69 by your page ID.

<?= apply_filters('the_content', get_post(69)->post_content); ?>
0

Can do this with a shortcode. add the following to function.php

function lorem_func($attr) {
$txt = "<p>Lorem ipsum dolor .........</p>";
shortcode_atts(
 array(
    'repeat' => 1,
 ), $attr
);
return str_repeat($txt, $attr['repeat'] );
}
add_shortcode('lorem', 'lorem_func');

To render within content use;

[lorem repeat="2"]

Just need to mod the "Lorem ipsum dolor ........." for the length required. And the lorum repeat=2 change to the required amount of paragraphs. In the above example =2 means 2 paragraphs, so its multifuctional.

0

Already answered but i think a much more handier function is get_post_field( 'fieldname', $post_id );

So in your case:

echo get_post_field( 'post_excerpt', $post_id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.