通过标签收集自定义帖子类型

16 php wordpress custom-post-type

我使用以下代码设置了名为"sector"的自定义帖子类型:

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' ),
    ));
}
Run Code Online (Sandbox Code Playgroud)

这允许我将"标签"添加到自定义帖子类型页面.

现在,我试图通过某些标签显示此自定义帖子类型的页面.

我通过使用以下代码设法使用帖子执行此操作:

<?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(); ?>
Run Code Online (Sandbox Code Playgroud)

这将获得所有标记为"featuredpost1"的帖子.

自定义帖子类型如何实现?

编辑/ UPDATE:

这确实有效,有没有办法可以在不同的页面上使用此功能?例如,在我的主页上通过标签获取帖子,因此在此页面上更新的内容将在主页上更新?

Cay*_*e K 12

Wordpress查询参数

如果你添加::

$args = array(
    'post_type' => array( 'sectors' ) //, 'multiple_types_after_commas' )
);
$query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)

要么

$query = new WP_Query( 'post_type=sectors' );
Run Code Online (Sandbox Code Playgroud)

这有助于您使用查询定位帖子类型.

它看起来像

$args = array(
    'tag_slug__and' => array('featuredpost1'),
    'post_type' => array( 'sectors' )
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
Run Code Online (Sandbox Code Playgroud)