如何在 WordPress 中循环访问 single.php 上的特定类别?

发布于 2024-08-29 20:55:59 字数 3370 浏览 2 评论 0原文

我创建了一个自定义页面,并将其设置为我的主页,在这个自定义页面中,我从特定类别中提取最新的帖子,我还创建了一种分页形式,单击该形式将使用户进入单一页面.php。我对 single.php 的意图是有两个自定义循环。

自定义循环一 我希望 single.php 能够区分它来自主页,并循环遍历所有标记为与主页上的类别相同的帖子。

其中一些帖子必须标记多个类别,因此循环必须知道忽略其他类别,而只关注有问题的类别。这有道理吗?

自定义循环二 如果用户没有从主页到达,single.php将像平常一样运行,即,如果用户来自index.php(博客),他们将被带到第二个循环(博客文章)

但是我不这样做似乎无法区分两个循环,我可能会让事情变得过于复杂,因为我有一个将所有内容包装在一起的循环,然后我有一个用于自定义分页的循环。

下面的代码向您展示我正在谈论的内容

custompage.php(设置为 home) - 这工作得很好,但我会发布它,以防有人能够整理它

<?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(); ?>

< strong>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 似乎无法区分这两个类别,而不是继续到下一个类别,它会中断并默认到第二个循环。

I've created a custom page and it is set as my homepage, within this custom page I am pulling out the latest post from a specific category, I've also created a form of pagination which when clicked upon will take the user to single.php. My intention for the single.php is to have two custom loops.

Custom loop one
I want single.php to distinguish that it has came from the homepage and loop through all of the posts tagged with the same category as the one on the homepage.

Some of these posts will have to be tagged with more than one category, so the loop will have to know to ignore the other categories and just pay attention to the category in question. Does that make sense?

Custom loop two
If the user hasn't arrived from the homepage, single.php will just act as it normally does i.e, if the user comes from index.php (the blog) they will be taken to this second loop (blog post)

However I don't seem to be able to make the distinction between the two loops, I might be over complicating matters, as I've got a loop which wraps everything together and then I have a loop for my custom pagination.

Here is the code below to show you what I'm talking about

custompage.php (set to home) - This works just fine but I'll post it just incase anyone is able to tidy it up

<?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 - Currently broken

<?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 } ?>

The problem I seem to be running into is when a post has been tagged with two categories, wordpress doesn't seem to be able to make the distinction between the two categories and instead of carrying on to the next category it breaks and defaults to the second loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2024-09-05 20:55:59

这个怎么样?

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

how about this?

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(); ?>

the queryposts('cat=1') at the top of your query wont run with that category, the category would have to be set inside your custom query

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

the 2nd page (SINGLE.PHP) with 2 loops..
if the user is coming from the homepage to a single page you want to attach the current category to the query, without this wordpress (while on the singles page will default to loop through all posts)

so for the singles page would the below be any good?

<?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');
} ?>

then once your singles page loads it will check for the current category in that post, if its in category 1, then it will load your custom loop, then it will loop through 2 posts, in the same category? giving you 2 links to other posts. also using an offset on that query should give you a link forward and a link backwards?

hope that helps..

fixing code layout.. didnt work too well :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文