【发布时间】:2015-07-06 05:50:28
【问题描述】:
我已使用以下代码设置了名为“部门”的自定义帖子类型:
register_post_type( 'sectors',
array(
'labels' => array(
'name' => __( 'Sectors' ),
'singular_name' => __( 'sectors' ),
),
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-heart',
'public' => true,
'rewrite' => array( 'slug' => 'your-cpt', 'with_front' => false ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'your-cpt-type', 'post_tag' ),
));
}
这让我可以将“标签”添加到自定义帖子类型页面。
现在,我正在尝试通过某些标签显示此自定义帖子类型的页面。
我已经设法通过使用以下代码来处理帖子:
<?php
$args = array('tag_slug__and' => array('featuredpost1'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<h5 class="captext"><?php the_title(); ?></h5>
<hr>
<div style="float: left; padding-right:20px;">
<?php the_post_thumbnail( 'thumb' ); ?>
</div>
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink(); ?>"> Read More...</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
这将获取所有带有“featuredpost1”标签的帖子。
自定义帖子类型如何实现?
编辑/更新:
这确实有效,有没有办法可以在不同的页面上使用此功能?比如在我的主页上通过标签获取帖子,那么这个页面上的任何更新都会在主页上更新??
【问题讨论】:
-
有人有什么想法吗?
-
您是否已将
"post-type" => "sectors"添加到您的参数中? -
您在哪个页面/模板上执行此操作。你真的需要自定义查询吗
标签: php wordpress custom-post-type