Currently the only way I’ve found to achieve this is to put each question into its own FAQ block and insert a Custom HTML block above each FAQ block with the anchor code for that question.
E.g.
Custom HTML Block
<span id="question-1-anchor"></span>
FAQ Block
Question and answer 1
Custom HTML Block
<span id="question-2-anchor"></span>
FAQ Block
Question and answer 2
Custom HTML Block
<span id="question-3-anchor"></span>
FAQ Block
Question and answer 3
However this method splits up the JSON structured data and I’m not sure if this has any impact on the validity of the structured data.
-
This reply was modified 4 years, 2 months ago by
james4311.
I can confirm the above solution does indeed invalidate the structured data. Google’s Rich Results Test reports the error ‘Duplicate field “FAQPage”‘.
Hi @james4311
currently the plugin does not offer a setting for this. But we will integrate it in the next update. Then you will be able to set/unset anchors for each FAQ block.
Alternatively you can use a “Table of Contents” plugin for this. E.G. https://de.wordpress.org/plugins/easy-table-of-contents/
This will automatically add anchors to all headings.
Or you can extend your functions.php with the following code-snippet:
/**
* Automatically add IDs to headings such as <h2></h2>
*/
add_filter( 'the_content', function ( $content ) {
if ( is_single() && in_the_loop() && is_main_query() ) {
$content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) {
if ( ! stripos( $matches[0], 'id=' ) ) :
$heading_link = '<a href="#' . sanitize_title( $matches[3] ) . '" class="heading-link">#</a>';
$matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $heading_link . $matches[3] . $matches[4];
endif;
return $matches[0];
}, $content );
}
return $content;
} );
Let us know if this works for you.
Greetings, Gordon
Thanks @gorbo
The snippet works but I’ll wait for the plugin update as I need to be able to enter custom anchor names to match the links that already exist across my website (and elsewhere).
Appreciate your help.