Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过标记收集自定义帖子类型_Php_Wordpress_Custom Post Type - Fatal编程技术网

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

Php 通过标记收集自定义帖子类型,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我已使用以下代码设置了名为“扇区”的自定义帖子类型: register_post_type( 'sectors', array( 'labels' => array( 'name' => __( 'Sectors' ), 'singular_name' => __( 'sectors' ), ), 'has_archive' => true,

我已使用以下代码设置了名为“扇区”的自定义帖子类型:

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”的所有帖子

自定义帖子类型如何实现这一点

编辑/更新:

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

如果你加上:

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

这将有助于您通过查询确定帖子类型

看起来像

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

凯西K的解决方案将完美地发挥作用。我还有第二种方法:

首先:将自定义帖子类型添加到主查询中。您可以通过在
functions.php
中添加几行代码来实现这一点

<?php   
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
    function add_my_post_types_to_query( $query ) {
        // Leave the query as it is in admin area
        if( is_admin() ) {
            return $query;
        }
        // add 'sectors' to main_query when it's a tag- or post-archive
      if ( is_tag() && $query->is_main_query() || is_archive() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'sectors', 'add_more_here' ) );
      return $query;
    }
?>

如果要查找带有标记名的自定义帖子类型,则需要在查询参数中指定:

  <?php $query = new WP_Query( array( "post_type" => "sectors", "tag" => "featuredpost1" ) );
        
   while ($query->have_posts()) : $query->the_post();
        
   the_title();
        
  endwhile; ?>


这可能会对您有所帮助。

有人有什么想法吗?您是否在args中添加了
“post type”=>“sections”
?您正在哪个页面/模板上执行此操作。你真的需要一个定制的querynice开箱即用的思维方式吗。这是一些有趣的知识。谢谢你发布这个答案!谢谢你,这工作做得很好!同时,感谢Zork的回答,以及Selva。
  <?php $query = new WP_Query( array( "post_type" => "sectors", "tag" => "featuredpost1" ) );
        
   while ($query->have_posts()) : $query->the_post();
        
   the_title();
        
  endwhile; ?>