So I am trying to generate a list of post tags on the data-type attribute but I need them to be sanitized so spaces are replaced with underscores and there are no special characters... really struggling with it.

                <article class="work_items">

                  query_posts(array( 'post_type' => 'work', 'posts_per_page' => '-1', 'orderby'=>'title','order'=>'ASC'));
                  if ( have_posts() ) : while ( have_posts() ) : the_post();

                    <aside class="work_item" data-id="id-<?php the_id(); ?>" data-type="
    $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
        $tag = preg_replace('/[^A-Za-z0-9-]+/', '_');
        echo $tag->name . ' ';

                                <div class="img_wrap">
                                    <?php echo get_the_post_thumbnail($post_id, 'Work Gallery', array('class' => 'lazy', 299,299)); ?>
                                </div><!-- /.img_wrap-->
                                <ul class="work_meta">
                                    <li class="work_title"><?php the_title(); ?></li>
                                    <li class="work_item_content"><?php echo excerpt(25); ?></li>
                                        if( has_tag() ) {
                                            // IF THE POST HAS TAGS
                                            the_tags('<li class="work_tags first">', '.</li><li class="work_tags">', '.</li>');
                                        else {
                                            // IF NO TAGS
                                <ul class="work_features">
                                    <li class="view_all"><a class="tooltip" title="full details" href="<?php echo the_permalink(); ?>">View All</a></li>
                                    <li class="expand"><a rel="prettyPhoto[mixed]" class="expanded tooltip" title="preview" href="#prettyPhoto-<?php the_id(); ?>">Expand</a></li>
                                    <li class="lightbox">
                                        <?php if (has_post_thumbnail( $post->ID ) ): ?>
                                        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
                                            <a class="work_lb tooltip" title="view in lightbox" href="<?php echo $image[0]; ?>" rel="prettyPhoto">Lightbox</a>
                                        <?php endif; ?>

                    </aside><!-- /.work_item-->

Any help would be greatly appreicated.


Use tag->slug instead of tag->name.

Spaces and special characters will be replaced by dashes but altering that is easy:

echo str_replace('-','_',$tag->slug);



  • That worked like a charm only issue is its replacing special characters with "-" and I need them to be replaced with "_" ... any ideas on that one? – Danny Aug 27 '13 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.