admin管理员组

文章数量:1125739

When my WordPress site loads up, towards the top, I have something that is generated in the DOM....

<a class="skip-link screen-reader-text" href="#content">Skip to content</a>

From my child theme, what action or filter can I use to change the href value from #content to something like #content-top? Having trouble to finding resources online that talk about making this adjustment...

I tried the following just to see if it goes away as a test...

 remove_action( 'wp_footer', 'the_block_template_skip_link' );

But that has no impact.

When my WordPress site loads up, towards the top, I have something that is generated in the DOM....

<a class="skip-link screen-reader-text" href="#content">Skip to content</a>

From my child theme, what action or filter can I use to change the href value from #content to something like #content-top? Having trouble to finding resources online that talk about making this adjustment...

I tried the following just to see if it goes away as a test...

 remove_action( 'wp_footer', 'the_block_template_skip_link' );

But that has no impact.

Share Improve this question edited Feb 6, 2024 at 2:55 klewis asked Feb 6, 2024 at 2:49 klewisklewis 8991 gold badge14 silver badges29 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

If remove_action( 'wp_footer', 'the_block_template_skip_link' ) did not work, then try with remove_action( 'wp_enqueue_scripts', 'wp_enqueue_block_template_skip_link' );, or both of that. (see source on GitHub)

As for changing the href value, I'm not aware of any (filter) hook to do that, but you can either edit your template and set the <main>'s id value to content-top, or you can use JavaScript to modify the href value. E.g.

document.querySelector( 'a.skip-link[href="#content"]' ).href = '#content-top';

the_block_template_skip_link function is a part of the WordPress block editor and is responsible for generating the Skip to content link in the HTML output. To modify the href value of this link, you can use the the_block_template_skip_link filter.

For more detail visit :- the_block_template_skip_link()

Add code to your child theme's functions.php file

function modify_block_template_skip_link($output) {
    // Replace the href value with your desired value, e.g., #content-top
    $output = str_replace('href="#content"', 'href="#content-top"', $output);
    return $output;
}

add_filter('the_block_template_skip_link', 'modify_block_template_skip_link');

本文标签: