admin管理员组

文章数量:1122832

I'm using the Press Customizr theme. I need to add an ID (not class) to an existing HTML element from the theme (specifically it's the Related posts section)

Is there a way I can do this with a filter/hook? The element has an existing class, so if I could target that element via its class and add a unique ID to it, without removing the class.

I'm using the Press Customizr theme. I need to add an ID (not class) to an existing HTML element from the theme (specifically it's the Related posts section)

Is there a way I can do this with a filter/hook? The element has an existing class, so if I could target that element via its class and add a unique ID to it, without removing the class.

Share Improve this question asked Jan 13, 2019 at 9:24 BYellowJBYellowJ 11 bronze badge 4
  • There is no filter for arbitrarily changing HTML elements. There can't be. You will need to check with the theme documentation and author to see if there's something in your theme that will let you do this. – Jacob Peattie Commented Jan 13, 2019 at 9:54
  • Well in that case the only solution would be to add an empty html element right next to the one I want to target. Is that possible? How do I add a new element while using "right after X element" as its location? – BYellowJ Commented Jan 13, 2019 at 10:00
  • You can’t do that either. Everything you’re asking would require support from the theme. – Jacob Peattie Commented Jan 13, 2019 at 12:01
  • 1 So you know, technically yes you can do that with JavaScript, i.e. jQuery('.the-existing-class').attr( 'id', 'the-id-you-want' ). However you can't be sure there aren't multiple elements with that class, and the code would apply to all of them unless you limited it. It's still a backwards way of thinking though. Adding something to the DOM like this should be done before the page is given to the browser, which is in the realm of the theme. – tmdesigned Commented Jan 13, 2019 at 12:05
Add a comment  | 

1 Answer 1

Reset to default 3

You can add an ID:

  1. Going to the settings area of the block.

  2. Scroll down and expand the "Advanced" section.

  3. Add the ID name you want in the HTML Anchor box. Don't use the hashtag in front of the name. That is added automatically by WP.

本文标签: How to add an ID to an existing HTML element