通过标签收集自定义帖子类型 [英] Gathering Custom post types via tags

查看:63
本文介绍了通过标签收集自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码设置了自定义帖子类型 sectors:

I have set up my custom post type called 'sectors', using the code below:

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' ),
    ));
}

这使我可以在自定义帖子类型页面上添加标签。

This has allowed me to add 'tags' to the custom post type pages.

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

Now, I am trying to display pages fron this custom post types by certain tags.

我已经管理好了为此,请使用以下代码:

I have managed to do this with posts, by using the following code:

<?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的帖子。

This will get all posts which have the tag 'featuredpost1'.

自定义帖子类型怎么可能?

How is this possible with custom post types?

编辑/更新:

现在可以正常工作,有没有办法可以在其他页面上使用此功能?例如,在我的主页上通过标签获取帖子,因此,此页面上进行的所有更新都会在主页上进行更新?

This does work now, is there a way I can use this functionality on a different page? For example, on my homepage get the posts via tags, so whatever is updated on this page will update on the homepage??

推荐答案

Wordpress查询参数

如果添加::

$args = array(
    'post_type' => array( 'sectors' ) //, 'multiple_types_after_commas' )
);
$query = new WP_Query( $args );

$query = new WP_Query( 'post_type=sectors' );

这将帮助您通过查询定位您的帖子类型。

This will help you target your post type with your query.

它看起来像

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

这篇关于通过标签收集自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆