I am trying to: I have created a posts list and I'm trying to display the Related Post Title but with a max length.
I have tried to insert it into the View but it don't work.
[wpv-post-link id='[wpv-post-id]' length="16" more="..."]
Link to a page where the issue can be seen: hidden link
I expected to see: the location name limited ad 16 characters plus the more "..."
Ex: The "San Valentino In Abruzzo Citeriore" I need to be "San Valentino In ..."
Instead, I got: The normal length.
So,
in custom code section, I have created this shortcode
add_shortcode( 'title', 'title_trim_words' );
function title_trim_words( $title )
{
// limit to ten words
return wp_trim_words( $title, 16, '...' );
}
And I have added it, [title] into the View, but it don't return nothing...
I have created a posts list and I'm trying to display the Related Post Title but with a max length.
Hi, the wpv-post-link shortcode is used to display a link, not just the post title. If you just want to display the title, you can use the wpv-post-title shortcode. However, neither wpv-post-link nor wpv-post-title supports the "length" attribute. If you want to know which attributes are supported by our shortcodes, you can refer to the documentation here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-title
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-link
If you want to customize the length of the title, you need a custom shortcode. If you want to display that trimmed title in a link, you need more custom code that creates a link tag using the wpv-post-url shortcode, and inserts the trimmed title shortcode in the link text.
And I have added it, [title] into the View, but it don't return nothing...
Please add a die() statement to your code to confirm it is being executed:
add_shortcode( 'title', 'title_trim_words' );
function title_trim_words( $title )
{
die( 'your code is executed with ' . $title . ' as the title');
// limit to ten words
return wp_trim_words( $title, 16, '...' );
}
If you do not see the words "your code is executed with...as the title" appear on the page, then check to be sure your snippet is Activated. My guess is that your $title variable is empty, because you don't have any shortcode attributes. Please consult the Shortcode API documentation in the WordPress Codex: https://codex.wordpress.org/Shortcode_API
Check the section "Enclosing vs self-closing shortcodes" to see examples accessing shortcode attributes and wrapped contents.
My issue is resolved now. Thank you!
I hava found this solution that work fine:
add_shortcode( 'title', 'title_trim_words' );
function title_trim_words( $title )
{
$title= substr(get_the_title(), 0, 17 );
$length=strlen(get_the_title());
if ($length > 17) {$more="…";}
// limit to ten words
return $title . $more ;
}