customization - Please I want to prefix my WP posts title according to each category

admin2025-06-06  8

What i mean is that.

My categories are:

Music. Video Etc...

So i want post title per category to be like this.

Post in music = should be "Download Music: wp_title()"

Post in video = should be "Download Video: wp_title()

What i mean is that.

My categories are:

Music. Video Etc...

So i want post title per category to be like this.

Post in music = should be "Download Music: wp_title()"

Post in video = should be "Download Video: wp_title()

Share Improve this question edited Nov 6, 2018 at 9:54 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Nov 6, 2018 at 6:13 teejah jamesteejah james 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You have already mentioned the function wp_title(). Right before outputting the title, it passes its value through the filter wp_title, which can be used here to prepend with additional information.

add_filter('wp_title', 'WPSE_20181106_prepend_title', 10, 3);
function WPSE_20181106_prepend_title($title, $sep, $seplocation) {
    // not a single post
    if (!is_singular('post')) {
        return $title;
    }

    // IDs of categories that should prepend the title
    $prepend_categories = [15, 35];

    // get all categories of post
    $categories = get_the_category();
    foreach ($categories as $category) {
        // found category
        if (in_array($category->term_id, $prepend_categories)) {
            // return new format, using __() so it is translateable
            return sprintf('%s %s: %s',
                __('Download', 'lang-slug'),
                $category->name,
                $title
            );
        }
    }
    // category not found, return default
    return $title;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749202695a317224.html

最新回复(0)