Only show read more text when when wp:post-excerpt meets excerptLength

To override the behavior of the render_block_core_post_excerpt function in a Block Theme, especially to customize the “Read More” link behavior based on the excerptLength, you’ll need to unregister the original block and register your own customized version of it. This process involves a few steps:

Deregister the Original Block: You need to deregister the original post-excerpt block.

Register a Custom Block: Create and register a custom block that mimics the original block but includes your custom logic.

Override the Render Function: Implement your custom logic in the render callback for your new block.

Here’s a basic outline of how you might approach this:

Step 1: Deregister the Original Block

You should deregister the original block. This is typically done on the init hook.

function wpb_custom_init() {
    unregister_block_type('core/post-excerpt');
}
add_action('init', 'wpb_custom_init');

Step 2: Register a Custom Block

Next, you’ll need to register your custom block. You can mostly reuse the settings from the original block, but you’ll replace the render callback with your own function.

function wpb_register_custom_post_excerpt_block() {
    register_block_type('core/post-excerpt', [
        // Copy the settings from the original block, but override the 'render_callback'
        'render_callback' => 'wpb_render_custom_post_excerpt',
    ]);
}
add_action('init', 'wpb_register_custom_post_excerpt_block');

Step 3: Implement the Custom Render Function

Now, implement your custom render function. This is where you’ll add your custom logic to handle the “Read More” text based on the excerptLength.

function wpb_render_custom_post_excerpt( $attributes, $content ) {
    // Your custom logic here. You can refer to the original function's source code and modify as needed.

    // Example:
    $excerpt_length = isset($attributes['excerptLength']) ? $attributes['excerptLength'] : 55;
    $more_text = isset($attributes['moreText']) ? $attributes['moreText'] : 'Read More';

    $excerpt = get_the_excerpt();

    if (strlen($excerpt) > $excerpt_length && !empty($more_text)) {
        // Add your custom "Read More" logic here
    }

    // Return the modified excerpt
    return $excerpt;
}

In this function, you will apply your own logic to determine whether to show the “Read More” text based on the excerptLength. You can refer to the source code of the original render_block_core_post_excerpt function for guidance and modify it according to your requirements.

Remember, this approach requires a good understanding of how block types are registered and rendered in WordPress, as well as familiarity with PHP and the WordPress block editor’s internals. Testing in a development environment first is highly recommended to ensure everything works as expected.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)