How to customize the post excerpt text with dynamic content?

If you display the latest blog entries on a website, the excerpt text is usually generated from the first words in the overall text or defined by the user in the backend. However, if you use custom fields in the backend and want to design the post preview according to this content, this can be solved very well in combination with the excerpt text.

With the help of the following code, existing blog content as well as user-defined fields can be combined. Superfluous representations must then still be hidden via CSS.

function testexcerpt() {
    $t_field1 = get_post_meta( get_the_ID(), 'FELD1', true );
    $t_field2 = get_post_meta( get_the_ID(), 'FELD2', true );
    $original = get_the_excerpt();
    $titel = get_the_title();
    echo '<div class="new-meta-date">'. $t_field1 .' </div> ';		
    echo '<div class="new-meta-sub">'. $t_field2 .' </div> ';
    echo '<div class="new-meta-titel">'. $titel .' </div> ';	
    echo $original;
add_filter( 'the_excerpt', 'testexcerpt' );
Without cookies
This website does not use cookies or tracking. More information can be found in the privacy policy.