Post_excerpt instead of post_content, out of loop

I am trying to get an excerpt from Wordpress on a page to appear in the stylized area of ​​my footer. Currently, I have the following which gives me the title and all the content:

<?php $page_id = 2; $page_data = get_page( $page_id ); $content = $page_data->post_content; $title = $page_data->post_title; echo '<h3>'. $page_data->post_title .'</h3>'; echo '<p>'. $page_data->post_content .'</p>'; ?> 

I tried various combinations of post_excerpt instead of post_content and tried to simulate and edit the example here: http://codex.wordpress.org/Function_Reference/get_page , but I have no luck. Several times I tried examples from other people, but the content was not at all.

Could this be because I'm trying to get it to display outside the loop, or have I just not hit the desired combination yet?

Thanks.

+4
source share
3 answers

Why don't you just limit the string using substr () ?

 $content = $page_data->post_content; $excerpt = substr($content, 0, 155); 

something like that

at this speed, you don’t need to try to find the true shutter speed variable, and you can control the length, whether it has ellipses at the end, etc.

Also, if you need to find excerpt variables, you can just do var_dump($page_data) and see what returns the required value, no?

EDIT:

you can try adding manual excerpts to pages using this function in functions.php function

 add_action( 'init', 'my_add_excerpts_to_pages' ); function my_add_excerpts_to_pages() { add_post_type_support( 'page', 'excerpt' ); } 

Change to EDIT:

So, I just dug a little deeper, and this little guy here can help you. Add this to your functions.php file ( source )

 add_post_type_support( 'page', 'excerpt' ); 
+8
source

Perhaps try to reset the wordpress request by placing it immediately before the code you sent.

 <?php wp_reset_query(); ?> 
0
source

Use this code for Excerpt ...

 <?php query_posts('cat=ID'.'&showposts=NO. OF POST') ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <p><?php echo substr(get_the_excerpt(), 0,65).' [...]'; ?></p> <a href="<?php the_permalink(); ?>">Read More...</a> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php endif;?> 
0
source

Source: https://habr.com/ru/post/1438541/


All Articles