用于替换自定义帖子类型的内容显示的功能

时间:2016-02-25 19:11:04

标签: wordpress custom-post-type storefront

我尝试创建功能来替换自定义帖子类型WordPress Storefront子主题的内容显示,但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

2 个答案:

答案 0 :(得分:0)

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

add_action('the_post','customise_storefront');

如果你打算在那里尝试访问$ post对象,你还应该在customise_storefront的开头包含一个全局$ post引用。

虽然如果你确实使用了the_post钩子,它仍会通过引用传递post对象,所以你可以修改为:

function customise_storefront ($post_object) {
    if (get_post_type ($post_object->ID) ...

请参阅https://codex.wordpress.org/Plugin_API/Action_Reference/the_post

答案 1 :(得分:0)

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
    }
}