0

我尝试创建功能来替换自定义帖子类型 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
4

2 回答 2

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
    }
}
于 2016-07-21T22:50:35.777 回答
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

于 2016-02-26T01:18:33.247 回答