【问题标题】:If while statements within if loop on WordpressWordpress 上的 if 循环中的 if while 语句
【发布时间】:2014-02-22 19:42:53
【问题描述】:

我已经编写了一些基本的 PHP 来获取特定类别中任何帖子的永久链接和标题。这是为页脚编写的,效果很好。

我现在的问题是,当我尝试在 if 循环中使用相同的代码时,它会失败。

我应该做些什么不同的事情?

这是我的代码:

<?php if (in_category('training'))
{

<?php query_posts('category_name=training'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

}

elseif (in_category('club'))

{

<?php query_posts('category_name=club'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

}

else

{

echo "Nothing to show";

}

 ?>

【问题讨论】:

  • 你能说出你遇到了什么错误吗?
  • 不幸的是没有错误。循环下方的内容无法加载。
  • 您是否尝试过在设置中使用 wp_debug true 来查看是否出现任何错误?或者,如果您在网络服务器错误日志中收到任何错误?
  • 我没有。对不起,这一切对我来说还是很新的。解析错误:语法错误,第 4 行 /Applications/MAMP/htdocs/thames_new/wp-content/themes/expound/side-bar.php 中的意外“

标签: php wordpress if-statement while-loop


【解决方案1】:
Please use this

<?php if (in_category('training'))
{

 query_posts('category_name=training'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; 

}

elseif (in_category('club'))

{
query_posts('category_name=club'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; 
}

else

{

echo "Nothing to show";

}

 ?>

【讨论】:

  • 谢谢 :-) 这已经解决了最初的问题,尽管现在该类别中的每个帖子都显示在页面上。有什么想法吗?
  • 您可以在同一页面中使用一个或多个查询。使用 循环之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2014-01-21
相关资源
最近更新 更多