What does this simple php code do?
I was browsing a WordPress site, and spotted this line
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>
<li id="recent-posts">
<ul>
<?php get_archiv开发者_如何转开发es('postbypost', 5); ?>
<ul>
</li>
<?php endif; ?>
What do the colon before and after else do exactly? How does this thing work?
That function will only execute dynamic_sidebar
if it's already declared. The colons are PHP's alternate syntax for control structures. They're meant to be used in templates/views.
In this case, it looks like the if
has an empty body and it's only used to call dyanamic_sidebar
if it exists, since the call to dynamic_sidebar(1)
will not occur if the first boolean check fails.
else
will output anything between itself and the <?php endif; ?>
. In this case, it would fire when the function dynamic_sidebar
does not exist or if dyanmic_sidebar(1)
does not return true
.
It is an alternative Syntax for control structure.
It means:
<?php
if (function_exists('dynamic_sidebar') && dynamic_sidebar(1)) {
} else {
?>
<li id="recent-posts">
<ul>
<?php get_archives('postbypost', 5); ?>
<ul>
</li>
<?php
}
?>
The dynamic_sidebar function in Wordpress, when called, will display the sidebar with the id of the number that is passed in (in this case, it is one). The snippet of code will print out that sidebar (if it exists, and the function dynamic_sidebar is defined), otherwise, it will print out whatever is below the code you posted all the way up to the line in the place of the sidebar.
精彩评论