0

Is it possible to have a custom function to truncate the contents of a defined DIV on a blog post page to use as the summary on the blog index page. So rather than using $the_content or $the_excerpt - Is it possible to create $the_customContent and have some PHP which checks the blog post page and collects the content of the div with class "ThisIsTheContentToUse" - reason for this is that my blog posts have content on the page above the content I want to be included as the blog summary on the blog index page - so either want to tell WP to ignore those blocks of content, or, probably easier - just tell WP where the content to truncate is - e.g. in the "ThisIsTheContentToUse" div... possible? If so... how? Can't seem to find anything online that defines this custom functionality - surely I can't be the first person to want to do this...?

Would apply_filters make this possible? https://developer.wordpress.org/reference/hooks/the_content/

So, The blog post is structured as:

<div class="headerArea">
<h2>The title is here</h2>
<ul>
</div>
<div class="bullets">
<li>Bullet 1</li>
<li>Bullet 2</li>
<li>Bullet 3</li>
</ul>
</div>
<div class="ThisIsTheContentToUse">
<p>The content starts here</p>
</div>

So, currently with the basic get_the_content - the result is:

"The title is here Bullet 1 Bullet 2 Bullet 3 The content starts here"

But what I want is just the content of the "ThisIsTheContentToUse" div - so it would be:

"The content starts here"

2
  • Need to clarify your question. Do you want to show a custom message before content? example - "This is custom message" then show the_content(); Commented Feb 12, 2022 at 12:52
  • @MonzurAlam - no, no need for a custom message - I just want to define what text on the blog post page that WP should display in the the_content field when it reads the page and displays the short summary on the blog index page.
    – Jdubbs
    Commented Feb 12, 2022 at 14:13

1 Answer 1

0

I think the easiest way is using the Excerpt field in the editor or add a custom field to enter your summary and then filter https://developer.wordpress.org/reference/hooks/the_excerpt/ to return the right text.

Because recognizing automatically the right div will force to parse HTML, and it's more complicated.

1
  • I appreciate thats the easy fix - but its not the one I want to go with - I dont want the CMS editor to have to enter content twice.
    – Jdubbs
    Commented Feb 12, 2022 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.