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
- Obtain page data and use regular loop
- How to display posts by vote count and if no value continue with latest posts with no votes?
- How can I load posts starting from, and including, the current post?
- Load wordpress content in other domain
- Genesis multiple category custom loop 404s after first page
- Custom Loop Using Shortcode in Custom Page Template
- Do I need to use The Loop on pages?
- Sort results by groups of numbers and letters
- Listing subpages title and content and styling the first iteration differently
- Infinite Scroll on a loop?
- Shortcode in posts called by ajax is not working
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- How to fix pagination for custom loops?
- Modify the main loop to display current month / year
- Show all posts using the template page and the loop?
- Checkbox doesn’t stay checked, conten
- Showing sticky posts with get_posts()
- Pagination – works on local but not live dev!
- On single.php, fetch 2 posts created after and 2 posts created before in relation to the actual post (using menu_order)
- query_posts() vs get_posts() multiple loops [duplicate]
- Page-loop is looping content from custom loops on the same site. How to avoid that?
- need help looping add_action in wp
- Problem getting current post tags to show in a widget
- Modified loop-single-forum.php to work inside of a sidebar Widget, not working on some site pages
- List authors with the last post title and order by last post date
- How to get the role id from the role name?
- Does WordPress Loop have a function like Shopify’s cycle?
- How do I paginate a get_posts() request inside another page loop (Paginating childpages)
- Basic multiple loops only displaying first loop
- Using Javascript for Looped Content
- Latest Post Styled Different Than other Posts
- Display post list with different styles
- ‘Auto Draft’ Post showing in wordpress loop
- How do I display an image before the first post of the loop when I’m using get_template_part?
- ACF custom field in [closed]
- Split wordpress loop to multiple layouts
- Post classes in the loop are being changed in certain themes
- single.php – how to mark current page in the loop
- Problem with displaying HTML content after in_array [closed]
- post thumbnail, conditions, else wont work
- How do I make a variable available inside partials?
- How to hook into container
- Why does `inclusive` not work in this wordpress loop?
- Combine the results of two loops [closed]