2013-10-05 103 views
4

所以我有這個網站,你可以看到有兩個菜單,其中一個旁邊的徽標右上方的另一個;WordPress的pre_get_posts類別過濾器刪除自定義菜單項

http://www.ducklingfarm.com

他們正在使用中的functions.php這個代碼創建的;

function register_my_menus() { 
register_nav_menus(
    array(
    'header-menu' => __('Header Menu'), 
    'extra-menu' => __('Extra Menu') 
) 
); 
} 
add_action('init', 'register_my_menus'); 

這是我使用菜單的代碼;

<nav> 
<?php wp_nav_menu(array('theme_location' => 'header-menu')) ?> 
</nav> 

<nav id="ecommerce"> 
<?php wp_nav_menu(array('theme_location' => 'extra-menu')); ?> 
</nav> 

,並且菜單做工精細,除非你去類別的側邊欄,如「博客」頁上的「公司章程」或「事件」;

http://www.ducklingfarm.com/blog/

的博客頁面是一個自定義後的類型,並且使工作類,我添加了一些代碼到functions.php文件,並且菜單沒有從那時起正常工作。該代碼是;

add_filter('pre_get_posts', 'query_post_type'); 
function query_post_type($query) { 
if(is_category() || is_tag()) { 
$post_type = get_query_var('post_type'); 
if($post_type) 
    $post_type = $post_type; 
else 
    $post_type = array('post','Blog'); 
$query->set('post_type',$post_type); 
return $query; 
} 
} 

所以我猜測代碼有問題。請幫幫我!我真的很感激它。

最佳, Jaeeun

我解決它通過改變最後的碼本;

add_filter('pre_get_posts', 'query_post_type'); 
function query_post_type($query) { 
if(is_category() && $query->is_main_query()) { 
$post_type = get_query_var('post_type'); 
if($post_type) 
    $post_type = $post_type; 
else 
    $post_type = array('post','Blog'); 
$query->set('post_type',$post_type); 
return $query; 
} 
} 
+1

到底是什麼問題菜單? –

回答

6

你可以試試這個(無需多次if$post_type = $post_type;

add_filter('pre_get_posts', 'query_post_type'); 
function query_post_type($query) { 
    if(is_category() && $query->is_main_query()) { 
     $query->set('post_type', array('post', 'Blog')); 
    } 
    return $query; 
} 
相關問題