【问题标题】:How to loop through a specific category on single.php in Wordpress?如何在 Wordpress 中遍历 single.php 上的特定类别?
【发布时间】:2010-04-23 17:58:44
【问题描述】:

我创建了一个自定义页面,并将其设置为我的主页,在此自定义页面中,我从特定类别中提取最新帖子,我还创建了一种分页形式,点击时将采用用户到 single.php。我对 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 } ?>

我似乎遇到的问题是,当帖子被标记为两个类别时,wordpress 似乎无法区分这两个类别,而是继续下一个类别,它打破了默认为第二个循环。

【问题讨论】:

  • 我很想看到这个问题的答案。我也有过非常相似的挣扎。
  • 嗨 Petrescu,欢迎来到 StackOverflow!编写问题时,无需在标题中重复标签(如 [wordpress])——最好放在“标签”字段中。这样问题就更容易阅读,而且如果阅读起来容易,则更有可能得到答案。
  • 感谢您修复我的帖子标题 Max。
  • 我遇到了类似的问题并想出了这个:stackoverflow.com/questions/13946468/…

标签: wordpress pagination categories


【解决方案1】:

这个怎么样?

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('cat=1') 不会与该类别一起运行,必须在您的自定义查询中设置该类别

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

带有 2 个循环的第二页 (SINGLE.PHP).. 如果用户从主页来到单个页面,您希望将当前类别附加到查询中,没有这个 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

【讨论】:

  • 似乎只是在呼应当前页面标题,在 href 中没有 url。我不确定循环本身是否不正确,或者只是这个 ul。
猜你喜欢
  • 2015-07-22
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多