2010-04-23 186 views
0

我已經創建了一個自定義頁面,並將其設置爲我的主頁,在此自定義頁面內我從特定類別中提取最新帖子,還創建了一種分頁形式,當點擊該分頁時會將用戶到single.php。我對single.php的意圖是有兩個自定義循環。如何通過Wordpress中single.php的特定類別進行循環?

自一環 我想single.php中來區分它來自於主頁,並遍歷所有標有同一類別的主頁上的一個帖子的。

其中一些帖子將被標記爲多個類別,因此循環必須知道忽略其他類別並只關注所討論的類別。那有意義嗎?

定製循環二 如果用戶沒有從主頁趕到,將single.php中只是充當它一般不會也就是說,如果用戶來自index.php文件(該博客),他們將被帶到這第二個循環(博客文章)

但是,我似乎無法區分兩個循環,我可能會過度複雜的事情,因爲我有一個循環,將所有的東西包裝在一起,然後我我的自定義分頁有一個循環。

這裏是下面的代碼,告訴你什麼是我說的

custompage.php(設爲首頁) - 這工作得很好,但我會後它只是櫃面任何人都能夠整理它

<?php query_posts('cat=1'); ?> 

<?php 
$myPosts = new WP_Query(); 
$myPosts->query('showposts=1'); 

if (have_posts()) : 
while ($myPosts->have_posts()) : $myPosts->the_post(); 
?> 

<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script> 
<div id="post-<?php the_ID(); ?>" class="info"> 
     <h2><?php the_title(); ?></h2> 
     <ul class="nav"> 
      <?php query_posts('posts_per_page=1&offset=1'); the_post(); ?> 
      <li class="prev"><a href="<?php the_permalink() ?>">Previous</a></li> 
      <?php wp_reset_query(); ?> 
      <li class="next"></li> 
     </ul> 
</div> 
<!-- end .info --> 

<?php endwhile; endif; ?> 
<?php wp_reset_query(); ?> 

的single.php - 目前破

<?php if(in_category('1')) { ?> 
    <!-- start --> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div id="post-<?php the_ID(); ?>" class="info"> 
    <script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script> 
      <h2><?php the_title(); ?></h2> 
      <ul class="nav"> 
       <li class="prev"><?php previous_post_link('%link', '&nbsp;', 'true', '1') ?></li> 
       <li class="next"><?php next_post_link('%link', '&nbsp;', 'true', '1'); ?></li> 
       <!--li class="prev"><?php //previous_post_link('%link', '%title;', 'true', '1') ?></li> 
       <li class="next"><?php //next_post_link('%link', '%title;', 'true', '1'); ?></li--> 
      </ul> 
    </div> 
    <!-- end .info --> 
    <?php endwhile; else: ?> 
    <?php endif; ?> 
    <!-- end --> 

<?php }else{ ?> 

    <div id="content" class="widecolumn" role="main"> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
     <h2><?php the_title(); ?></h2> 
     <div class="entry"> 
      <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?> 
     </div> 
     </div> 
    <?php comments_template(); ?> 
    <?php endwhile; else: ?> 
     <p>Sorry, no posts matched your criteria.</p> 
    <?php endif; ?> 
    </div> 

<?php } ?> 

的proble我似乎碰到的是,當一個帖子被標記爲兩個類別時,wordpress似乎無法區分這兩個類別,而是繼續進入下一個類別,並且默認爲第二個類別循環。

+0

我很樂意看到這個問題的答案。我有過類似的鬥爭。 – 2010-04-23 18:03:55

+0

嗨,Petrescu,歡迎來到StackOverflow!在撰寫問題時,不需要在標題中重複標籤(如[wordpress]) - 最好放置在「標籤」字段中。這樣,問題更容易閱讀,並且如果它很容易閱讀,則更有可能得到答案。 – MaxVT 2010-04-23 18:15:33

+0

感謝您修復我的文章標題最大。 – petrescu 2010-04-23 18:24:53

回答

0

這個怎麼樣?

HOMEPAGE 
<?php 
$myPosts = new WP_Query(); 
$myPosts->query('showposts=1&cat=1'); 

if (have_posts()) : 
while ($myPosts->have_posts()) : $myPosts->the_post(); 
?> 

<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script> 
<div id="post-<?php the_ID(); ?>" class="info"> 
     <h2><?php the_title(); ?></h2> 
     <ul class="nav"> 
      <?php query_posts('posts_per_page=1&offset=1'); the_post(); ?> 
      <li class="prev"><a href="<?php the_permalink() ?>">Previous</a></li> 
      <?php wp_reset_query(); ?> <-- not sure if this will reset the overall query 
      <li class="next"></li> 
     </ul> 
</div>  
<?php endwhile; endif; ?> 
<?php wp_reset_query(); ?> 

的queryposts在查詢不會與該類別運行的頂部(「貓= 1」),類別會對您的自定義查詢

$myPosts->query('showposts=1&cat=1'); 

第2頁(裏面設置SINGLE.PHP)與2個循環.. 如果用戶從主頁來到一個頁面,你想附加當前類別的查詢,沒有這個wordpress(而在單一頁面將默認循環通過所有帖子)

因此對於單曲頁面會在下面有什麼好處?

<?php if(in_category('1')) { ?> 
<!-- your selected category --> 
    <!-- start --> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div id="post-<?php the_ID(); ?>" class="info"> 
    <script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script> 
      <h2><?php the_title(); ?></h2> 
      <ul> 
      <php global $post; 
       $my_query = get_posts('numberposts=1&cat=1&offset=-1'); 
       foreach($my_query as $post) : 
      setup_postdata($post); 
      $link = get_post_meta($post->ID, 'site-url', true); ?> 
    <li> 
       <a href="<php echo $link; ?>"><php the_title(); ?></a> 
      </li> 
      <php endforeach; ?> 
      <php global $post; 
       $my_other_query = get_posts('numberposts=1&cat=1&offset=1'); 
       foreach($my_other_query as $post) : 
      setup_postdata($post); 
      $link = get_post_meta($post->ID, 'site-url', true); ?> 
    <li> 
       <a href="<php echo $link; ?>"><php the_title(); ?></a> 
      </li> 
      <php endforeach; ?> 
      </ul> 

    </div> 
    <!-- end .info --> 
    <?php endwhile; else: ?> 
    <?php endif; ?> 
    <!-- end --> 

<?php }else{ 
    include('standard-wp-singles-page-stuff.php'); 
} ?> 

那麼一旦你的單打頁面加載,將在該職位檢查當前的類別,如果在第1類,那麼它會加載自定義的循環,那麼它會通過2個職位循環,在同一類別?給你2個鏈接到其他職位。也使用該查詢偏移應該給你一個鏈接向前和鏈接倒退?

希望幫助..

固定代碼佈局..沒有工作太清楚:P

+0

只是似乎迴應當前頁面標題,在href中沒有網址。 我不確定循環本身是不正確的,還是隻是這個ul。 – petrescu 2010-04-26 19:47:54

+0

對不起m8 ....也許其他一些WordPress的大師可以看看這一個.. :) – Marty 2010-04-27 13:15:09