2016-02-25 125 views
0

我試圖創建函數來替換自定義帖子類型WordPress Storefront子主題的內容顯示,但如果條件不起作用! 評論如果陳述是我所嘗試過的。函數替換內容顯示爲自定義帖子類型

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

回答

0

您正在將您的操作掛鉤到init。當時沒有發佈數據,所以這就是爲什麼你的測試失敗。嘗試在發佈數據加載完成後使用更高版本的掛鉤

add_action('the_post','customise_storefront'); 

你還應該包括customise_storefront開始全球$帖子引用如果你打算嘗試訪問$ post對象那裏。

但如果你使用the_post鉤,它經過引用後的對象,所以你可以修改到:

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

https://codex.wordpress.org/Plugin_API/Action_Reference/the_post

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 
    } 
} 
相關問題