获取已注册的自定义帖子类型列表

时间:2014-11-05 作者:Tarun

如何获取所有已注册自定义帖子类型的列表,并将其显示在任何模板页面上。

我有这样的代码。

<?php

    /**
     * Template Name: Custom Post Types List
     */

    get_header();

    $args=array(
        \'public\'                => true,
        \'exclude_from_search\'   => false,
        \'_builtin\'              => false
    ); 

    $output = \'names\'; // names or objects, note names is the default
    $operator = \'and\'; // \'and\' or \'or\'
    $post_types = get_post_types($args,$output,$operator);

    $posttypes_array = array();

    foreach ($post_types  as $post_type ) {
        $posttypes_array[] = $post_type;
    }

    echo "<pre>";
    print_r($posttypes_array);
    echo "</pre>";

    get_footer();

?>

5 个回复
SO网友:Kamal Ahmed

您的代码看起来不错。但是,您可以尝试以下代码来获取所有自定义帖子

    $args = array(
       \'public\'   => true,
       \'_builtin\' => false,
    );

    $output = \'names\'; // names or objects, note names is the default
    $operator = \'and\'; // \'and\' or \'or\'

    $post_types = get_post_types( $args, $output, $operator ); 

    foreach ( $post_types  as $post_type ) {

       echo \'<p>\' . $post_type . \'</p>\';
    }

    ?>
您还可以使用一组参数来过滤结果。有关参数的详细列表,您可以查看官方WordPress Codex页面:https://codex.wordpress.org/Function_Reference/get_post_types

SO网友:Mehedi Hasan

如果要将所有帖子类型作为列表获取,则需要使用get_post_types 函数并使用foreach.

<?php
    // Get All Post Types as List
    foreach ( get_post_types( \'\', \'names\' ) as $post_type ) {
        echo \'<p>\'.$post_type.\'</p>\';
    }
?>
这两行代码可以列出所有注册的帖子类型。如果你想了解更多get_post_types 功能访问wp codex官方页面:https://codex.wordpress.org/Function_Reference/get_post_types

SO网友:samjco
global $wp_post_types;

$posttypes = array_keys( $wp_post_types );

// Remove _builtins or others
$pt_remove = array("attachment","nav_menu_item","customize_changeset","revision");

foreach ( $posttypes as $posttype ):

 if ( in_array($posttype, $pt_remove) ) continue;

 $posttype_names[ $posttype ] = $posttype;

endforeach;

echo "<pre>";
 print_r($posttype_names);
echo "</pre>";
SO网友:maverick

<?php
$args = array(
  \'post_type\'   => \'clients\',
  \'post_status\' => \'publish\',
  \'tax_query\'   => array(
    array(
        \'taxonomy\' => \'clients_service\',
        \'field\'    => \'slug\',
        \'terms\'    => \'therapy\'
    )
  )
 );

$testimonials = new WP_Query( $args );
if( $testimonials->have_posts() ) :
?>
  <ul>
    <?php
      while( $testimonials->have_posts() ) :
        $testimonials->the_post();
        ?>
          <li><?php printf( \'%1$s - %2$s\', get_the_title(), get_the_content() );  ?></li>
        <?php
      endwhile;
      wp_reset_postdata();
    ?>
  </ul>
<?php
else :
  esc_html_e( \'No clients in the therapy taxonomy!\', \'text-domain\' );
endif;
?>
调用新的WP\\u查询时,将检索相关的客户端,我们可以对它们进行循环。在循环中,我们只需在一个简单的列表中输出客户端的标题和内容。

SO网友:Tariq Ahmed

您的代码是正确的。您在执行这两项操作的时间安排方面存在的问题:

注册CPT调用您编写的post类型函数您有两种解决方案之一:

Solution 1:

在调用之前,请确保您的CPT以更高的优先级注册get_post_types 作用

Solution 2:

减少get_post_types 注册CPT后启动的优先级。

add_action(\'init\', \'your_function_name\', 10);
我希望这有帮助。

非常感谢。

结束

相关推荐