admin管理员组文章数量:1402962
I using the below code to get modified data for the post, but I need to get the same modified post for my other custom posts. For example, it works if i change post to test( my custom post type name)"if( 'post' === get_post_type() )", but i need for both test, post and movies etc.
function et_last_modified_date_blog( $the_date ) {
if ( 'post' === get_post_type() ) {
$the_time = get_post_time( 'His' );
$the_modified = get_post_modified_time( 'His' );
$last_modified = sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y' ) ) );
$date = $the_modified !== $the_time ? $last_modified : get_post_time( 'M j, Y' );
return $date;
}
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );
I using the below code to get modified data for the post, but I need to get the same modified post for my other custom posts. For example, it works if i change post to test( my custom post type name)"if( 'post' === get_post_type() )", but i need for both test, post and movies etc.
function et_last_modified_date_blog( $the_date ) {
if ( 'post' === get_post_type() ) {
$the_time = get_post_time( 'His' );
$the_modified = get_post_modified_time( 'His' );
$last_modified = sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y' ) ) );
$date = $the_modified !== $the_time ? $last_modified : get_post_time( 'M j, Y' );
return $date;
}
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );
Share
Improve this question
edited Mar 30, 2020 at 14:52
RiddleMeThis
3,8078 gold badges22 silver badges30 bronze badges
asked Mar 30, 2020 at 14:43
herbals freshherbals fresh
134 bronze badges
2 Answers
Reset to default 2In your if statement just add more conditions like this.
function et_last_modified_date_blog( $the_date ) {
if ( 'post' === get_post_type() || 'movies' === get_post_type() || 'etc' === get_post_type()) {
$the_time = get_post_time( 'His' );
$the_modified = get_post_modified_time( 'His' );
$last_modified = sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y' ) ) );
$date = $the_modified !== $the_time ? $last_modified : get_post_time( 'M j, Y' );
return $date;
}
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );
The "||" means OR. So the condition would read If post type equals post or movies or etc.
Another way is to use in_array
. Also you do not return the original date if your check fails. See code below for fix.
function et_last_modified_date_blog( $the_date ) {
// all post types were are checking for
$post_types = array('post', 'movies');
// check to see if current post type is in the $post_types array
if (in_array(get_post_type(), $post_types)) {
$the_time = get_post_time( 'His' );
$the_modified = get_post_modified_time( 'His' );
$last_modified = sprintf( __( 'Last updated %s', 'Divi' ), esc_html( get_post_modified_time( 'M j, Y' ) ) );
$date = $the_modified !== $the_time ? $last_modified : get_post_time( 'M j, Y' );
return $date;
}
// NOTE: you should return the original date here in case your check "fails"
return $the_date;
}
add_action( 'get_the_date', 'et_last_modified_date_blog' );
add_action( 'get_the_time', 'et_last_modified_date_blog' );
本文标签: How to get 2 or multiple custom post types in wordpress functionsphp
版权声明:本文标题:How to get 2 or multiple custom post types in wordpress functions.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744619704a2615947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论