在wordpress中自定义帖子类型。基本的四个步骤。步骤1:文件路径位置:主题/函数。主题中的php。在函数中粘贴代码。php(注册自定义帖子类型)
<?php
add_action( \'init\', \'custom_post_type_func\' );
function custom_post_type_func() {
//posttypename = services
$labels = array(
\'name\' => _x( \'Services\', \'services\' ),
\'singular_name\' => _x( \'services\', \'services\' ),
\'add_new\' => _x( \'Add New\', \'services\' ),
\'add_new_item\' => _x( \'Add New services\', \'services\' ),
\'edit_item\' => _x( \'Edit services\', \'services\' ),
\'new_item\' => _x( \'New services\', \'services\' ),
\'view_item\' => _x( \'View services\', \'services\' ),
\'search_items\' => _x( \'Search services\', \'services\' ),
\'not_found\' => _x( \'No services found\', \'services\' ),
\'not_found_in_trash\' => _x( \'No services found in Trash\', \'services\' ),
\'parent_item_colon\' => _x( \'Parent services:\', \'services\' ),
\'menu_name\' => _x( \'Services\', \'services\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'description\' => \'Hi, this is my custom post type.\',
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'page-attributes\' ),
\'taxonomies\' => array( \'category\', \'post_tag\', \'page-category\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\'
);
register_post_type( \'services\', $args );
}
?>
第二步:如何在wordpress模板页面中显示wordpress自定义帖子类型?
您可以在模板页面的任何位置显示以下内容:
<?php $args = array( \'post_type\' => \'services\', \'posts_per_page\' => 20 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="services-items">
<?php the_title();
if ( has_post_thumbnail( $post->ID ) ) {
echo \'<a href="\' . get_permalink( $post->ID ) . \'" title="\' . esc_attr( $post->post_title ) . \'">\';
echo get_the_post_thumbnail( $post->ID, \'thumbnail\' );
echo \'</a>\'; }
?>
</div>
<?php endwhile; ?>
步骤3:创建新模板,以便像这样显示单个帖子
单一-{自定义帖子类型名称}。php或单个服务。php
步骤4:在单个服务中粘贴代码。php文件
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="main-post-div">
<div class="single-page-post-heading">
<h1><?php the_title(); ?></h1>
</div>
<div class="content-here">
<?php the_content(); ?>
</div>
<div class="comment-section-here"
<?php //comments_template(); ?>
</div>
</div>
<?php endwhile; ?>
这是一个带有单个贴子页面的自定义贴子类型示例。