Skip Navigation

[Resolved] Loop title issue

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the customer wanted to trim the text that the [wpv-post-title] shortcode provides.

Solution:

In order to do this it will require some custom shortcode.

// Add Shortcode
function trim_link( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'string' => '',
            'length' => '',
            'url' => '',
        ),
        $atts
    );
 
    if (strlen($atts['string']) > $atts['length']){
        $shortened_text = substr($att['string'], 0, $atts['length']);
        return "<a href='".$atts['url']."'>".$shortened_text."...</a>";
    }else{
        return "<a href='".$atts['url']."'>".$atts['string']."</a>";
    }
     
 
}
add_shortcode( 'trim_link', 'trim_link' );

Add it to your functions.php file and then you can add it to your view by doing this.

[trim_link string='[wpv-post-title]' length='140' url='[wpv-post-url]'] 

Please try this and let me know if it helps.
Thanks,

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 44 replies, has 3 voices.

Last updated by Anonymous 6 years ago.

Assisted by: Shane.

Author
Posts
#628680

Anonymous
Screenshot of Notizie - Gruppo Scout San Damiano 1 (1).jpg
Screenshot of Notizie - Gruppo Scout San Damiano 1.jpg

Hi, I created a custom view with Toolset to show the loop of my site's posts.

Page: hidden link
View: hidden link

As you can see, there is a problem when the title of the post is too long so that the content of the card goes out of the box.

I only succeed in cropping the length of the body

[wpv-post-excerpt length="95" more="..." format="noautop"]

But I can't do the same with the post link title

[wpv-post-link]

I made a screenshot of what I would like to have (2 lines max title length), I really need a solution or workoround.

#628824

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

Thank you for contacting our support forum.

Can you try this custom shortcode below.

// Add Shortcode
function trim_link( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'string' => '',
			'length' => '',
			'url' => '',
		),
		$atts
	);

	if (strlen($atts['string']) > $atts['length']){
	    $shortened_text = substr($att['string'], 0, $atts['length']);
	    return "<a href=".$atts['url'].">".$shortened_text."...</a>";
	}else{
	    return "<a href=".$atts['url'].">".$atts['string']."</a>";
	}
	

}
add_shortcode( 'trim_link', 'trim_link' );

Add it to your functions.php file and then you can add it to your view by doing this.

[trim_link string='[wpv-post-title]' length='140' url='[wpv-post-url]'] 

Please try this and let me know if it helps.
Thanks,
Shane

#628875

Anonymous
Screenshot of Notizie - Gruppo Scout San Damiano 1.jpg

I added the code to the function.php of the child-theme

I added [trim_link string='[wpv-post-title]' length='140' url='[wpv-post-url]'] after the default previous [wpv-post-link]

There is an error: I set the lenght to 15 chars, but the string is not trimmed, instead, if the title is longer then 15 chars, it is not shown at all and it is just shown "..."

#628905

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

Would you mind providing me with access to the website so that I can check on this for you further?

The private fields will be enabled for your next response.

Thanks,
Shane

#628912
#628963

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

I've fixed the code and it should work fine now.

Thanks,
Shane

#628965

Anonymous
download.jpg

Sorry but there is still a problem:

the shortcode is set to trim titles longer than 15 chars, but the count if ofter wrong (8 chars instead of 15 chars)

#628970

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

It is actually Correct.

Apparently this is retuning it as 15 string. I checked this and it seems the " is throwing off the count. If this is removed then it works fine.

Thanks,
Shane

#628971

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

What is happening is that the apostrophes are being converted to this & a p o s; hence the count thrown off

#629034

Anonymous
download (2).jpg
download (1).jpg

1) I extended length to 40 chars because I like it more

2) It is ok for me if the trim function removes " ", I can add it later this way ( outside [wpv-post-title] )
[trim_link string=' "[wpv-post-title]" ' length='40' url='[wpv-post-url]']

There are some problems though:

3) if the string is trimmed, and ... are added at the end, the closing " is not added. Why?

4) in the fourth post, there are 2 " at the beginning. The first is not trimmed in the function

5) if you go to page 2, the titles are not clickable. Why?

#629394

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

The AJAX reload is whats causing the code to break. From what I can see the anchor tag is still there but for some reason the text is being rendered outside the tag which should be impossible in my opinion.

I'm still looking into this.

Thanks,
Shane

#629524

Anonymous

Ok, I'll still wait for a solution because I need to use the shortcode in many others view but I will use it only when it will work correctly everywhere.

Many thanks again

#630063

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Simone

Shane is away today and Monday for Easter. As he has provided custom code and is working on improving it, I'll leave it to him to continue with this, I just wanted you to know he won't be able to reply before Tuesday.

#631054

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simone,

I'm still looking into this for you but right now this works fine without the AJAX pagination.

Could you try this.

Thanks,
Shane

#631067

Anonymous

I need to show only few posts at time, so pagination is the only solution and I need a fix.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.