Skip Navigation

[Resolved] If more less than a number of characters add link to Post

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Minesh 1 year, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2642081

Dear Toolset-Team,

My Loop Item in a view is coded like this:

[wpv-post-date format="d.m.Y"]<br>
<h3 class="modifiedtitel">[types field="karriere-jobtitel"][/types]</h3>
[trimfield length="30" more=" Read more..." field="karriere-jobdetails" postid="[wpv-post-id]"][/trimfield]

However, if I enter less than 30 characters, logically no link to the post appears.

Can you give me a code that I can put underneath so that if there are fewer than 30 characters, a link to the post will appear?

Thank you very much in advance for your help.

Best regards
Alisa Andris

#2642111

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will require to know what is the shortcode code added for the shortcode [trimfield].

Can you please share problem URL where you added the [trimfield] shortcode as well as to what number you want to restrict the content with admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2645563

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where you added the above shortcode and where I can check the results on the frontend.

I have set the next reply to private which means only you and I have access to it.

#2647861

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

With your current theme's functions.php I see the following code added:

function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 30, // Number of words to show
        'more'      => 'Weiterlesen...',  // Text to show at the end of the filtered text
        'field'     => '',
        'postid'    => '0'
    ), $atts ) );
    global $post;
    if ( empty($field) ){
        return;
    }
    if ( !isset($post) ){
        return;
    }
    $field = types_render_field($field, array('raw'=>'true'));
 
    $excerpt_more_link = '&nbsp;&nbsp;<a href="' . get_the_permalink($postid) . '">' . $more . '</a>';
    $output = wp_trim_words($field, $length, $excerpt_more_link );  // create the filter text
    return $output;  // return filter text
 
    //example: [trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]
}
add_shortcode( 'trimfield', 'custom_trim_field' );

I've changed the following line from above code:

  'length'    => 30, // Number of words to show

To

  'length'    => 5, // Number of words to show

That means you will have to add at least 5 words to display the read more link.

Can you please confirm it works as expected now when you pass length="10" or length="15" to your shortcode.

#2652435

Hey,

i want still that 30 words are shown. But if there are less than 30 Word are shown then there is no "Read more" Link to the Post. Can you check this again?

#2652451

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added to functions.php file as given under:

/*------Custom Post Type - show an excerpt of a WYSIWYG custom field*/
function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 5, // Number of words to show
        'more'      => 'Weiterlesen...',  // Text to show at the end of the filtered text
        'field'     => '',
        'postid'    => '0'
    ), $atts ) );
    global $post;
    if ( empty($field) ){
        return;
    }
    if ( !isset($post) ){
        return;
    }
    $field = types_render_field($field, array('raw'=>'true'));
    if(str_word_count($field) > $atts['length']   ) {
    	$excerpt_more_link = '&nbsp;&nbsp;<a href="' . get_the_permalink($postid) . '">' . $more . '</a>';
   		$output = wp_trim_words($field, $length, $excerpt_more_link );  // create the filter text
	}else{
		$output=$field;
	}
    return $output;  // return filter text
 
    //example: [trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]
}
add_shortcode( 'trimfield', 'custom_trim_field' );
#2656893
Problem.jpg

Dear Minesh,

as if you see in the uploaded image: The Links in the trimmed text are not klickable. Thats the reason I neet a Link to the Post even if there are just a few words. When you are at the Post itself, you can click at the Links.
But still no Link to the Post is appearing if there are less than 30 words. Do you understand what I mean? Can you help me?

Best regards
Alisa

#2657229

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see for the first view item the field value has the anchor tags - the actual field value stored in database as given under:

Urlaubsvertretung von <a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">www.Chirurgie-Arnold.de</a>

<a href="mailto:handchirurgin@chirurgie-arnold.de">handchirurgin@chirurgie-arnold.de</a>

<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">Stellenanzeige öffnen</a>

We can not calculate the number of words with such string. Is that Ok to remove the anchor tag or you can add only string to that first item and then let me know I will try to adjust the code if required accordingly.

#2658377

Hey Minesh,

the only thing I would like added is a link to the post page. The links on the post page are clickable. However, if there are too few words in the post, the "Find out more" link won't work and you won't have a chance to click on the link. For simplicity's sake, we can also remove the "Learn more" link and add a link to the post on all listed entries in the view. Maybe that's easier?
How do I create a link to the post itself?

I thank you in advance.

#2658397

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To your view:
- hidden link

I've added the link as given under to your "karriere-jobtitel" field:

<h3 class="modifiedtitel"><a href="[wpv-post-url]">[types field="karriere-jobtitel"][/types]</a></h3>

You can remove the "Find out more" link if required.