在循环中查询自定义帖子类型

时间:2014-02-04 作者:Sam

我有一个自定义的post类型,名为服务:

// Services
function services_cpt() {
  $labels = array(
    \'name\'               => _x( \'Services\', \'post type general name\' ),
    \'singular_name\'      => _x( \'Service\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add New\', \'production\' ),
    \'add_new_item\'       => __( \'Add New Service\' ),
    \'edit_item\'          => __( \'Edit Service\' ),
    \'new_item\'           => __( \'New Service\' ),
    \'all_items\'          => __( \'All Services\' ),
    \'view_item\'          => __( \'View Service\' ),
    \'search_items\'       => __( \'Search Services\' ),
    \'not_found\'          => __( \'No services found\' ),
    \'not_found_in_trash\' => __( \'No services found in the Trash\' ), 
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'Services\'
  );
  $args = array(
    \'labels\'        => $labels,
    \'description\'   => \'Holds our services and service specific data\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\' ),
    \'has_archive\'   => true,
  );
  register_post_type( \'services\', $args ); 
}
add_action( \'init\', \'services_cpt\' );
我想使用页面模板来显示帖子,而不是归档服务。php。我构建了以下页面模板:

<?php
/*
Template Name: Services
*/
// Get header
get_header(); ?>

<div id="primary">
<?php $loop = new WP_Query( array( \'post_type\' => \'services\', \'posts_per_page\' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<section class="col third">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'medium\'); ?></a>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</section>

<!-- End loop -->
<?php endwhile; wp_reset_query(); ?>

<!-- End primary -->
</div>
<?php get_footer(); ?>
当我转到前端的页面时,它会显示默认存档。php。我不知道我做错了什么。非常感谢您的帮助:)

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您可能与has_archive 此处的参数:

 $args = array(
    \'labels\'        => $labels,
    \'description\'   => \'Holds our services and service specific data\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\' ),
    \'has_archive\'   => true,
  );
设置has_archivefalse 如果不需要自动生成的存档。

但我真的不明白不使用模板系统的逻辑。

结束

相关推荐

Pagination on Custom Loop

我想将最新帖子的样式与自定义页面上某个类别中的所有其他帖子的样式不同。到目前为止,我有下面的代码,它正是这样做的:<?php if (have_posts()) : ?> <?php query_posts(\"cat=4&showposts=1\"); ?> <?php while (have_posts()) : the_post(); ?> (Style1)<?php the_title(); ?><