主循环

The Loop should be placed in the Theme’s index.php and in any other Templates used to display post information.

Be sure to include the call for the header template at the top of your Theme’s templates. If you are using The Loop inside your own design (and your own design is not a template), set WP_USE_THEMES to false:

1
<?php define( 'WP_USE_THEMES', false ); get_header(); ?>

The loop starts here:

1
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

and ends here:

1
2
3
<?php endwhile; else : ?>
<p><?php esc_html_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:

1
2
3
4
5
6
7
8
9
10
<?php 
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>

元数据

API

添加元数据 获得元数据 更新元数据 删除元数据
文章 add_post_meta() get_post_meta() update_post_meta() delete_post_meta()
用户 add_user_meta() get_user_meta() update_user_meta() delete_user_meta()
评论 add_comment_meta() get_comment_meta() update_comment_meta() delete_comment_meta()
参数 $post_id, $meta_key, $meta_value, $unique (可选的) $post_id, $meta_key, $single (可选的) $post_id, $meta_key, $meta_value, $prev_value (可选的) $post_id, $meta_key, $meta_value (可选的)

postmeta

语法:

1
2
3

<?php $meta_name = get_post_meta($post->ID, $key, $single); ?>

此方法返回一个数组,如果single设置为true则返回数组中的第一项

postmeta元数据储存在数据库中的wp_postmeta中

(mixed) Single metadata value, or array of values. False if there’s a problem with the parameters passed to the function.


常用函数

  • edit_post_link('Edit', ' | ', '')
    管理员编辑

  • the_category(',')
    分类,使用,分隔

  • 分页导航

    1
    2
    3
    4
    5
    the_posts_pagination(array(
    'mid_size' => 5,
    'prev_text' => __('上一页'),
    'next_text' => __('下一页'),
    ));
  • wp_list_cats()
    调用分类链接列表