我被迫与wordpress合作,如果你使用它,你可能知道我的意思:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>它起作用了,毫无疑问。但我不明白这究竟是甚麽意思。它不是三元操作符,也不是我知道的任何东西。在我从事的任何php项目中,我从未见过这样的声明。所以我有几个问题:
the_post()在做什么?这些双点点在做什么?我已经搜索过它,但是没有关于我的问题的信息,似乎没有人对wordpress的工作方式感兴趣。是的,但我不明白。如果有人能给我解释的话,那就太好了。
发布于 2014-02-11 13:14:04
<?php define('WP_USE_THEMES', false); get_header(); ?>
The loop starts here:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
This is using PHP's alternative syntax for control structures, and could also be expressed as:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>发布于 2014-02-11 13:18:33
post()
此函数不接受任何参数。
此函数不返回任何值。
<?php
while ( have_posts() ) : the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
endwhile;
?>have_posts()参数此函数不接受任何参数。
返回值(布尔值):成功为真,失败为假。下面的示例可用于确定是否存在任何帖子,如果存在,则循环遍历这些帖子。
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>注在循环中调用此函数将导致无限循环。例如,请参见以下代码:
<?php
while ( have_posts() ): the_post();
// Display post
if ( have_posts() ): // If this is the last post, the loop will start over
// Do something if this isn't the last post
endif;
endwhile;
?>如果您想要检查当前循环中是否有更多的帖子而没有这种不幸的副作用,您可以使用此函数。
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}发布于 2014-02-11 13:53:16
1.什么是循环?
循环是WordPress用来显示posts的PHP代码。使用循环,WordPress处理显示在当前页面上的每个帖子,并根据其与循环标记中指定的条件匹配的方式对其进行格式化。
它将获取与特定页面相关的数据。
:(冒号)用于告诉从这里开始的条件/循环。可以用{}(括号引号)替换它。
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>2.这是只使用Wordpress,还是还可以在其他地方使用?
是的你当然可以用。您可以通过包含一个名为"wp-blog-header.php"的核心文件来访问完整的wordpress功能,该文件位于wordpress目录的根目录上。
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>将此文件包含在外部文件的顶部,您可以访问wordpress数据库、wordpress函数、wordpress钩子。
3.当前的帖子存储在哪里?
wordpress数据库中存在11个默认表。您可以在数据库中看到wp_posts表。所有职位都存储在本表中。
假设,如果您在帖子中创建元标记,它将存储在wp_postmeta中。
https://stackoverflow.com/questions/21702679
复制相似问题