About Custom Posts
-
I use this code to show a custom post named Banner and using this code it works perfectly and displaying the post in proper structure as i implement the single.php .
// register a custom post type called ‘banner’
function wptutsplus_create_post_type() {
$labels = array(
‘name’ => __( ‘Banner’ ),
‘singular_name’ => __( ‘banner’ ),
‘add_new’ => __( ‘New banner’ ),
‘add_new_item’ => __( ‘Add New banner’ ),
‘edit_item’ => __( ‘Edit banner’ ),
‘new_item’ => __( ‘New banner’ ),
‘view_item’ => __( ‘View banner’ ),
‘search_items’ => __( ‘Search banners’ ),
‘not_found’ => __( ‘No banners Found’ ),
‘not_found_in_trash’ => __( ‘No banners found in Trash’ ),
);
$args = array(
‘labels’ => $labels,
‘has_archive’ => true,
‘public’ => true,
‘hierarchical’ => false,
‘supports’ => array(
‘title’,
‘editor’,
‘excerpt’,
‘custom-fields’,
‘thumbnail’,
‘page-attributes’
),
‘taxonomies’ => array( ‘post_tag’, ‘category’),
);
register_post_type( ‘banner’, $args );
}
add_action( ‘init’, ‘wptutsplus_create_post_type’ );// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {// start by setting up the query
$query = new WP_Query( array(
‘post_type’ => ‘banner’,
));// now check if the query has posts and if so, output their content in a banner-box div
if ( $query->have_posts() ) { ?>
<div class=”banner-box”>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id=”post-<?php the_ID(); ?>” <?php post_class( ‘banner’ ); ?>><?php the_content(); ?></div>
<?php endwhile; ?>
</div>
<?php }
wp_reset_postdata();}
But the prob lem is when i want create another custom post named Service using this this by rename the function and its parameter also , it gives the custom post in left side navigation menu bt when i create a new service post and then viewing the post its showing that “Page not found ” . I use the below code to display Service :
// register a custom post type called ‘banner’
function wptutsplus_create_post_type() {
$labels = array(
‘name’ => __( ‘Service’ ),
‘singular_name’ => __( ‘service’ ),
‘add_new’ => __( ‘New service’ ),
‘add_new_item’ => __( ‘Add New service’ ),
‘edit_item’ => __( ‘Edit service’ ),
‘new_item’ => __( ‘New service’ ),
‘view_item’ => __( ‘View service’ ),
‘search_items’ => __( ‘Search services’ ),
‘not_found’ => __( ‘No services Found’ ),
‘not_found_in_trash’ => __( ‘No services found in Trash’ ),
);
$args = array(
‘labels’ => $labels,
‘has_archive’ => true,
‘public’ => true,
‘hierarchical’ => false,
‘supports’ => array(
‘title’,
‘editor’,
‘excerpt’,
‘custom-fields’,
‘thumbnail’,
‘page-attributes’
),
‘taxonomies’ => array( ‘post_tag’, ‘category’),
);
register_post_type( ‘service’, $args );
}
add_action( ‘init’, ‘wptutsplus_create_post_type’ );// function to show home page banner using query of banner post type
function wptutsplus_home_page_service() {// start by setting up the query
$query = new WP_Query( array(
‘post_type’ => ‘service’,
));// now check if the query has posts and if so, output their content in a banner-box div
if ( $query->have_posts() ) { ?>
<div class=”banner-box”>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id=”post-<?php the_ID(); ?>” <?php post_class( ‘service’ ); ?>><?php the_content(); ?></div>
<?php endwhile; ?>
</div>
<?php }
wp_reset_postdata();}
- The topic ‘About Custom Posts’ is closed to new replies.