Tuesday 2 May 2017

Creating Custom Post Types and Template In WordPress

Creating Custom Post Types In WordPress
Step 1 – Register Post Type in function.php

// Creates pressrelease Custom Post Type
function press_release() {
    $args = array(
      'label' => 'Press Release',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'pressrelease'),
        'query_var' => true,
        'menu_icon' => 'dashicons-video-alt',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'pressrelease', $args );
}
add_action( 'init', 'press_release' );



Step 2 – Create a Custom Post Type Template and get the post in this template

<?php Template Name: Press release ?>
<?php get_header();
<div>

<?php
$args = array('post_type' => 'pressrelease','order'=>'DESC','orderby' => 'title');

$press = new WP_Query( $args );

while ( $press->have_posts() ) : $press->the_post(); ?>
<h3><?php the_title();?></h3>

<p><?php the_content(); ?></p>
<?php endwhile; wp_reset_postdata();?>
</div>

// get any page content in this template

<div>
<?php $query = new WP_Query('page_id=25');

if($query->have_posts()) : $query->the_post(); ?>

<h3>Revenue Cycle Management</h3>

<p><?php the_content(); ?></p>
<a href="<?php the_permalink();?>">Learn More</a>

<?php endif;?>
</div>

<?php get_footer(); ?>