Try this one
/* Change Excerpt length */
function excerpt($num, $post_id) { // removed empty default value
$limit = $num+1;
$excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
$excerpt = explode(' ', $excerpt, $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."…";
echo $excerpt;
}
Another solution – using setup_postdata($post);
and wp_reset_postdata();
function custom_get_the_excerpt($post_id) {
global $post;
$save_post = $post;
$post = get_post($post_id);
setup_postdata($post);
$output = get_the_excerpt($post);
wp_reset_postdata();
$post = $save_post;
return $output;
}
Related Posts:
- Word Count Function Preventing Permalink Editing
- Custom excerpt function re-factoring
- How to remove the excerpt in the Dzonia Lite theme
- How to put “Read more” link in Custom Excerpt inside p tag?
- Error when adding excerpt to the content through functions.php
- Query Pages and post excerpts dynamically
- How to remove the space before the ellipsis in excerpt?
- How to trim content AND retain HTML?
- Merging two excerpt functions to work with conditions
- Mega Menu Walker
- How to add a class to the attachment images
- How to call a function only once (global variable scope)
- Multiple is_page() in page.php
- Fatal error: Call to undefined function plugin_dir_path()
- Switching wp_get_sites to get_sites
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- How to correct schedule my event weekly with wp_schedule_event()
- One button to change all settings in theme customizer?
- Add custom image sizes to media uploader
- Execute a shortcode when clicking on a image
- Limit the_excerpt with max of x characters
- How can update custom meta for all posts
- Automatic Excerpt Not Working
- Is $hook a global variable in WordPress
- Change logo based on incoming domain
- wrong php date()?
- Include a php file from functions.php only to homepage
- How do I fetch all comments per post via WP REST API?
- Shortcode content output but not in correct place
- How to restrict page access to logged-in users?
- How to Display a Single Post Excerpt
- How do I display a user’s previous orders as a select box option on a product?
- Handling Body class based on Template
- Remove meta description on certain pages
- Image media upload metabox
- Mass update excerpt
- How to combine wordpress_logged_in cookies in one cookie?
- Two Different Links for Same Product – WooCommerce [closed]
- Automatically adding new post categories to menu
- Get post id in a function when edit/add a post
- esc_html_e() is not translating string in wordpress [closed]
- get author_name from queried post
- Is it possible to intercept all ajax requests and get the parameters and the returns?
- Pass php dynamic variable to shortcode
- Limit 1 global comment per minute
- Modify custom field input data before creating a new post
- Is there a way to conditionally check whether a WordPress post title is empty?
- Error call_user_func_array() expects parameter 1 to be a valid callback when using image_size_names_choose
- Find hours between post_date and post_date_gmt
- Run a code only on theme activation only during first activation
- Shortcode from a function not working
- Is it possible to return content, and then also continue to do other things?
- How to remove ?p= from wordpress short links
- Creating WordPress Shortcode with Variable
- Add meta tags to a custom header
- Need php code’s output to display underneath a Divi Module
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- How to make function appear in sentence?
- How to define a variable already defined in a plugin function?
- How to use mysql LIKE with wpdb?
- woocommerce related product title length
- Custom query vars filters problem with pagination
- Function sanitize_title() does not appear to be working
- Shortcodes showing in excerpt despite using strip_shortcodes
- Query for user roles
- Use SCSSPHP to compile Theme Customizer Values into .SCSS files ready to compile to CSS
- Displaying Event within two given time frames
- How to prevent a function from running based on host (ie web vs local)?
- How to call custom function outside from the admin page?
- Is there a way to create a “copy post” link?
- Use of antispambot with $curauth->email
- Delete images from media library when user deletes an image from ACF Gallery
- Reject all malicious URL requests functions.php
- posts_per_page displays only 2 posts instead of 4 posts
- Notice: Undefined variable
- How to override WooCommerce Order Item Meta with data from Cart?
- Taxonomy Child Term, Counter is staying on 0
- Access to “My Site” is missing from the admin bar
- How to redirect (301) trashed post to it’s parent taxonomy TERM ARCHIVE instead of 404 page
- How to call WordPress PHP file using external js file
- Need help with PHP functions
- Using get_theme_mod with checkbox to display content
- use system script advertising in wordpress multisite
- WordPress styling recent post
- Styling admin page rows in order of importance (checkboxes)
- Insert wordpress tags below posts via functions.php
- Break a WordPress function to run in patches and re-continue
- Pagenav Not appearing on custom Template
- get_post_meta no longer working
- Highlight posts currently being edited on multiple editor site?
- My title is showing after the shortcode
- Load a Header in wordpress
- Trouble figuring out how to get my button to submit comment
- Adding multiple conditional tags in a function?
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- How do I change the Go To Categories link in the term_updated_messages
- WooCommerce – Show ‘Sales’ badge on variable products where only some variations have sales price [closed]
- generate excerpt from the part of big text dump that contains keyword
- Custom Admin Menu Report for Specific User ID
- how to create twitter card without plugin in wordpress website?