Custom Post Types

add_action(‘init’,’product_register’);
function product_register()
{
$labels = array(
‘name’ => _x(‘Product’, ‘post type general name’),
‘singular_name’ => _x(‘Product Item’, ‘post type singular name’),
‘add_new’ => _x(‘Add New’, ‘Product item’),
‘add_new_item’ => __(‘Add New Product Item’),
‘edit_item’ => __(‘Edit Product Item’),
‘new_item’ => __(‘New Product Item’),
‘view_item’ => __(‘View Product Item’),
‘search_items’ => __(‘Search Product’),
‘not_found’ =>  __(‘Nothing found’),
‘not_found_in_trash’ => __(‘Nothing found in Trash’),
‘parent_item_colon’ => ”
);

$args = array(
‘labels’ => $labels,
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘query_var’ => true,
‘menu_icon’ => admin_url() . ‘/images/product.png’,
‘rewrite’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array(‘title’,’editor’,’thumbnail’)
);
register_post_type( ‘product’ , $args );
}

Display Post Content

<?php
$args = array(
‘post_type’ => ‘blog’,
‘order’ => ‘DESC’,
‘posts_per_page’ => 3,
‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1),//using Pagination
);

query_posts($args);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );

?>

<article>

<div>

<h2><a href=”<?php the_permalink(); ?>” rel=”bookmark”><?php the_title(); ?></a></h2>
<div>
<ul><li><span>Posted: </span><?php the_author(); ?></li>
<li><span>Date: </span><?php the_time(‘F j, Y’);?></li>
<li><span>Categories: </span><?php $category = get_the_category(); echo $category[0]->cat_name;?></li>
<li><span>Comments: </span><?php echo get_comments_number($post_id); ?></li></ul>
<div></div>
</div>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<h2><?php echo ”; ?> </h2>
<?php endif; ?>
</div>
<div>
<p><?php echo substr($post->post_content,0,400); ?></p>
<a href=”<?php the_permalink(); ?>”>Read more….</a>

OR——-<?php echo get_post_permalink($post_7); ?>
</div>

</article>

<?php endforeach;
wp_reset_postdata();
?>

Category Name And Link Page

<?php
$args = array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
);
$categories = get_categories($args);
foreach($categories as $category){
?>
<ul>
<li><?php echo ‘<p><a href=”‘ . get_category_link( $category->term_id ) . ‘” . ‘ . ‘>’ . $category->name.'</a> </p> ‘;?></li>

</ul>
<?php } ?>

 

Display Post By ID:-

$post = get_post(93);
$title = $post->post_title;
$content = $post->post_content;
?>
<h2><?php echo $title;?></h2>
<p><?php echo $content;?></p>

Advertisements

One thought on “Custom Post Types

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s