Title: Function to get html-anchor id
Last modified: May 28, 2020

---

# Function to get html-anchor id

 *  [schmause](https://wordpress.org/support/users/schmause/)
 * (@schmause)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/function-to-get-html-anchor-id/)
 * Hi,
    i am trying to set up a dynamic content-directory for my blog-posts in a
   sidebar. I want it to fetch the html-anchors that i can set in the post-editor(
   under Blocks- Advanced-tab).
 * What function do i use for that, what object stores the anchor-information?
 * Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/function-to-get-html-anchor-id/#post-12906123)
 * Do you mean additional CSS classes? It’s the only advanced field in paragraph
   blocks. A tag’s ID attribute is something else and is not supported by default
   blocks. The additional CSS classes are stored within the post content itself.
   You can see for yourself through the Code Editor view (access through the 3 dot
   icon at upper right).
 * While you could parse or search post content in PHP to extract added class names,
   it’s better accomplished through JavaScript after the page loads.
 *  Thread Starter [schmause](https://wordpress.org/support/users/schmause/)
 * (@schmause)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/function-to-get-html-anchor-id/#post-12907105)
 * Hi, thank you for your answer.
    I think i need to clarify a bit.
 * I can set html-anchors for h1 – h6 elements inside the editor (described here:
   [https://wordpress.org/support/article/page-jumps/](https://wordpress.org/support/article/page-jumps/)).
 * The resulting anchor (not CSS-ID) can be used similar to slugs as links. I want
   to know (dynamically), what these links are, depending on the blog-post you are
   on, to feed these links to a sidebar menu.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/function-to-get-html-anchor-id/#post-12911956)
 * So you basically want to auto-generate a table of contents based on the current
   post’s `<H?>` tags whose links jump to the referenced section? Your code would
   need to parse through post content to find all `<H?>` tags which have ID attributes.
   There is added block data contained within the block’s HTML comment, but it is
   replicated in the HTML so block comments can be ignored. You can use `preg_match_all()`
   to find all the tags while capturing the ID attribute and content within each
   tag. Use the captured data to construct anchor links in the sidebar.
 * Creating appropriate RegExp to accurately find and capture data can be rather
   difficult. There are a number of “fiddle” sites for RegExp which allow you to
   more easily try out and refine your RegExp. One example is [regexr.com](https://regexr.com/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Function to get html-anchor id’ is closed to new replies.

## Tags

 * [Content directory](https://wordpress.org/support/topic-tag/content-directory/)
 * [html anchor](https://wordpress.org/support/topic-tag/html-anchor/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/function-to-get-html-anchor-id/#post-12911956)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
