NotJay
NotJay

Reputation: 4076

How can I preg_replace a tag in the title on WordPress?

I am attempting to locate ANY of the post TAGS in the POST TITLE and preg_replace the match with the tag surrounded by a span to add css (bold) to it. The end result should be the post title with any tags bolded.

        <h2 class="entry-title">
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>">
                <?php 
                    $titlename = the_title();
                    $tags = array(just_tags());
                    foreach($tags as $tag) {
                        $displaytitle = preg_replace("$tag", "<span class=\"larger\">$tag</span>", $titlename);
                    }
                    echo $displaytitle;
                ?>
            </a>
        </h2>

As you can see in the code, I modified a few functions to attempt to get just the tags, no $before and $after.

function get_just_the_tag_list() {
    return get_the_term_list('post_tag');
}

function just_tags() {
    echo get_just_the_tag_list();
}

Upvotes: 0

Views: 1690

Answers (3)

crowjonah
crowjonah

Reputation: 2878

Your preg_replace is looking for the text "$tag" in $titlename. Take it out of quotation marks, or wrap it in curly braces "{$tag}"!

get_the_terms_list returns an HTML formatted list of terms. You want to use get_the_terms, and that is automatically returned as an array, so $tags should be defined like so (assuming this is in the loop and $post is accurate:

$tags = get_the_terms($post->ID, 'post-tags');

<h2 class="entry-title">
        <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>">
            <?php 
                $titlename = get_the_title();
                $tags = get_the_terms($post->ID, 'post_tag');
                foreach($tags as $tag) {
                    $titlename = str_replace($tag->name, '<span class="larger">'.$tag->name.'</span>', $titlename);
                }
                echo $titlename;
            ?>
        </a>
    </h2>

This means that your $displaytitle is being completely rewritten for each $tag, and if the last $tag isn't found in the post title, nothing will change.

Upvotes: 1

Liam Bailey
Liam Bailey

Reputation: 5905

You should really look into Wordpress filters. There is a filter directly on the_title(), which would allow you to perform this functionality.

apply_filters('the_title','my_filter')

function my_filter($title)
{
//do what you want and
return $title; //when finished altering.
}

If you want to stay the way you are you need

get_the_title()
$titlename = get_the_title();//inside the loop
or
global $post;
$titlename = get_the_title($post->ID);//outside the loop

plus crowjonah's answer, removing the quotes around $tag, although you probably need to make it preg_replace("/" . $tag->name . "/", '<span class="larger">'.$tag->name.'</span>', $titlename );

Or Benjamin Paap's str_replace

str_replace($tag->name, '<span class="larger">'.$tag->name.'</span>', $titlename  );

Upvotes: 0

Benjamin Paap
Benjamin Paap

Reputation: 2759

Souldn't you just do something like this?

$titlename = the_title();
$tags = get_the_terms($post->ID, 'post_tag');
foreach($tags as $tag) {
    $displaytitle = str_replace($tag->name, "<span class=\"larger\">$tag</span>", $titlename);
}

You don't need to use regular expressions because you want to replace the whole tag. The just_tags function is not needed anymore.

Upvotes: 1

Related Questions