-- 创造无限可能

wordpress开发:文章相关函数

2022-05-18 07:44:23
378 人浏览 1 人点赞
有用,点赞支持一下

相关数据库

  • 网站的内容(文章、产品等)都是保存在posts和postmeta数据表
  • posts表为通用数据表
  • postmeta为自定义数据表

内容相关函数

query_posts();//查询内容
// 检查是否存在Post/Page
if(have_posts()){
    while(have_posts()){
        the_post();// 如果存在Post/Page则予以显示
        the_meta(); // 获取自定义字段列表
    } 
} 
the_title();// 标题
the_permalink()// Url
the_category(',')// 所属Category
the_author();// 作者
the_ID();// 内容ID
the_time('时间格式')// 显示时间,时间格式由“字符串”参数决定,具体参考PHP手册
the_content();// 显示内容(Post/Page)
comments_popup_link();// 正文中的留言链接。如果使用 comments_popup_script() ,则留言会在新窗口中打开,反之,则在当前窗口打开

edit_post_link();// 如果用户已登录并具有权限,显示编辑链接
get_links_list();// 显示Blogroll中的链接
comments_template();// 调用留言/回复模板
wp_list_pages();// 显示Page列表
wp_list_categories();// 显示Categories列表
next_post_link(' %link ');// 下一篇文章链接
previous_post_link('%link');// 上一篇文章链接
get_calendar();// 日历
wp_get_archives('type=postbypost&limit=20')// 显示内容存档
posts_nav_link() // 导航,显示上一篇/下一篇文章链接
get_archives('内容类型', 10);//显示10篇最新更新文章
get_post_meta(); // 根据文章id获取自定义的字段值,默认获取多个
get_post_thumbnail_id() //根据文章id获取文章的缩略图id列表
wp_get_attachment_image_src( , "Full");//根据缩略图id获取缩略图地址
get_the_post_thumbnail() //获取文章的缩略图
the_post_thumbnail() // 当前文章的缩略图
has_post_thumbnail() // 判读是否有缩略图
the_posts_pagination() // 文章上下篇
get_post_type() // 获取内容的类型
get_the_content() // 获取文章内容
get_the_author_meta() // 获取作者的自定义信息
get_author_posts_url() // 获取作者的头像地址
get_categories() // 获取内容分类列表
get_category_link() //获取分类链接地址
get_query_var() //获取请求参数

调用相关文章

// 获取当前文章的标签
$tags = wp_get_post_tags($post->ID);

if ($tags) {
    $first_tag = $tags[0]->term_id;
    $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>10,
        'caller_get_posts'=>1
    );
    // 根据标签id查询文章
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()){
            $my_query->the_post();
            the_title();
            the_title_attribute();
        }
    }

}
// 重置查询
wp_reset_query();

?>

调用指定分类的文章

$posts = get_posts('category=4&numberposts=10');
if( $posts ){
    foreach( $posts as $post ){
        setup_postdata( $post );
        the_permalink();
        the_title();
    }
}

自定义内容类型

为了方便管理,我们可以自定义数据类型

自定义内容类型

function create_movie_review() {
    register_post_type( 'movie_reviews',
        array(
            'labels' => array(
                'name' => 'Movie Reviews',
                'singular_name' => 'Movie Review',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Movie Review',
                'edit' => 'Edit',
                'edit_item' => 'Edit Movie Review',
                'new_item' => 'New Movie Review',
                'view' => 'View',
                'view_item' => 'View Movie Review',
                'search_items' => 'Search Movie Reviews',
                'not_found' => 'No Movie Reviews found',
                'not_found_in_trash' => 'No Movie Reviews found in Trash',
                'parent' => 'Parent Movie Review'
            ),

            'public' => true, //是否可见
            'menu_position' => 15,//自定义文章类型菜单的位置
            'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),//显示的字段
            'taxonomies' => array( '' ),//自定义分类
            'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),//管理菜单的图标
            'has_archive' => true//启用自定义文章类型的存档功能
        )
    );
}
add_action( 'init', 'create_movie_review' );

根据自定义属性获取文章

  1. 根据单个自定义属性查询内容
    // 自定义的属性都是保存在postmeta表内,我们可以利用`meta_ky`和`meta_value`查询自定义的数据
    $rd_args = array(
     'meta_key' => 'key',
     'meta_value' => 'value',
     'meta_compare'=>'='//默认是等号,可以不用,'=','!=','<','<=','>','>=','like','not like','in','not in','between','not between','REGEXP','EXISTS','NOT EXISTS'
    );
    $rd_query = new WP_Query( $rd_args );
    
  2. 查询属性值是否在限定数组内
    $rd_args = array(
     'meta_key' => 'key',
     'meta_value' => ['value1','value2','value3'],
     'meta_compare'=>'IN' //NOT IN
    );
    $rd_query = new WP_Query( $rd_args );
    
  3. 根据多个自定义属性查询内容
    $rd_args = [
     'meta_query' => [
         'relation' => 'AND', // 以下条件两者都必须匹配,默认为‘and’
         [
             'key' => 'key1',
             'value' => 'value1'
         ],
         [
             'key' => 'key2',
             'value' => 'value2'
         ]
     ]
    ];
    $rd_query = new WP_Query( $rd_args );
    
  4. 根据符合条件查询查询内容
    $rd_args = [
     'meta_query' => [
         'relation' => 'AND', // 以下条件两者都必须匹配
         [
             'key' => 'key1',
             'value' => 'value1'
         ],
         [
             'relation' => 'or',//嵌套查询条件
             [
                 'key' => 'key1',
                 'value' => 'value1'
             ],
             [
                 'key' => 'key2',
                 'value' => 'value2'
             ]
         ]
     ]
    ];
    $rd_query = new WP_Query( $rd_args );
    
  5. 查询排序
    $args = array(
     'orderby' => 'title',
     'order' => 'ASC'
    );
    $rd_query = new WP_Query( $args );
    //orderby取值
    
  6. 多个属性排序
    $args = array(
     'orderby' => array(
         'type',
         'date'
     ),
     'order' => array(
         'ASC',
         'DESC'
     )
    );
    $rd_query = new WP_Query( $args );
    
  7. orderby取值
  • ID: post id
  • author: 作者
  • title: 标题
  • name: post slug.
  • type: post type.
  • date: date,默认值
  • modified: 最后修改时间
  • parent: parent id.
  • rand: 随机
  • comment_count: 评论数
  • menu_order: Order by Page Order. Used most often for Pages (using the value you add to the metabox in the Edit Page screen) and for Attachments (using the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with menu_order enabled.
    meta_value: Sort by the value for a meta key (or custom field). This only works if you also include ameta_key parameter in your arguments. Meta values are sorted alphabetically and not numerically (so 34 will come before 4, for example).
  • meta_value_num: Order by numeric meta value. As with meta_value, you must also include a meta_keyargument in your query.
  • postin: Preserve post ID order given in the postin array.

WP_query相关文章

https://www.wpdaxue.com/mastering-wp_query-an-introduction.html