【问题标题】:How to add excerpt in custom post type in wordpress?如何在wordpress的自定义帖子类型中添加摘录?
【发布时间】:2018-01-08 05:11:08
【问题描述】:

我在 WordPress 中创建了一个自定义帖子类型,如何添加自定义摘录并在其中添加一个字段。自定义帖子类型保存在同一个 wp_posts 表中。和添加选项显示所有字段。但现在我想在其中添加自定义摘录字段。我有任何 WordPress 功能来添加摘录。任何人都可以提供帮助!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    将您的支持字段更改为此 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'cmets' ) );

    【讨论】:

      【解决方案2】:

      我希望您通过在主题 function.php 文件中添加函数 register_post_type() 来创建自定义帖子类型。如果是,您只需使用“支持”更新您的代码。然后转到屏幕选项并单击“摘录”。

      $args = array(
          'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
      );
      
      register_post_type( 'book', $args );
      

      或者也可以添加如下代码

      add_action( 'init', 'my_add_excerpts_to_pages' );
      function my_add_excerpts_to_pages() {
           add_post_type_support( 'page', 'excerpt' ); //change page with your post type slug.
      }
      

      【讨论】:

        【解决方案3】:

        如何在 WordPress 中添加自定义文章类型的摘录?

        示例 1:

        <?php
        /**
         * Enables the Excerpt meta box in post type edit screen.
         */
        function wpcodex_add_excerpt_support_for_post() {
            add_post_type_support( 'your post type slug name here', 'excerpt' );
        }
        add_action( 'init', 'wpcodex_add_excerpt_support_for_post' );
        ?>
        

        更多细节在这里:https://codex.wordpress.org/Function_Reference/add_post_type_support

        示例 2:

        <?php
        add_action( 'init', 'create_testimonial_posttype' );
        function create_testimonial_posttype(){
          register_post_type( 'testimonials',
            array(
              'labels' => array(
                'name' => __( 'Testimonials' ),
                'singular_name' => __( 'Testimonial' )
              ),
              'public' => true,
              'has_archive' => true,
              'rewrite' => array('slug' => 'clients'),
              'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
            )
          );
        }
        ?>
        

        【讨论】:

          【解决方案4】:

          在屏幕顶部有一个选项,即屏幕选项,可以在添加帖子时添加exceprt。选择专家,exceprt字段自动添加到添加帖子页面。

          【讨论】:

            猜你喜欢
            • 2018-02-23
            • 1970-01-01
            • 2014-06-25
            • 1970-01-01
            • 2012-04-10
            • 1970-01-01
            • 2012-11-28
            • 2011-11-19
            • 1970-01-01
            相关资源
            最近更新 更多