【问题标题】:About wordpress post types templates关于 wordpress 帖子类型模板
【发布时间】:2015-04-07 00:50:32
【问题描述】:

wordpress 中自定义帖子类型的模板存在问题。这是我的function.php:

add_action( 'init', 'register_cpt_door' );

function register_cpt_door() {

$args = array( 
    'hierarchical' => true,

    'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
    'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array('slug' => 'door'),
    'capability_type' => 'post'
);

register_post_type( 'door', $args );

}

我对单页使用 single-door.php,它可以工作。但是我尝试使用category-door.php和archive-door.php进行分类,两者都不起作用,wordpress返回index.php。

我应该使用什么模板名称?

【问题讨论】:

标签: php wordpress custom-post-type


【解决方案1】:

它不起作用,因为category-door.php 是带有标签door 的类别的模板。 archive-door.php 是自定义帖子类型存档的模板,而不是特定类别中具有类型门的帖子的模板。

https://codex.wordpress.org/Template_Hierarchy

分类术语可以属于多个帖子类型,因此无法为特定帖子类型范围内的分类创建模板。

这里的解决方案是为门创建自定义分类:

https://codex.wordpress.org/Function_Reference/register_taxonomy

创建自定义分类后(我将其称为 door-categories),您可以在 taxonomy-door-categories.php 文件中的该分类中创建模板术语。

【讨论】:

    【解决方案2】:

    请查看Template Hierarchy

    文件category-door.php 将用于具有slug door的类别。

    文件archive-door.php 用于自定义帖子类型的主存档,您已使用选项'has_archive' => true 启用该文件。它不适用于类别索引。

    对于您的自定义帖子,您使用了 Wordpress 中默认存在的分类法 category,因此您可以使用模板文件 category.php。 在循环中,您可以检查帖子类型并为其赋予不同的类/样式:

    <?php while ( have_posts() ) : the_post(); ?>
    
        <?php if( 'door' == get_post_type() ): ?>
    
            <div class="door">
                This is a door
            </div>
    
        <?php else: ?>
    
            <div class="post">
                This is a post
            </div>
    
        <?php endif; ?>
    
    <?php endwhile; ?>
    

    否则,如果您希望自定义帖子类型的类别与 register a new taxonomy 不同。

    【讨论】:

      【解决方案3】:
      Custom Post Type in wordpress. Basic four steps 
      
      Step 1 : File Path location : theme/function.php in your theme
      
        Paste code in function.php (register custom post type )
      
        <?php
      
      add_action( 'init', 'custom_post_type_func' );
      function custom_post_type_func() {
          //posttypename = services
      $labels = array(
      'name' => _x( 'Services', 'services' ),
      'singular_name' => _x( 'services', 'services' ),
      'add_new' => _x( 'Add New', 'services' ),
      'add_new_item' => _x( 'Add New services', 'services' ),
      'edit_item' => _x( 'Edit services', 'services' ),
      'new_item' => _x( 'New services', 'services' ),
      'view_item' => _x( 'View services', 'services' ),
      'search_items' => _x( 'Search services', 'services' ),
      'not_found' => _x( 'No services found', 'services' ),
      'not_found_in_trash' => _x( 'No services found in Trash', 'services' ),
      'parent_item_colon' => _x( 'Parent services:', 'services' ),
      'menu_name' => _x( 'Services', 'services' ),
      );
      $args = array(
      'labels' => $labels,
      'hierarchical' => true,
      'description' => 'Hi, this is my custom post type.',
      'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
      'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
      'public' => true,
      'show_ui' => true,
      'show_in_menu' => true,
      'show_in_nav_menus' => true,
      'publicly_queryable' => true,
      'exclude_from_search' => false,
      'has_archive' => true,
      'query_var' => true,
      'can_export' => true,
      'rewrite' => true,
      'capability_type' => 'post'
      );
      register_post_type( 'services', $args );
      }
      ?>
      

      第2步:如何在wordpress模板页面中显示wordpress自定义帖子类型?

        : you can show anywhere in template page like this :
      
      
      
      <?php   $args = array( 'post_type' => 'services', 'posts_per_page' => 20 );
                  $loop = new WP_Query( $args );
                  while ( $loop->have_posts() ) : $loop->the_post(); ?>
                  <div class="services-items">
                  <?php the_title(); 
              if ( has_post_thumbnail( $post->ID ) ) {
              echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
              echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
              echo '</a>'; }
      
      ?>
                  </div>
          <?php endwhile; ?>
      

      第 3 步:为像这样显示单个帖子创建新模板

      single-{自定义帖子类型名称}.php 或者 单服务.php

      第 4 步:将代码粘贴到 single-services.php 文件中

       <?php /* The loop */ ?>
                  <?php while ( have_posts() ) : the_post(); ?>
                      <div class="main-post-div">
                      <div class="single-page-post-heading">
                      <h1><?php the_title(); ?></h1>
                      </div>
                      <div class="content-here">
                      <?php  the_content();  ?>
                      </div>
                      <div class="comment-section-here"
                      <?php //comments_template(); ?>
                      </div>
                      </div>
      
                  <?php endwhile; ?>
      

      这是带有单个帖子页面的自定义帖子类型示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        相关资源
        最近更新 更多