I have a custom function to trim the excerpt:
function custom_excerpt_length( $length ) {
return 10; }
and the filter to apply it:
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
and then I get the excerpt in my content.php as:
<?php the_excerpt(); ?>
The function works if not custom user entered excerpt is found. so basically grabs the first 10 words from the blog post content. But it doesn't trim when I custom enter the excerpt. Any ideas to display the trimmed version of the user entered excerpt in the blog post page?
I finally got what I needed. So I just wanted to reduce the trimmed excerpt to a lower character count. I just added to my functions page:
function custom_excerpt_more( $more ) {
$more = '…';
return $more;
}
add_filter( 'excerpt_more', 'custom_excerpt_more' ); //displays ... after the excerpt
function custom_excerpt_length( $length ) {
return 30; //number of words to display in the excerpt
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); //999 si it filters at the end
And then in my content.php page:
<p><?php echo substr(get_the_excerpt(),0,80);?></p>
That way I get no more than 80 characters in my blog post list page and 30 words on my individual blog post page (content-single.php).
Related
Right so this seems to have been asked a few times but I cannot seem to find definitive working answer.
Currently I am trying to limit my post title and post excerpt on my WP website.
I have managed to trim the titles by character using
function custom_trim_my_title( $title ) {
if ( strlen( $title ) >= 72 && ! is_singular() ) {
$title = substr( $title, 0, 72 ) . '...';
return $title;
}
return $title;
}
add_filter( 'the_title', 'custom_trim_my_title' );
And that seems to be working okay but I can't seem to get it to work for the excerpt.
I was using this word count:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
But the longer words still made it look messy so I need a character count and have tried this:
add_filter('the_excerpt','excerpt_char_limit');
function excerpt_char_limit($e){
return substr($e,0,50);
}
But it doesn't seem to work.
Any help would be great.
Try using
add_filter('the_excerpt','excerpt_char_limit',999);
instead. It is just in case if some other filter prevents your filter. (999 means executing later than other filters)
I am trying to use the following shortcode in the wordpress post title. The shortcode looks like the following:
//Use [year] in your posts.
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Any suggestions how to execute this shortcode in the post title?
I appreciate your replies!
You can absolutely use a shortcode in a title. You just need to use the WordPress hooks system to run the shortcode when the title is called. So if you want to have a shortcode [year] that spits out the current year, you'll create the shortcode:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
Then, hook into the filter for the_title() to run your shortcode:
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
That takes care of the Post/Page title, but you'll also want to run it for the single_post_title hook which is used in wp_head on the title tag on your site. That way, the browser will show the proper title as well:
add_filter( 'single_post_title', 'my_shortcode_title' );
Note: You don't need a separate function here because it's running the exact same code. So your total code would look something like this:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
I don't think you can apply (save in admin post edit screen) a shortcode in the post title with a shortcode. The post_title is sanitize to avoid tag or shortcode, the post title is used by many function that a shortcode can break.
To make modification on the post_title, you can use the filter the_title
add_filter('the _title', 'yourfunction');
function yourfunction($title){
global $post;
// if you set a custom field on the post where you want to display the year
if(get_post_meta($post->ID, 'display_year', true) == 1){
$title = $title. ' '. date('Y');
}
return $title;
}
Hope it helps
Please add this code in 'function.php'. Try this.
<?php
function TitleFunction($title)
{
global $post;
$title = $title. ' ' .get_the_date('Y');
return $title;
}
add_filter( 'the_title', 'TitleFunction', 10, 2 );
?>
I am just working on filter titles in pages and posts and everything works as expected but when I view the single.php whereas it shows the actual post as should be, the function hook filter i am working on inside functions.php of my plugin is filtering every title of each post of my blog, I was expecting to filter just the title of the current post:
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
/**/
}
}
any help is grateful.
Thanks ;)
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((some condition true)){
$add_this = 'some string';
$new_title = $title.' '.$add_this;
return $new_title;
}
return $title;
}
when you view any post it is shown through single.php so every post becomes current post when you view it
I understand now that for the filter title hook it is the same viewing a single post from single.php than viewing the whole blog. would there be a way of filtering with a condition to only retrieve the title of the current post in single.php?, I did try "get_the_title(get_the_ID())" like this but obviously I cant nest it.
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
if(is_single()){
$title=get_the_title(get_the_ID());
}
}
}
Just a quicky here, I've created a function to limit the excerpt length for a specific post-type only (as I am having the excerpt for that specific post type show in a fading slider only) using the following function:
function client_excerpt($length) {
global $post;
if ($post->post_type == 'testimonial')
return 20;
else
return 55;
}
add_filter('excerpt_length', 'client_excerpt');
Now, that works just fine when I call get_the_excerpt within the loop outputting my div's for the slider. However, I don't want the "Read More..." link to appear on those excerpt's only. Can I stop them showing on those specific excerpts within my function?
Try this, using the excerpt_more filter:
function new_excerpt_more( $more ) {
global $post;
if ($post->post_type == 'testimonial'){
return '';
}
}
add_filter('excerpt_more', 'new_excerpt_more');
How do I modify a page title for specific pages in shortcode?
The following will change the title but it executes for every page. I need more control over where it executes.
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
Is there a way to call the above in a shortcode function? I know how to use do_shortcode() but the above is a filter.
My goal is to modify the page title based on a URL parameter. This only happens for specific pages.
Although WordPress shortcodes was not designed to do this, it can be done. The problem is shortcodes are processed AFTER the head section is sent so the solution is to process the shortcode BEFORE the head section is sent.
add_filter( 'pre_get_document_title', function( $title ) {
global $post;
if ( ! $post || ! $post->post_content ) {
return $title;
}
if ( preg_match( '#\[mc_set_title.*\]#', $post->post_content, $matches ) !== 1 ) {
return '';
}
return do_shortcode( $matches[0] );
} );
add_shortcode( 'mc_set_title', function( $atts ) {
if ( ! doing_filter( 'pre_get_document_title' ) ) {
# just remove the shortcode from post content in normal shortcode processing
return '';
}
# in filter 'pre_get_document_title' - you can use $atts and global $post to compute the title
return 'MC TITLE';
} );
The critical point is when the filter 'pre_get_document_title' is done the global $post object is set and $post->post_content is available. So, you can find the shortcodes for this post at this time.
When the shortcode is normally called it replaces itself with the empty string so it has no effect on the post_content. However, when called from the filter 'pre_get_document_title' it can compute the title from its arguments $atts and the global $post.
Taken from the WordPress Codex
Introduced in WordPress 2.5 is the Shortcode API, a simple set of
functions for creating macro codes for use in post content.
This would suggest that you can't control page titles using shortcodes as the shortcode is run inside the post content at which point the title tag has already been rendered and is then too late.
What is it exactly that you want to do? Using the Yoast SEO Plugin you can set Post and Page titles within each post if this is what you want to do?
You could create your custom plugin based on your URL parameters as so:
function assignPageTitle(){
if( $_GET['query'] == 'something' ) { return 'something'; }
elseif( $_GET['query'] == 'something-else' ) { return 'something-else'; }
else { return "Default Title"; }
}
add_filter('wp_title', 'assignPageTitle');