admin管理员组

文章数量:1135090

I'm trying to create a custom wordpress title for a posts made under a custom post. Basically, its a version changelog for an application. I would like to only input the version number in the title field, and the output has to be standardized with a string accordingly.

My custom post type is 'custom_version' and the title output that I'm looking for is "Application has been updated to version ".

I've understood that this can be achieved using add_filter and I've tried playing with this code for days now, but I'm not really a PHP Pro so help is much appreciated :) Here's the code:

add_filter('the_title', 'new_title');
function new_title($title) {
    global $post, $post_ID;
    $title['custom_version'] = 'Application has been updated to v'.$title;
    return $title;
}

I'm trying to create a custom wordpress title for a posts made under a custom post. Basically, its a version changelog for an application. I would like to only input the version number in the title field, and the output has to be standardized with a string accordingly.

My custom post type is 'custom_version' and the title output that I'm looking for is "Application has been updated to version ".

I've understood that this can be achieved using add_filter and I've tried playing with this code for days now, but I'm not really a PHP Pro so help is much appreciated :) Here's the code:

add_filter('the_title', 'new_title');
function new_title($title) {
    global $post, $post_ID;
    $title['custom_version'] = 'Application has been updated to v'.$title;
    return $title;
}
Share Improve this question edited Jan 24, 2012 at 17:00 Geoff Dalgas 1015 bronze badges asked Aug 22, 2011 at 3:39 shahzshahz 331 gold badge1 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

The issue is that you are mixing up the $title variables. $title is the parameter passed to the new_title function, but then you use it as an array: $title['custom_version']. Try this:

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('custom_version' == get_post_type($id))
        $title = 'Application has been updated to v'.$title;
    return $title;
}

I'd also highly recommend prefixing your function with a unique prefix because you may run into another plugin/theme using a function called new_title, which will reek havoc!

I think WooCommerce did a good job on showing possible ways to verify if you really want to override the title using the_title filter.

Sometimes, you may change the title that will appear in your category list, and other places you don't want to.

Here is how they do:

# plugins/woocommerce/includes/wc-page-functions.php

function wc_page_endpoint_title( $title ) {
    global $wp_query;
    if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
        $endpoint       = WC()->query->get_current_endpoint();
        $action         = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
        $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action );
        $title          = $endpoint_title ? $endpoint_title : $title;
        remove_filter( 'the_title', 'wc_page_endpoint_title' );
    }
    return $title;
}

add_filter( 'the_title', 'wc_page_endpoint_title' );

More important than rewriting the title, are the methods and checks they do...

! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() etc

Keep those in mind before overrind titles you don't want to, or in places you shouldn't.

本文标签: filtersCustomizing Wordpress thetitle with addfilter