【问题标题】:Show if greater than 3 related articles within same category in wordpress显示 wordpress 中同一类别中是否有超过 3 篇相关文章
【发布时间】:2012-05-06 18:19:35
【问题描述】:

我似乎遇到了困难。 如果 wordpress 的帖子页面模板上有超过或等于 3 个帖子,我需要显示一段特定的文本。 (loop-single.php)

它应该足够动态地检测相关类别的帖子总数是否大于或等于 3。

这是我发现的一个代码,它在类别模板页面(archive.php)上运行良好,但是当我在帖子模板中使用它时它会搞砸。

<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>

<!-- Less than 3 post - nothing shown at all -->

<?php $count++;
  endwhile; endif; ?>
<?php if ($count > '3') { ?>

<div> This line shown when 3 or more posts are in current post category</div>

<?php } ?>

注意:我试图让它在 loop-single.php 模板文件上工作。

任何帮助将不胜感激, 谢谢你


已更新代码以包含上述解决方案。我修复了一些语法错误,但它现在抛出 T-STRING 错误:解析错误:语法错误,意外 T_STRING

这是我的完整页面代码:

<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php roots_post_before(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php roots_post_inside_before(); ?>
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>

<!-- POST DATE STAMP -->
<div class="post-top">
<div class="date-stamp">
<b><?php the_time('M d'); ?></b>
</div>
</header>

<div class="entry-content">
<?php the_content(); ?>
</div>

<footer>
<hr />


<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{

<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) --> 
<h5>Featured: <?php $cats=get_the_category(); echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p      comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />

}
else
{
Why hello there LESS than three
}
?>



</footer>
<?php comments_template(); ?>
<?php roots_post_inside_after(); ?>
</article>
<?php roots_post_after(); ?>
<?php endwhile; /* End loop */ ?>

【问题讨论】:

  • 为了清楚起见,您是否要在单个帖子上生成此文本(即:single.php)?还是生成多个帖子的页面?
  • 抱歉刚刚更新了上面的内容,包括它是 single-loop.php 模板文件。

标签: php wordpress conditional-operator conditional-statements


【解决方案1】:

这应该让你开始:

<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{
    //CODE EXECUTED IF THREE OR MORE POSTS EXIST IN CURRENT CATEGORY
}
else
{
    //CODE EXECUTED IF LESS THAN THREE POSTS EXIST IN CURRENT CATEGORY
}
?>

额外信息:失败的原因是您的循环只执行了一次迭代。单个帖子不会多次循环,因为......嗯......这是一个帖子。这种方法的作用是获取现有类别,并查询匹配类别中的所有 Wordpress 帖子。使用 PHP 的 count 函数将为您提供使用给定参数找到的确切帖子数。

警告:上面的脚本不会找到匹配类别中的所有帖子。只有该给定类别中的五个最近的。如果您想要所有匹配帖子的实际总数,请将一行更改为以下内容:

$posts = get_posts(array('category' => $cat, 'numberposts' => -1));

代码更新:这一行:

<article <?php post_class(); ?> id="post-<?php the_ID(); ?>"> //missing semicolon after post_class()

还有这个块:

<?php
$cats=get_the_category();
$posts = get_posts(array('category' => $cats[0]->cat_ID));
if(count($posts) >= 3)
{
?> 
<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) --> 
<h5>Featured: <?php echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />
<?php
}
else
{
echo 'Why hello there LESS than three';
}
?>

【讨论】:

  • hmmm 在 loop-single.php 中实现上述代码后,我现在尝试查看任何帖子时似乎遇到了错误。错误是 Parse error: syntax error, unexpected '
  • 查看代码更改。您错过了我在 cmets 中指出的分号,并且您试图在 PHP 代码块中生成 HTML。让我知道这是否涵盖了您的所有错误。如果没有,请发布您可能看到的任何其他内容。
  • 我合并了上述更改。我注意到的唯一一件事是它的数学运算不正确。当一个类别中有 2 个或只有 1 个帖子时,将限制设置为 >=3 似乎没有什么不同。现在,当我将其更改为 >=6 时,无论类别中是否有 6 个帖子,它都会在所有帖子上显示“为什么你好,少于三个”。
  • 我也替换了上面的行,正如你之前提到的,但没有骰子。同样的行为。
  • 我在更新的代码块中提供了一个小修订。立即尝试。
猜你喜欢
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2011-07-13
相关资源
最近更新 更多