首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义职位类型和类别

自定义职位类型和类别
EN

Stack Overflow用户
提问于 2013-05-17 15:09:20
回答 5查看 6.5K关注 0票数 6

几天来,我一直在尝试创建一个带有类别的定制post类型。到目前为止,我已经完成了这项工作,我可以轻松地添加内容,并将其分配给一个类别。我的密码在下面。

我不明白,而且似乎无法工作的是创建一个归档页面来显示某一类别的帖子。

例如:我的帖子类型被称为广告。我的分类叫Photographers.

页面是否能够动态地计算出您所处的类别,并显示属于该类别的所有自定义文章?

代码语言:javascript
运行
复制
function my_custom_post_advert() {
    $labels = array(
        'name'               => _x( 'Adverts', 'post type general name' ),
        'singular_name'      => _x( 'Advert', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'advert' ),
        'add_new_item'       => __( 'Add New Advert' ),
        'edit_item'          => __( 'Edit Advert' ),
        'new_item'           => __( 'New Advert' ),
        'all_items'          => __( 'All Adverts' ),
        'view_item'          => __( 'View Advert' ),
        'search_items'       => __( 'Search Adverts' ),
        'not_found'          => __( 'No adverts found' ),
        'not_found_in_trash' => __( 'No adverts found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Adverts'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our adverts and advert specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'category' ),
        'has_archive'   => true,
    );
    register_post_type( 'advert', $args );  
}
add_action( 'init', 'my_custom_post_advert' );

function my_taxonomies_advert() {
    $labels = array(
        'name'              => _x( 'Advert Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Advert Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Advert Categories' ),
        'all_items'         => __( 'All Advert Categories' ),
        'parent_item'       => __( 'Parent Advert Category' ),
        'parent_item_colon' => __( 'Parent Advert Category:' ),
        'edit_item'         => __( 'Edit Advert Category' ), 
        'update_item'       => __( 'Update Advert Category' ),
        'add_new_item'      => __( 'Add New Advert Category' ),
        'new_item_name'     => __( 'New Advert Category' ),
        'menu_name'         => __( 'Advert Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'advert_category', 'advert', $args );
}
add_action( 'init', 'my_taxonomies_advert', 0 );
EN

回答 5

Stack Overflow用户

发布于 2014-08-11 18:00:28

基本上,您需要创建一个页面模板,其中包含一个自定义的wp_query,这样WordPress就可以确定您所处的类别。

一旦创建了页面模板,您就可以在WordPress管理中创建一个页面.选择新的页面模板作为模板。

如果希望类别是动态的,则可以始终设置页面模板以接受$_GET参数,以确定显示广告的类别。就像这样:

代码语言:javascript
运行
复制
http://example.com/adverts-listing/?mycat=photographers

代码语言:javascript
运行
复制
http://example.com/adverts-listing/?mycat=programmers

等。

下面是一个页面模板外观的示例(当然,这会因您使用的主题而有所不同……这个例子是为了使用二十四个主题而建的):

代码语言:javascript
运行
复制
<?php
/**
 * Template Name: Advert Listing
 *
 */

get_header(); ?>

  <section id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

    <?php
      // Set the args array for the query to happen
      $args = array(
        'post_type' => 'adverts',
        'post_status' => 'publish',
        'posts_per_page' => 10
      );

      // Dynamically set the mycat argument from a $_GET parameter
      if( isset($_GET['mycat']) ) {
        $args['tax_query'] => array(
          array(
            'taxonomy' => 'advert_category',
            'field' => 'slug',
            'terms' => $_GET['mycat']
          )
        );
      }

      // Issue the query
      $q = null;
      $q = new WP_Query($args);

      // Start the loop
      if( $q->have_posts() ) : ?>
        <header class="page-header">
          <h1 class="page-title"><?php _e( 'Advert Listing:', 'twentyfourteen' ); ?></h1>
        </header>

        <?php

        while ($q->have_posts()) : $q->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> adverts type-adverts status-publish hentry">

            <header class="entry-header">
              <a href="<?php echo get_permalink(get_the_ID()); ?>"><h3 class="entry-title"><?php the_title(); ?></h3></a>
            </header><!-- .entry-header -->

            <div class="entry-content">
              <?php the_excerpt(); ?>
            </div>

          </article>

          <?php

        endwhile;

        // Previous/next post navigation.
        twentyfourteen_paging_nav();

      else :
        // If no content, include the "No posts found" template.
        get_template_part( 'content', 'none' );

      endif;

      wp_reset_query();  // Restore global post data stomped by the_post().
      ?>

    </div><!-- #content -->
  </section><!-- #primary -->

<?php

get_sidebar( 'content' );
get_sidebar();
get_footer();
票数 2
EN

Stack Overflow用户

发布于 2013-05-21 19:03:02

您应该能够导航到/adverts。另外,has_archive应该为您创建一个归档页面。

票数 1
EN

Stack Overflow用户

发布于 2013-05-17 16:23:11

为了省下很多麻烦,我过去用过的是这个定制的post类型插件 --它的作用就像一种魅力:

类型让我们通过添加内容类型、自定义字段和分类法来定制WordPress管理。您将能够完成WordPress管理,并将其转化为您自己的内容管理系统。

然后我使用这个自定义post类型存档插件

这个插件将启用自定义post类型的档案(也每年,每月和每天)连同提要,可定制的标题和分页。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16612411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档