Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示wordpress中同一类别中的相关文章是否超过3篇_Php_Wordpress_Conditional Operator_Conditional Statements - Fatal编程技术网

Php 显示wordpress中同一类别中的相关文章是否超过3篇

Php 显示wordpress中同一类别中的相关文章是否超过3篇,php,wordpress,conditional-operator,conditional-statements,Php,Wordpress,Conditional Operator,Conditional Statements,我似乎有困难。 我需要显示一个特定的文本块,如果有超过或等于3后在一个网页模板在wordpress。(loop single.php) 它应该是动态的,足以检测相关类别中的职位总数是否大于或等于3 下面是我发现的一段代码,它在分类模板页面(archive.php)上运行良好,但在帖子模板中使用它时会出错 <?php $count = 1; if (have_posts()) : while(have_posts()): the_post(); ?> <!-- Less tha

我似乎有困难。 我需要显示一个特定的文本块,如果有超过或等于3后在一个网页模板在wordpress。(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 } ?>

当前职位类别中有3个或更多职位时显示此行
注意:我正试图在loop-single.php模板文件中实现这一点

任何帮助都将不胜感激, 多谢各位


更新代码以包含上述解决方案。我修复了一些语法错误,但它现在抛出了一个T字符串错误:解析错误:语法错误,意外的T_字符串

以下是我的完整页面代码:

<?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 */ ?>



} 其他的 { 为什么不到三个 } ?>
这应该让您开始:

<?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
}
?>
代码更新:此行:

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

作为特色的:


说清楚一点,您是否试图在一篇文章(即:single.php)中生成此文本?或者是一个生成多篇文章的页面?很抱歉,刚刚添加了上面的内容,其中包括single-loop.php模板文件。hmmm在loop-single.php中实现了上述代码后,我现在尝试查看任何文章时似乎遇到了错误。错误是解析错误:语法错误,意外“请检查代码更改”。您缺少了我在注释中指出的分号,您试图在PHP代码块中生成HTML。如果这涵盖了你所有的错误,请告诉我。如果没有,请发布您可能看到的任何其他内容。我已将上述更改合并。我注意到的唯一一件事是它没有做正确的数学运算。当一个类别中有2篇或只有1篇文章时,将限制设置为>=3似乎没有什么区别。现在,当我将其更改为>=6时,它会在所有帖子上显示“为什么hello少于3篇”,而不管类别中是否有6篇帖子。我还替换了前面提到的行,但没有骰子。同样的行为。我在更新的代码块中提供了一个小的修改。现在试试看。
<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';
}
?>