Wordpress PHP 语法循环很奇怪
Wordpress PHP Syntax Loop is strange
我无法理解这段代码。我了解 'wordpress-loop' 的基础知识,但我不明白其中的一些语法。它几乎看起来像一个三元运算符。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
特别是这一行..
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
它说 OK 如果有 posts,而我们有 posts,显示 post?但是 : 是什么意思。
PHP offers an alternative syntax for some of its control structures;
namely, if, while, for, foreach, and switch. In each case, the basic
form of the alternate syntax is to change the opening brace to a colon
(:) and the closing brace to endif;, endwhile;, endfor;, endforeach;,
or endswitch;, respectively.
示例:
if (condition) :
endif;
等同于:
if (condition) {
}
具体来说,WordPress 说:如果我们有 posts,遍历每个 post,并得到 post.
这里有一个缩进版本要清楚:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
http://php.net/manual/en/control-structures.alternative-syntax.php
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
我无法理解这段代码。我了解 'wordpress-loop' 的基础知识,但我不明白其中的一些语法。它几乎看起来像一个三元运算符。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
特别是这一行..
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
它说 OK 如果有 posts,而我们有 posts,显示 post?但是 : 是什么意思。
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
示例:
if (condition) :
endif;
等同于:
if (condition) {
}
具体来说,WordPress 说:如果我们有 posts,遍历每个 post,并得到 post.
这里有一个缩进版本要清楚:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
http://php.net/manual/en/control-structures.alternative-syntax.php
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.