V
V
Vitaly2021-09-28 14:33:02
WordPress
Vitaly, 2021-09-28 14:33:02

How to set excerpt output (the_excerpt) in Wordpress regardless of field completion?

Given: CPT, the data in which is filled through ACF. That is, in the usual sense, the record has neither a quote nor content.
Required: Display a list of posts (archive) using Elementor.

Problem: Some Elementor widgets that display a list of posts rely on standard fields.

The_excerpt() and get_the_excerpt() functions output an excerpt according to the rule: if something is filled in the excerpt field, then this is the content; if not filled, then the content of the post; if there is no content, then nothing is displayed. The same functions have hooks - that's great! That is, I replace the content of the quote with my content from ACF without any problems. Except these hooks only fire when has_excerpt() returns true; that is, they give me the opportunity to OVERRIDE the quote being output. But I need them to work every time the quote is requested, and regardless of the presence of a quote or the content of the post (which, we remember, I don’t have) - they still called the function defined in my add_filter and received / displayed my content.

How to do it, how to cheat the system?
The frontal solution, of course, is to intercept the hook when creating a post and force writing something like 'xyz' in the quote field. The user will not see it anyway, but the system will see the completed quote. But this is a kind of brute force)) I want to be more elegant and without rewriting the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-09-28
@artzolin

Why don't you write your own function and use it in the theme? So it will be easier to collect ACF fields

function custom_excerpt() {

  $html = '';

  if ( has_excerpt() ) {
    $html = get_the_excerpt();
  } else {
    // если нет контента, то собираем $html из полей ACF
  }

  return $html;

}

Usage in the theme:
<?php echo custom_excerpt(); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question