If I understand your question correctly, you can try the following:
add_filter( 'the_content', 'incrementparagraphs', 10, 1 );
function incrementparagraphs( $content )
{
return preg_replace_callback(
'|<p>|',
function ( $matches )
{
static $i = 0;
return sprintf( '<p data-section-id="%d">', $i++ );
},
$content
);
}
to replace every <p>
tags with <p data-section-id="1">
, where the data-section-id
attribute contains the incremental count of the current p
tag.
Example:
Before:
<p>aaa</p>
<p>bbb</p>
<p>ccc</p>
After:
<p data-section-id="0">aaa</p>
<p data-section-id="1">bbb</p>
<p data-section-id="2">ccc</p>
More on preg_replace_callback
here.
Related Posts:
- Split Content and Gallery
- How can i display the content in plaintext
- remove tags from the_content
- the_content and wp_link_pages
- Endless loop with wp_insert_post and wp_update_post
- Use the_content outside the loop
- post__in – Placing content from a foreach loop inside of an array
- get_the_content not working in loop?
- In a foreach loop, how do I target the last item in the loop?
- the_excerpt(), get_the_excerpt() and the_content() all killing “the Loop”
- Content/Excerpt length control for a specific loop?
- How to iterate through database until it find a match
- Output 2 items within the Loop
- Combining the_excerpt with the_content
- the_content() seems to block my ACF-code
- Removing the_content() from the page
- How to make post content from each post display within the same div
- Loop after page content
- Only display videos and images in the_content() within loop
- Styling text and images in the_content()
- Get all posts including sticky ones with get_posts(),setup_postdata(), and foreach loop?
- How to print out menu names in the foreach loop?
- Cannot retieve the_content() and the_author() – both returning empty strings
- Add class to last 3 posts in loop
- Output meta into arrays
- Need to add class to first element in a foreach loop
- Foreach displays from last to first
- Simple foreach loop help needed in wordpress “the loop”
- How to print redux gallery feilds id? [closed]
- How is WordPress changing the content markup?
- Strip from or something better?
- How to check all items in a loop have post thumbnail?
- Problem in replacing the_content with the_excerpt()
- Give ID to divs inside the loop
- Insert content above an embedded video inside Wordress’ php the_content
- Split Content and Gallery
- Get featured image outside the loop using foreach
- Why loop renders only last metabox from array? [closed]
- the_content() keeps repeating itself
- Echo Most Recent Sticky Post in Loop?
- Foundation modal in wordpress php loop
- ACF field not appearing correctly in loop
- Making a specific page wordpress compatable that is not index
- how to use custom loop in the_content filter
- change the_content images for different sizes (Desktop, tablet, mobile)?
- Have multiple loops on a page appear after/between page content
- In Loop Output Place Custom Field Content In Between Paragraphs
- Help with add_filter(‘the_content’, ‘some_function’) and multiple matches
- Eliminate duplicates in a foreach loop [closed]
- Single Loop With Dual Content Area
- Wrap every 2 divs in row – for each loop [closed]
- How to add custom HTML markup and classes to the_content()?
- ACF loops false value with repeater and checkbox
- Excerpt all post content Content Same Size without word cutting off
- the_content() “crashes” for single pages
- Hide old posts each year
- Retrieve each widget separately from a sidebar
- Display all authors and their only one latest post
- Remove comma from last item output from loop
- How to paginate this custom loop? [duplicate]
- Select data from database and list it in Loop (WordPress)
- I’ve been out of the loop since 2.8. What did I miss?
- Loop first six posts in carousel, next eight in grid
- Infinite blog loop
- One custom loop with condition to check child posts
- Loop get_theme_mod
- Dynamic Stylesheet loads but doesn’t finish
- Template structure
- Filter the_posts doesn’t work on search page
- Getting content from a referenced post
- How to get the excerpt for is_home() outside of the loop isn’t working
- First archive page with a few posts
- get_the_ID() in the footer returns wrong value
- Loop through ACF taxonomies and output associated posts
- How to get user ID’s from multiple usernames?
- WP_Query multiple post results
- Convert Featured products loop into Recent products loop?
- Query posts only with actual text content (not including shortcode or images)
- How do i create a list-posts-page?
- Exclude posts based on an array
- How can I increase the post count for custom post types only?
- List ALL posts by author on author archive
- How to Add a cutsom slug to my custom author role
- Using is_page_template inside loop
- How to output the taxonomies that are assigned to each custom post?
- when looping through custom post data not appearing
- Replace an array (with identical values) with another array in the_content
- How to show the last article in a different way in my blog?
- How to fix pagination for custom loops?
- Check to see if a field is within an array in twig
- Add div after every 4 posts then every 2 posts for a responsive loop
- Archive sorted by month – separate
- Humanmade | Custom meta box class: How to display a repeatable meta box group?
- Can’t print out returned value
- Get query result before posts are displayed?
- WordPress Group By Problem
- Home page loop with pagination problem
- Way of getting queried loop before the query with a filter hook?
- Problem with ms-thumb-frame-selected class in Master Slider
- How can you make it so the comment box shows on some pages or posts but not all?