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
2 Answers
Reset to default 10The 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
版权声明:本文标题:filters - Customizing Wordpress the_title with add_filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736773589a1952217.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论