我需要在wp从数据库获取自定义帖子后立即检查post\\u meta中的某个值是否为true,如果为false,我需要修改/向帖子添加一些数据,然后显示修改后的版本,如果为true,则应像往常一样继续。一般来说,要实现这样的目标,最好的挂钩和方法是什么?
编辑:之前忘了提到,但我更喜欢在插件中这样做。
我需要在wp从数据库获取自定义帖子后立即检查post\\u meta中的某个值是否为true,如果为false,我需要修改/向帖子添加一些数据,然后显示修改后的版本,如果为true,则应像往常一样继续。一般来说,要实现这样的目标,最好的挂钩和方法是什么?
编辑:之前忘了提到,但我更喜欢在插件中这样做。
您可以使用the_content
过滤以在输出内容之前修改内容。
function my_the_content_filter( $content ) {
// maybe limit to archive or single cpt display?
if( is_post_type_archive( \'your-cpt\' )
|| is_singular( \'your-cpt\' ) ){
global $post;
$meta = get_post_meta( $post->ID, \'some-key\', true );
if( false === $meta ){
return $content . \'some extra content\';
}
}
return $content;
}
add_filter( \'the_content\', \'my_the_content_filter\' );
我会在我的single-custon_post_type.php
, 使用get_meta()
函数和条件检查。
如果元字段未设置,或不包含所需的数据,则可以单独显示内容。
我的主题当前函数使用pre_get_posts, 目前正处于我的主题功能的底部。php文件:function mytheme_portfolio_archive_pages( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_post_type_archive( \'mytheme_portfolio\' ) ) {