2014-09-29 118 views
0

我有一個自定義帖子類型0123'。顯示當前帖子類型的所有帖子標題列表

我想實現的是生成所有press自定義帖子類型的標題列表(以及它的相應鏈接)。

我試過用這段代碼沒有運氣,有什麼想法?

<?php function all_posts_custom_posts($query) { 
      $post_type = $query->query_vars['post_type']; 

      if ('press' == $post_type){ 
        $query->query_vars['posts_per_page'] = -1; 
        return; 
      } 
    } 
    add_action('pre_get_posts', 'all_posts_custom_posts',1); ?> 

並且還從列表中加入class來突出顯示當前帖子。

+0

關閉我的頭頂,在第3行做'echo $ post_type;'看看它吐出了什麼。也嘗試翻轉第4行的比較。 – 2014-09-29 16:51:30

回答

2

我以前有過類似的問題。以下代碼示例將顯示我如何解決我的問題。

<?php 
$type = 'products'; 
$args=array(
    'post_type' => $type, 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'caller_get_posts'=> 1 

$my_query = null; 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { 
    while ($my_query->have_posts()) : $my_query->the_post(); ?> 
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
    <?php 
    endwhile; 
} 
wp_reset_query(); // Restore global post data stomped by the_post(). 
?> 
相關問題