2013-04-25 97 views
0

我無法得到這個顯示來自我的自定義帖子類型名爲「資源」的帖子。任何幫助?爲什麼我的wordpress查詢不能工作?自定義帖子類型

<?php 
    add_action('pre_get_posts', 'add_custom_post_type_to_query'); 
    function add_custom_post_type_to_query($query) { 
     if (is_home() && $query->is_main_query()) 
      $query->set('post_type', array('post', 'page', 'resources')); 
     return $query; 
    } 
?> 

<?php if (have_posts()) : ?> 
    <?php /* Start the Loop */ ?> 
    <?php while (have_posts()) : the_post(); ?> 
     <?php the_title(); ?> 
     <?php the_content(); ?> 
     <?php get_template_part('content', get_post_format()); ?> 
    <?php endwhile; ?> 
    <?php twentytwelve_content_nav('nav-below'); ?> 
<?php else : ?> 
+1

你把'add_custom_post_type_to_query'東西放在你的模板中了嗎?它應該駐留在你的'functions.php'文件中 – diggy 2013-04-25 19:48:12

回答

0

這部分應該進入的functions.php:

<?php 
    add_action('pre_get_posts', 'add_custom_post_type_to_query'); 
    function add_custom_post_type_to_query($query) { 
     if (is_home() && $query->is_main_query()) 
      $query->set('post_type', array('post', 'page', 'resources')); 
     return $query; 
    } 
?> 

,而這部分是在模板文件:

<?php if (have_posts()) : ?> 
    <?php /* Start the Loop */ ?> 
    <?php while (have_posts()) : the_post(); ?> 
     <?php the_title(); ?> 
     <?php the_content(); ?> 
     <?php get_template_part('content', get_post_format()); ?> 
    <?php endwhile; ?> 
    <?php twentytwelve_content_nav('nav-below'); ?> 
<?php else : ?> 

說明:

第1部分被激活過濾器/對pre_get_posts()採取行動。但是當你把它放在一個主題文件或模板文件中時,過濾器會啓動得太晚或根本不啓動。

您的functions.php主題中的文件是第一個要解析的文件,因此它應包含與頁面的渲染或可視化無直接關係的所有功能。

然而,這是一個渲染機制,loop應該在適當的主題文件。