[ISSUE] ACF Blocks - excerpt

N04H

New member
Oct 19, 2020
16
7
3
Polska
Hello,
I have a problem with ACF. I created an ACF block that I complete in the posts. Unfortunately Wordpress and Rank Match not loading the ACF block content for the_excerpt() . Anyone have an idea why? Best regards and thank you for your answer

register block:
PHP:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types() {
    if( function_exists('acf_register_block_type') ) {
        acf_register_block_type(array(
            'name'              => 'przepis',
            'title'             => __('Przepis'),
            'description'       => __('A custom testimonial block.'),
            'render_template'   => 'template-parts/blocks/przepis.php',
            'category'          => 'formatting',
            'icon'              => 'admin-comments',
            'keywords'          => array( 'przepis' )
        ));
    }
}

block file:

PHP:
<?php

/**
* przepis Block Template.
*
* @param   array $block The block settings and attributes.
* @param   string $content The block inner HTML (empty).
* @param   bool $is_preview True during AJAX preview.
* @param   (int|string) $post_id The post ID this block is saved to.
*/

// Create id attribute allowing for custom "anchor" value.
$id = 'przepis-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'przepis';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
}
// Load values and assign defaults.
$opis = get_field('opis') ?: '';
$skladniki = get_field('skladniki') ?: '';
$przygotowanie = get_field('przygotowanie') ?: '';
$porcje = get_field('porcje') ?: '';
?>

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<p itemprop="description"><?php echo $opis; ?></p>
<h2 id="przepis">Przepis na: <?php echo strtolower(get_the_title()); ?></h2>
<ul class="spis-tresci">
<li class="toc-level-1" style="margin-left:20px"><a href="#przepis">Przepis na: <?php echo strtolower(get_the_title()); ?></a></li>
<li class="toc-level-2" style="margin-left:40px"><a href="#skladniki">Składniki <? if($porcje){echo ' (porcje: '.$porcje.')';} ?> </a></li>
<li class="toc-level-2" style="margin-left:40px"><a href="#przygotowanie">Przygotowanie</a></li>
</ul>
<h3 id="skladniki">Składniki <? if($porcje){echo '(porcje: <span itemprop="recipeYield"> '.$porcje.'</span>)';} ?></h3>
<ul><?php foreach ($skladniki as $skladnik) { echo '<li itemprop="recipeIngredient">'.$skladnik['skladnik'].'</li>'; } ?></ul>
<h3 id="przygotowanie">Przygotowanie</h3>
<ul><?php foreach ($przygotowanie as $krok) {echo '<li itemprop="recipeInstructions">'.$krok['krok'].'</li>';} ?></ul>
</div>

HTML code in post
HTML:
<div id="przepis-block_5ff5768de2329" class="przepis">
<p itemprop="description">Goście za rogiem, a Ty nie masz nawet ciasta? Spieszymy z pomocą! Poznaj nasz <a class="wpil_keyword_link" href="/przepisy/" title="przepis" data-wpil-keyword-link="linked">przepis</a> na błyskawiczną babkę z ajerkoniakiem! Goście będą zachwyceni!</p>
<h2 id="przepis">Przepis na: babka z ajerkoniakiem</h2>
<ul class="spis-tresci">
<li class="toc-level-1" style="margin-left:20px"><a href="#przepis">Przepis na: babka z ajerkoniakiem</a></li>
<li class="toc-level-2" style="margin-left:40px"><a href="#skladniki">Składniki  </a></li>
<li class="toc-level-2" style="margin-left:40px"><a href="#przygotowanie">Przygotowanie</a></li>
</ul>
<h3 id="skladniki">Składniki </h3>
<ul><li itemprop="recipeIngredient">4 jajka</li><li itemprop="recipeIngredient"> 1 łyżka cukru waniliowego </li><li itemprop="recipeIngredient">200g cukru</li><li itemprop="recipeIngredient">200ml oleju słonecznikowego </li><li itemprop="recipeIngredient">200ml ajerkoniaku</li><li itemprop="recipeIngredient">proszek do pieczenia</li><li itemprop="recipeIngredient">120g mąki pszennej</li><li itemprop="recipeIngredient">120g mąki ziemniaczanej</li></ul>
<h3 id="przygotowanie">Przygotowanie</h3>
<ul><li itemprop="recipeInstructions">W misce ubijamy jajka oraz cukier. </li><li itemprop="recipeInstructions">W mniejszej miseczce łączymy mąkę ziemniaczaną i pszenną oraz proszek do pieczenia.</li><li itemprop="recipeInstructions">Do ubijającej się masy wlewamy olej oraz ajerkoniak.</li><li itemprop="recipeInstructions">Zmniejszamy obroty miksera i dodajemy mąkę.</li><li itemprop="recipeInstructions">Gotową masę przelewamy do formy. </li><li itemprop="recipeInstructions">Pieczemy w piekarniku nagrzanym do 180°C przez godzinę.</li></ul>
</div>


the_excerpt(); = NULL :(
 
Last edited:

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
Of course is the_excerpt empty. Since you seem to know a bit about coding, I suppose you know what the_excerpt is: a separate entry in the database for each post if it has any text. And you do that manually on each post.

Why do you think your ACF block would fill the_excerpt?
 
  • Like
Reactions: N04H

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
If you want to auto generate an excerpt from that ACF block, here's the solution:
 
  • Like
Reactions: N04H

N04H

New member
Oct 19, 2020
16
7
3
Polska
Of course is the_excerpt empty. Since you seem to know a bit about coding, I suppose you know what the_excerpt is: a separate entry in the database for each post if it has any text. And you do that manually on each post.

Why do you think your ACF block would fill the_excerpt?


Yes, I know, but in other posts wordpress automatically creates the_excerpt from the content, in this case it does not download the content of blocks from acf
 

N04H

New member
Oct 19, 2020
16
7
3
Polska
A solution that I'm not proud of, but maybe someone will find it useful


function.php
PHP:
function awp_get_excerpt($post=false) {
    if (!$post) {
        global $post;
    }
    if (!$post) { return ''; }
    $excerpt = '';
    $blocks = parse_blocks($post->post_content);
    if (count($blocks) == 1 && $blocks[0]['blockName'] == null) {  // Non-Gutenberg posts
        $excerpt = get_the_excerpt($post->ID);
    } else {
        foreach ($blocks as $block) {
            if ($block['blockName'] == 'acf/przepis') {
                $excerpt = $block['attrs']['data']['opis'];
                $excerpt = substr( $excerpt, 0, 260);
                $result = substr( $excerpt, 0, strrpos( $excerpt, ' ') );
                break;
            }
        }
    }
    return ( $result ? strip_tags( $result ).'...' : '' );
}

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
    if ('przepisy' == get_post_type() ){
    return awp_get_excerpt($post);
}
return $excerpt;
}, 10, 2 );

add_action( 'rank_math/frontend/description', function( $description ) {
    global $post;
    $desc = RankMath\Post::get_meta( 'description', $post->ID );
 
    if (is_single() && 'przepisy' == get_post_type() ){
        $description = $desc." ".awp_get_excerpt($post);
    }
    return $description;
});
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu