我已經創建了一個自定義頁面,並將其設置爲我的主頁,在此自定義頁面內我從特定類別中提取最新帖子,還創建了一種分頁形式,當點擊該分頁時會將用戶到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', ' ', 'true', '1') ?></li>
<li class="next"><?php next_post_link('%link', ' ', '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 »</p>'); ?>
</div>
</div>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
<?php } ?>
的proble我似乎碰到的是,當一個帖子被標記爲兩個類別時,wordpress似乎無法區分這兩個類別,而是繼續進入下一個類別,並且默認爲第二個類別循環。
我很樂意看到這個問題的答案。我有過類似的鬥爭。 – 2010-04-23 18:03:55
嗨,Petrescu,歡迎來到StackOverflow!在撰寫問題時,不需要在標題中重複標籤(如[wordpress]) - 最好放置在「標籤」字段中。這樣,問題更容易閱讀,並且如果它很容易閱讀,則更有可能得到答案。 – MaxVT 2010-04-23 18:15:33
感謝您修復我的文章標題最大。 – petrescu 2010-04-23 18:24:53