Categories
PHP WordPress

Create anchor links using custom fields value

//You need to avoid special characters (accept alpha-numeric characters only)

<?php if(get_field('text_block')): ?>
    <ul>
    <?php while(has_sub_field('text_block')): ?>
        <li><a href="<?php the_permalink() ?>#<?php echo preg_replace('/[^A-Za-z0-9]/', "", get_sub_field('paragrah_header')); ?>" class="scroll-anchor"><?php the_sub_field('paragrah_header'); ?></a></li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>
<h2 id="<?php echo preg_replace('/[^A-Za-z0-9]/', "", get_sub_field('paragrah_header')); ?>"><?php the_sub_field('paragrah_header'); ?></h2>