Wordpress 用于替换自定义帖子类型的内容显示的函数

Wordpress 用于替换自定义帖子类型的内容显示的函数,wordpress,custom-post-type,storefront,Wordpress,Custom Post Type,Storefront,我试图创建函数来替换自定义帖子类型WordPress店面子主题的内容显示,但if条件不起作用! 注释的if语句就是我尝试过的 //if (is_singular() && (get_post_type() == 'gallery')) { add_action( 'init', 'customise_storefront' ); //} function customise_storefront() { //if(get_post_type( $post->I

我试图创建函数来替换自定义帖子类型WordPress店面子主题的内容显示,但if条件不起作用! 注释的if语句就是我尝试过的

//if (is_singular() && (get_post_type() == 'gallery')) {
add_action( 'init', 'customise_storefront' );
//}


function customise_storefront() {

    //if(get_post_type( $post->ID ) == 'gallery'){
    //if('gallery'== get_post_type()) {
    //if (is_singular('gallery')) {
    //if (is_single('gallery')) {
    //if (is_singular() && (get_post_type() == 'gallery')) {
       // Remove the storefromt post content function
       remove_action( 'storefront_single_post', 'storefront_post_content', 30 );
       remove_action( 'storefront_loop_post', 'storefront_post_content', 30 );
       // Add our own custom function
       add_action( 'storefront_single_post', 'gallery_post_content', 30 );
       add_action( 'storefront_loop_post', 'gallery_post_content', 30 );
    //}
}

/* Gallery post content*/
if ( ! function_exists( 'gallery_post_content' ) ) {
    function gallery_post_content() {
       ?>
            <div class="entry-content" itemprop="articleBody">
            <?php
            the_content(
                sprintf(
                    __( 'Continue reading %s', 'storefront' ),
                    '<span class="screen-reader-text">' . get_the_title() . '</span>'
                )
            );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
                'after'  => '</div>',
            ) );
            ?>
            </div><!-- .entry-content -->
            <?php
//如果(是单数的()&&(get-post-type()='gallery')){
添加_操作('init','customize_storefront');
//}
功能定制_店面(){
//如果(获取帖子类型($post->ID)='gallery'){
//if('gallery'==get\u post\u type()){
//如果(是单数('gallery')){
//如果(是单身(‘画廊’){
//如果(是单数形式的()&&(get-post类型()=='gallery')){
//删除storefromt post content函数
删除操作('storefront\u single\u post'、'storefront\u post\u content',30);
删除\u操作('storefront\u loop\u post','storefront\u post\u content',30);
//添加我们自己的自定义函数
添加操作(“店面单贴”、“画廊贴内容”,30);
添加操作('storefront\u loop\u post'、'gallery\u post\u content',30);
//}
}
/*图库帖子内容*/
如果(!function_存在('gallery_post_content')){
功能库\发布\内容(){
?>

您正在将操作挂接到init。此时没有可用的post数据,因此这就是if测试失败的原因。加载post数据后,请尝试使用稍后的挂接,例如

add_action('the_post','customise_storefront');
如果要尝试访问那里的$post对象,还应该在Customize_storefront的开头包含一个全局$post引用

但是,如果确实使用了_post钩子,它仍然会通过引用传递post对象,因此您可以修改为:

function customise_storefront ($post_object) {
    if (get_post_type ($post_object->ID) ...
请参见添加操作(“模板重定向”、“自定义店面”); 功能定制_店面(){ 如果(是单数('gallery')){ //删除storefromt post content函数 删除操作('storefront\u single\u post'、'storefront\u post\u content',30); 删除\u操作('storefront\u loop\u post','storefront\u post\u content',30); //添加我们自己的自定义函数 添加操作(“店面单贴”、“画廊贴内容”,30); 添加操作('storefront\u loop\u post'、'gallery\u post\u content',30); } } /*图库帖子内容*/ 如果(!function_存在('gallery_post_content')){ 功能库\发布\内容(){ ?>
add_action( 'template_redirect', 'customise_storefront' );



function customise_storefront() {
    if (is_singular('gallery')) {
       // Remove the storefromt post content function
       remove_action( 'storefront_single_post', 'storefront_post_content', 30 );
       remove_action( 'storefront_loop_post', 'storefront_post_content', 30 );
       // Add our own custom function
       add_action( 'storefront_single_post', 'gallery_post_content', 30 );
       add_action( 'storefront_loop_post', 'gallery_post_content', 30 );
    }
}

/* Gallery post content*/
if ( ! function_exists( 'gallery_post_content' ) ) {
    function gallery_post_content() {
       ?>
            <div class="entry-content" itemprop="articleBody">
            <?php
            the_content(
                sprintf(
                    __( 'Continue reading %s', 'storefront' ),
                    '<span class="screen-reader-text">' . get_the_title() . '</span>'
                )
            );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
                'after'  => '</div>',
            ) );
            ?>
            </div><!-- .entry-content -->
            <?php
    }
}