• Hi there,

    I am trying to exclude certain areas of pages from the custom extracts Relevanssi generates. I found the filter hook relevanssi_excerpt_content in the user manual which should be appropriate. I have included the provided snippet in my functions.php:

    add_filter( 'relevanssi_excerpt_content', 'rlv_remove_noindex_class' );
    function rlv_remove_noindex_class( $content ) {
        return preg_replace( '#<(.*) class=".*?relevanssi_noindex".*?</\1>#ms', '', $content );
    }

    On one page I used the following content for testing:

    <h1 class="relevanssi_noindex">Holzbau (Don't show this in excerpt)</h1>

    However, the text Holzbau (Don’t show this in excerpt) still appears in the Relevanssi excerpts. In addition, I tried several regex examples from the web which select an element with class, but again none worked.

    Do you have any ideas what else i could try?

    Thank you and kind regards

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Mikko Saari

    (@msaari)

    I tested your function, and it works. Are you sure the excerpts you’re seeing are from Relevanssi?

    Thread Starter kayamurer

    (@kayamurer)

    Hi Mikko,

    Thank you for your quick reply.

    I think it is the excerpts from Relevanssi. If I delete the plugin, the excerpts change. Even if I activate and deactivate the option to generate user-defined excerpts, the excerpts change.

    I just saw that there are known incompatibilities with the Page Builder Beaver Builder, I think this may be the problem.

    Please excuse me for not having seen this before.

    Plugin Author Mikko Saari

    (@msaari)

    You can debug the function:

    add_filter( 'relevanssi_excerpt_content', 'rlv_remove_noindex_class' );
    function rlv_remove_noindex_class( $content ) {
    
    var_dump(preg_replace( '#<(.*) class=".*?relevanssi_noindex".*?</\1>#ms', '', $content));
    var_dump($content);
    
        return preg_replace( '#<(.*) class=".*?relevanssi_noindex".*?</\1>#ms', '', $content );
    }

    This should output something if the filter is in use.

    • This reply was modified 1 year, 11 months ago by Mikko Saari.
    Thread Starter kayamurer

    (@kayamurer)

    This just throws the following error:

    Parse error: syntax error, unexpected ';', expecting ')'

    • This reply was modified 1 year, 11 months ago by kayamurer.
    Plugin Author Mikko Saari

    (@msaari)

    It was missing one ), I added that.

    Thread Starter kayamurer

    (@kayamurer)

    Thanks! Now i can see the whole contents of the found pages (I guess that’s correct?) I still see the unwanted contents wrapped in a div with class relevanssi_noindex. But on the results page the class is stripped off from the tag.

    https://schaerholzbau.pincgestaltet.ch/?s=holzbau

    Plugin Author Mikko Saari

    (@msaari)

    The H1 tags in the page content don’t have the relevanssi_noindex class. Does something strip it off, perhaps?

    Thread Starter kayamurer

    (@kayamurer)

    I guess yes. All page builder and layout specific tags are removed in the search results. Also all classes are stripped off. this is the corresponding page where the class is set. https://schaerholzbau.pincgestaltet.ch/kompetenzen/holzbau/

    Maybe i can try to create a different search.php template and use that. Or do you have any other idea?

    Thread Starter kayamurer

    (@kayamurer)

    I think i found the problem. In this forum thread https://community.wpbeaverbuilder.com/t/getting-bb-page-content-for-a-post/5553/3 a staff member from beaver builder wrote that the beaver builder content is only rendered within a loop using the the_content function with this example. So i guess that’s why all the tags are missing and the classes. This is the example from beaver builder support:

    query_posts( array(
        'post_type' => 'page',
        'p' => $pageId,
    ) );
    
    while (have_posts()) : the_post();
        the_content();
    endwhile;
    
    wp_reset_query();
    Plugin Author Mikko Saari

    (@msaari)

    I don’t think the_content() should strip the classes from the post content.

    Well, this leaves two options: either figure out a way to get the classes to remain in the text so they can be used, or write the filter in a way that uses what’s available.

    Thread Starter kayamurer

    (@kayamurer)

    I guess I expressed myself a little inaccurately. I actually meant that Beaver Builder only renders its layouts (with all classes etc.) when the page content has been processed by the_content. This does not seem to be the case with the Beaver Builder theme on the search results. I am already in contact with Beaver Builder support and trying to find a solution. Thanks Mikko for your support.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Exclude class from custom excerpt’ is closed to new replies.