Skip Navigation

[Resolved] Trying to create View that uses WordPress’ more marker to truncate content

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

Problem:

Want a read more link when the "more" tag has been used. So the condition is "If there's a more tag, output a read more link; if there's no more tag, don't output a read more link".

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/trying-to-create-view-that-uses-wordpress-more-marker-to-truncate-content/#post-2178253

Relevant Documentation:

This support ticket is created 3 years, 4 months 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by davidL-7 3 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#2175579
Edit Post ‹ Sierra Health Foundation — WordPress 2021-09-21.png

Tell us what you are trying to do?

Hi, I'm trying to create a view that displays a post's content (not the excerpt, because we need HTML formatting to work), but that truncates when it hits the more marker than one can drop in when authoring a WordPress post (see attached).

I've tried dropping in the wpv-post-body shortcode, but it doesn't seem to respect the more marker.

If your not familiar with the more marker, it's been part of WordPress for a very long time. When displaying a post on the archive page, the options are always to either display the excerpt or to display the content; if one chooses the content, one can add the more marker and the result will be that on the archive page, the content only displays up to the more maker, and then a "read more" link gets rendered.

What is the link to your site?
hidden link

See the post with headline "Sergio Cuellar transitions to San Joaquin Valley Health Fund". On the page above, we want what's displayed to end after the more marker placed after the end of the second sentence (see attached screenshot of the edit post screen.

To reiterate, the reason we can't use the excerpt is because excerpts don't support HTML, and on hidden link you'll see many items that use a link in the content.

Thanks in advance for you support,
David

#2175899

Hello,

You are right, the wpv-post-body shortcode doesn't seem to respect the more marker.

But you can try the custom shortcode workaround of below similar thread:
https://toolset.com/forums/topic/block-editor-doesnt-respect-read-more/#post-1504955

#2176569

Hi Luo,

Thanks for the reply. The thread you directed me to was addressing this exact issue, so I implemented the workaround using the option to integrated it using Toolset's custom code interface. Everything works great, but I need to make one tweak and am not sure how to do it:

I only need a "Read more" link to display if this function gets called. At first I dropped the [wpv-post-read-more] shortcode into the loop editor after the [get_content_before_more] shortcode, but this had the unwanted effect of including a Read more link on every post. The client usually writes very short posts and doesn't use the

more

divider, but on the odd occasion when they do, they want a Read more link.

So I'm thinking that the function needs to be modified. Here's what we've got currently:

[php]
// get current post's content
$content = get_post_field( 'post_content', get_the_ID() );

// Get content parts
$content_parts = get_extended( $content );

// Return only the content before the read more tag
return $content_parts['main'];
[php]

I'm not quite good enough with the post object to figure out how to pull the URL to the individual post so that I can return a read more link after the main part of the content. Can you point me in the right direction?

Thanks!
David

#2176803

For the new questions:

The client usually writes very short posts and doesn't use the "more" divider, but on the odd occasion when they do, they want a Read more link.

You don't need to modify the function, if there isn't the "more" tag, shortcode [get_content_before_more] should output all post content

You can use shortcode [wpv-post-read-more] shortcode to output a link to read more content of the post, for example:
[wpv-post-read-more label="Continue reading"]

See our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-read-more

#2177737

HI Luo,

I wasn't clear about what our challenge is. Let me restate it:

You don't need to modify the function, if there isn't the "more" tag, shortcode [get_content_before_more] should output all post content

Yes, I understand that, and the [get_content_before_more] outputs exactly what we want it to.

You can use shortcode [wpv-post-read-more] shortcode to output a link to read more content of the post, for example:
[wpv-post-read-more label="Continue reading"]

That's where our problem is. If I use the [wpv-post-read-more] shortcode in the view, I get the Read more link after every post. I need it to be conditional.

In only want a read more link when the "more" tag has been used. So the condition is "If there's a more tag, output a read more link; if there's no more tag, don't output a read more link".

I'm thinking that that condition has to be processed in the function; I've tried to modify the function, but haven't had any success so far. Any thoughts?

Thanks,
David

#2178253

In your case, you can try to modify the PHP codes as below:

add_shortcode('get_content_before_more', 'get_content_before_more_func');
function get_content_before_more_func() {
    // get current post's content
    $content = get_post_field( 'post_content', get_the_ID() );
 
    // Get content parts
	$res = '';
    $content_parts = get_extended( $content );
	$res = $content_parts['main'];
	if(!empty($content_parts["extended"])){
		$res .= do_shortcode('[wpv-post-read-more label="Continue reading"]');
	}

    return $res;
}

it will display [wpv-post-read-more] shortcod too.

#2181289

Hi Luo,

That did the trick, thank you so much! Just to help out anyone else who might need this, I made the following modification to make the "Continue Reading" link more accessible by adding a title attribute with the post title in the link:

add_shortcode('get_content_before_more', 'get_content_before_more_func');
function get_content_before_more_func() {
    // get current post's content
    $content = get_post_field( 'post_content', get_the_ID() );
  
    // Get content parts
    $res = '';
    $content_parts = get_extended( $content );
    $res = $content_parts['main'];
    if(!empty($content_parts["extended"])){
        // $res .= do_shortcode('[wpv-post-read-more label="Continue reading"]');
      $posturl = do_shortcode('[wpv-post-url]');
      $continuelink = do_shortcode('[wpv-post-title output="sanitize"]');
      $res .= '<a href="' . $posturl . '" title="Continue reading ' . $continuelink . '">Continue reading...</a>';
    }
 
    return $res;
}

Thanks again!
David