2012-12-18 84 views
0

我希望在我的WordPress主頁index.php中插入一行代碼,它將顯示除「日記」類別中的帖子之外的所有帖子。根據類別顯示所有帖子的PHP代碼

的代碼原有路線如下

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?> 
    <div <?php post_class(); ?>> 
    <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span> 
     <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
      <p class="info"> 
       <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span> 
       <span class="cmtinfo"><?php comments_popup_link(__('Leave a comment', 'Mflat'), __('1 Comment', 'Mflat'), __('% Comments', 'Mflat')); ?><?php edit_post_link(' Edit', ' &#124; ', ''); ?></span> 
      </p> 
     <div class="entry"> 
      <?php $Mflat_options = get_option('Mflat_theme_options'); ?> 
      <?php if ($Mflat_options['home_excerpt_check']== 1) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?> 
     </div> 
     <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 
      <p class="infobottom"> 
       <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?> 
       <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?> 
       </p> 
    </div> 
     <?php endwhile; ?> 
    <div class="flip"> 
     <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div> 
     <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div> 
    </div> 
<?php else : ?> 
<div id="main"> 
<h2><?php _e('Not Found', 'Mflat'); ?></h2> 
</div> 
    <?php endif; ?> 

我嘗試了以下代碼,但無濟於事

<?php if(have_posts()) : ?><?php while(have_posts()){ 
    <if(!in_category('Diary')){ 
     : the_post(); 
     } 
    }?> 

有沒有什麼更好的方法,我可以完成這件事? 我是PHP的新手,雖然我知道必須完成的邏輯,但我無法完全生成代碼來完成這項工作。我認爲布爾值會有點太多工作,並且發現一個類似的嚴重溢出的類似(http://stackoverflow.com/questions/13587958/display-recent-posts-based-on-their-category-in-wordpress)線程,但是它不適合我的需求。

每一個貢獻都深受讚賞。由於

回答

1

循環之前剛剛運行query_posts()功能 - 它改變了循環

<?php 
query_posts(array(
    'category_name' => 'my-category-slug', // get posts by category name 
    'posts_per_page' => -1 // all posts 
)); 
?> 

<?php while(have_posts()): the_post(); ?> 
    <h1><?php the_title(); ?></h1> 

    <?php the_content(); ?> 
<?php endwhile; ?> 

我不好,我只注意到你想排除類別。同樣的功能,不同的參數:

$category = get_term_by('name', $cat_name, 'category'); // get category 
$id = $category->term_id; // get the category ID 

query_posts('cat=-' . $id); // exclude the found ID 

<?php 
$category = get_term_by('name', 'Diary', 'category'); // get category data 
$id = $category->term_id; // get the category ID 

// USE THIS IF YOU WANT TO _EXCLUDE_ ALL POSTS FROM "DIARY" 
query_posts($query_string . '&cat=-' . $id); 

// USE THIS IF YOU WANT TO GET ALL POSTS FROM "DIARY" ONLY 
// query_posts($query_string . '&cat=' . $id); 
?> 

<?php if(have_posts()) : ?> 
    <?php while(have_posts()) : the_post(); ?> 
     <div <?php post_class(); ?>> 
      <span class="postlabel"><a href="<?php the_permalink() ?>"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></a></span> 

      <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 

      <p class="info"> 
       <span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span> 
       <span class="cmtinfo"> 
        <?php comments_popup_link(__('Leave a comment', 'Mflat'), __('1 Comment', 'Mflat'), __('% Comments', 'Mflat')); ?> 
        <?php edit_post_link(' Edit', ' &#124; ', ''); ?> 
       </span> 
      </p> 

      <div class="entry"> 
       <?php $Mflat_options = get_option('Mflat_theme_options'); ?> 
       <?php if ($Mflat_options['home_excerpt_check']== 1) { the_excerpt(__('&raquo; Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?> 
      </div> 

      <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 

      <p class="infobottom"> 
       <?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?> 
       <?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?> 
      </p> 
     </div> 
    <?php endwhile; ?> 

    <div class="flip"> 
     <div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div> 
     <div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div> 
    </div> 
<?php else : ?> 
    <div id="main"> 
     <h2><?php _e('Not Found', 'Mflat'); ?></h2> 
    </div> 
<?php endif; ?> 
+0

如何落在這個崗位分離成「日記」分類的?這是否意味着代碼看起來像這樣? <?php query_posts(array( 'category_name'=>'Diary',//通過類別名稱獲得帖子 'posts_per_page'=> -1 //所有帖子 )); ?> – Selase

+0

@Selase請查看我編輯的答案;) –

+0

@ Tim:謝謝。我試過了,它和我想要的完全相反。在這種情況下,它只顯示'日記'作爲類別而不是其他回合 – Selase

相關問題