2010-04-23 191 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