在WordPress中检测您的自定义帖子类型

时间:2017-02-10 作者:Danish Iftikhar

稍后,我需要注册帖子类型的哪些属性来检测代码中的帖子类型。假设下面是注册自定义帖子类型的代码。

function dwwp_register_post_type() {

    $singular = __( \'Job Listing\' );
    $plural = __( \'Job Listings\' );
        //Used for the rewrite slug below.
        $plural_slug = str_replace( \' \', \'_\', $plural );

        //Setup all the labels to accurately reflect this post type.
    $labels = array(
        \'name\'                  => $plural,
        \'singular_name\'         => $singular,
        \'add_new\'               => \'Add New\',
        \'add_new_item\'          => \'Add New \' . $singular,
        \'edit\'                  => \'Edit\',
        \'edit_item\'             => \'Edit \' . $singular,
        \'new_item\'              => \'New \' . $singular,
        \'view\'                  => \'View \' . $singular,
        \'view_item\'             => \'View \' . $singular,
        \'search_term\'           => \'Search \' . $plural,
        \'parent\'                => \'Parent \' . $singular,
        \'not_found\'             => \'No \' . $plural .\' found\',
        \'not_found_in_trash\'    => \'No \' . $plural .\' in Trash\'
    );

        //Define all the arguments for this post type.
    $args = array(
        \'labels\'              => $labels,
        \'public\'              => true,
        \'publicly_queryable\'  => true,
        \'exclude_from_search\' => false,
        \'show_in_nav_menus\'   => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 6,
        \'menu_icon\'           => \'dashicons-admin-site\',
        \'can_export\'          => true,
        \'delete_with_user\'    => false,
        \'hierarchical\'        => true,
        \'has_archive\'         => true,
        \'query_var\'           => true,
        \'capability_type\'     => \'post\',
        \'map_meta_cap\'        => true,
        // \'capabilities\' => array(),
        \'rewrite\'             => array( 
            \'slug\' => strtolower( $plural_slug ),
            \'with_front\' => true,
            \'pages\' => true,
            \'feeds\' => false,

        ),
        \'supports\'            => array( 
            \'title\'
        )
    );

        //Create the post type using the above two varaiables.
    register_post_type( \'jobs\', $args);
}
add_action( \'init\', \'dwwp_register_post_type\' );

1 个回复
SO网友:Pedro Coitinho

你在找get_post_type?

您可以使用它按slug获取任何CPT类型,并有条件地使用它

例如:

function filter_the_content( $content ) {
  if ( ! is_user_logged_in() && \'my_cpt\' == get_post_type() ) {
    return "Content reserved to logged in users";
  }

  return $content;
}

add_filter( \'the_content\', \'filter_the_content\' );
文件:

https://developer.wordpress.org/reference/functions/get_post_type/

相关推荐