美文网首页
自定义内容类型

自定义内容类型

作者: 翌子涵 | 来源:发表于2016-11-11 21:01 被阅读62次

    内容类型 - Custom Post Types

    1.自定义内容类型

    • 文章:posts
    • 页面:pages
      自定义文章类型帮助我们将不同类型的文章放在不同的容器中,它可以将我们的文章和其他的区别开。

    register_post_type():注册内容类型,第一个参数是定义了自定义文章类型的名字;第二个是一个数组,定义新的自定义文章类型的属性。

    2. 自定义内容类型的标签与更新提示信息

    /*
     * 自定义内容类型-电影
     */
    function movietalk_custom_post_type_movie(){
      $labels = array(
        // 菜单页面显示的文字
        'name'    => '电影',
        'singular_name'=> '电影',
        'add_new' => '添加电影',
        'add_new_item'=> '添加电影资料',
        'edit_item' => '编辑电影资料',
        'new_item'  => '新电影',
        'all_items' => '所有电影',
        'view_item' => '查看电影',
        'search_items'  => '搜索电影',
        'not_found' => '没有找到电影资料',
        'not_found_in_trash'  => '回收站里没找到电影资料',
        'menu_name' => '电影',
        );
      $args = array(
        'public' => true,
        'labels' => $labels,
        );
      register_post_type('movie', $args);
      // 'movie':文章类型的名称
      // $args:自定义文章类型配置数组
    }
    add_action('init', 'movietalk_custom_post_type_movie');
    // 将函数连接到指定action(动作)
    

    3. 自定义内容类型的更多参数

    'menu_postion'  => 5,   // 菜单项所在位置
    'supports'  => array('title', 'editor', 'thumnail', 'excerpt', 'custom-fields','revisions'),
    // 对文章类型的一些功能支持(标题、作者、特色图像、摘要、自定义字段等)
    

    4. 自定义内容类型的地址重写

    'has_archive' => true,  // 文章是否有归档页面
    'rewrite'     => array('slug' => 'film', 'with_front' => false),
    // 是否url重写
    

    插件 》Custom Post Type Permalinks:自定义文章类型固定链接设置插件

    5. 使用页面模板来显示自定义内容列表

    自定义内容列表模板文件

    // page-movie.php
    <?php
    /*
     * Template Name: 电影页面
     */
     get_header(); ?>
    <div class="entry-content">
      <?php query_posts('post_type=movie'); ?>
      <!-- query_posts():放在LOOP之前,限定循环所需要的条件 -->
      <?php while(have_posts()):the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
      <?php endwhile; ?>
    </div>
    

    分类法

    1. 自定义分类法

    分类法就是对内容进行分类的方法,WP提供了两种,一种叫category分类,另一种叫tag标签;分类可以拥有层级关系,而标签没有层级关系。
    创建更多的分类法,然后分配到指定的内容类型上去使用

    /*
     * 自定义分类法 - 电影类型
     */
    function movietalk_custom_taxonomy_genre(){
      $labels = array(
        'name' => '类型',
        'singular_name' => '类型',
        'search_items'  => '搜索类型',
        'popular_itmes' => '热门类型',
        'all_items'     => '所有类型',
        'parent_item'   => null,
        'parent_item_colon' => null,
        'edit_item'     => '编辑类型',
        'update_item'   => '更新类型',
        'add_new_item'  => '添加新类型',
        'separate_items_with_commas'  => '使用逗号分割不同的类型',
        'add_or_remove_items' => '添加或移除类型',
        'choose_from_most_used' => '从使用最多的类型里选择',
        'menu_name'     => '类型',
        );
      $args = array(
        'public'  => true,
        'labels'  => $labels,
        );
      register_taxonomy('genre', 'movie', $args);
      register_taxonomy($taxonomy, $object_type, $args);
      //$taxonomy 要注册的分类法的名称
      //$object_type 分类法所对应的文章类型
      //$args 相关参数
    
    }
    add_action('init', 'movietalk_custom_taxonomy_genre');
    

    --

    字段

    1. 自定义字段

    WP本身就有自定义字段功能,在发布内容的时候,我们可以定义一段额外的字段,然后为这些字段输入响应的内容。在主题的模板文件里,我们可以用代码来调用并显示这些自定义字段的内容。

    插件 》Advanced Custom Fields:我们可以创建不同的字段组,然后分配到指定的内容类型上去使用。

    菜单栏 》字段 》新建 》添加字段
    规则(创建一组规则以确定自定义字段在哪个编辑界面显示) 》文章类型 == movie

    2. 自定义字段的显示

    <?php while(have_posts()) : the_post(); ?>
      <div class="entry-entent">
        <h1><?php the_title(); ?></h1>
        <?php if(get_field('runtime')): ?>
          <!-- 判断是否填写了runtime字段 -->
          <strong>片长:</strong><?php the_field('runtime'); ?>
          <!-- 显示runtime字段 -->
        <?php endif; ?>
        <?php the_content(); ?>
      </div>
    <?php endwhile; ?>
    

    3. 图像字段

    字段 》添加字段 》海报-poster-图像
    返回值:图像ID
    预览图大小:Poster
    前端显示

    // functions.php
    add_image_size('poster', 128, 180, true);
    // 添加一个叫poster的缩略图尺寸,按照宽度128像素,高度180像素并允许裁切,生成缩略图
    
    // single-movie.php
    <!-- 获取海报的ID -->
    <?php $poster_id = get_field('poster'); ?>
    <!-- 若附件是图像,函数尝试返回其缩略图或适当大小的图像 -->
    <?php echo wp_get_attachment_image($poster_id, 'poster'); ?>
    

    4. 关系字段

    相关文章

      网友评论

          本文标题:自定义内容类型

          本文链接:https://www.haomeiwen.com/subject/sluxpttx.html