Wordpress Plugins UKM Custom Post

By | October 13th 2018 05:15:32 PM | viewed 157 times

Create a file in the location \wp-content\plugins\ukm_custom_post\ukm_custom_post.php

<?php

/*
Plugin Name: ukm custom post
Description: Add post types for movies
Author:ukmodak
*/
 

add_action( 'init', 'ukm_custom_post' );
 
   // The custom function to register a movie post type
function ukm_custom_post() {
 
     // Set the labels, this variable is used in the $args array
  $labels = array(
    'name'               => __( 'Village' ),
    'singular_name'      => __( 'Village' ),
    'add_new'            => __( 'Add New Village' ),
    'add_new_item'       => __( 'Add New Village' ),
    'edit_item'          => __( 'Edit Village' ),
    'new_item'           => __( 'New Village' ),
    'all_items'          => __( 'All Village' ),
    'view_item'          => __( 'View Village' ),
    'search_items'       => __( 'Search Village' ),
    'featured_image'     => 'Poster',                 // this add feature image
    'set_featured_image' => 'Add Poster'
  );
 
  // The arguments for our post type, to be entered as parameter 2 of register_post_type()
  $args = array(
    'labels'            => $labels,
    'description'       => 'Holds our Village and Village specific data',
    'public'            => true,
    'menu_position'     => 5,
    'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),  // these add such field on the post
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'has_archive'       => true,
    'query_var'         => 'film',
	'hierarchical'      => true,
	'rewrite'           => true,
	'taxonomies'        => array('ukm_custom_texonomy'),               // this indicate ukm_custom_texonomy
    'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),

  );
 
  // Call the actual WordPress function
  // Parameter 1 is a name for the post type
  // Parameter 2 is the $args array
  register_post_type( 'village', $args);
}


    // this add some custom  field on the post
add_action( 'admin_init', 'my_admin' );
	function my_admin() {
	   add_meta_box( 'village_review_meta_box','Village Review Details','display_village_review_meta_box','village', 'normal', 'high');
	}

function display_village_review_meta_box( $village_review ) {
  // Retrieve current name of the Director and Movie Rating based on review ID
$movie_director = esc_html( get_post_meta( $village_review->ID, 'directory', true ) );
$movie_rating = intval( get_post_meta( $village_review->ID, 'movie_rating', true ) );
?>
<table>
	<tr>
		<td style="width: 100%">Movie Director</td>
		<td>
		<input type="text" size="80" name="movie_review_director_name" value="<?php echo $movie_director; ?>" />
		</td>
	</tr>
	<tr>
		<td style="width: 150px">Movie Rating</td>
		<td>
			<select style="width: 100px" name="movie_review_rating">
			<?php
			// Generate all items of drop-down list
			for ( $rating = 5; $rating >= 1; $rating -- ) {
			?>
				<option value="<?php echo $rating; ?>" <?php echo selected( $rating, $movie_rating ); ?>>
				<?php echo $rating; ?> stars <?php } ?>
			</select>
		</td>
	</tr>
</table>
<?php
}


?>

// save this custom field of the post					
<?php
add_action( 'save_post', 'add_movie_review_fields', 10, 2 );

function add_movie_review_fields( $village_review_id, $village_review ) {
	    // Check post type for movie reviews
	if ( $village_review->post_type == 'village' ) {
		// Store data in post meta table if present in post data
		if ( isset( $_POST['movie_review_director_name'] ) && $_POST['movie_review_director_name'] != '' ) {
			update_post_meta( $village_review_id, 'directory', $_POST['movie_review_director_name'] );
		}
		if ( isset( $_POST['movie_review_rating'] ) && $_POST['movie_review_rating'] != '' ) {
			update_post_meta( $village_review_id, 'movie_rating', $_POST['movie_review_rating'] );
		}
	}
}
					
?>
		
 

To view ukm custom post(single)

Create a page in your theme as single-village.php and add the following code:

 <?php

/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * e.g., it puts together the home page when no home.php file exists.
 *
 * Learn more: {@link https://codex.wordpress.org/Template_Hierarchy}
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

get_header(); ?>

	 <div class="workingArea bg">
        <div class="contentBg fullwidth">
            <div class="col-xs-12 col-sm-9 col-md-9 col-lg-9 paddingLeftzero contentAreaMediaWithMaxmin360  bg clrRight">
                <div class="fullwidth contentBg contentMinHeight">
                  <div class='content_padding'>				
                   <?php
				   /*
						// Start the loop.
						while ( have_posts() ) : the_post();

							get_template_part( 'content', get_post_format() );

							// Previous/next post navigation.
							the_post_navigation( array(
								'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'ukmtheme' ) . '</span> ' .
									'<span class="screen-reader-text">' . __( 'Next post:', 'ukmtheme' ) . '</span> ' .
									'<span class="post-title">%title</span>',
								'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'ukmtheme' ) . '</span> ' .
									'<span class="screen-reader-text">' . __( 'Previous post:', 'ukmtheme' ) . '</span> ' .
									'<span class="post-title">%title</span>',
							) );

						// End the loop.
						endwhile;
		          */
				  
		      // or
				$args =  array(
				'posts_per_page' => '1',
				'post_type' => 'village',
				'p' => get_the_ID(),
				/*'tax_query' => array(
			        'relation' => 'AND',
				        array(
				            'taxonomy' => 'movie_genre',
				            'field'    => 'slug',
				            'terms'    => array( 'action', 'comedy' ),
				        ),
				        array(
				            'taxonomy' => 'actor',
				            'field'    => 'term_id',
				            'terms'    => array( 103, 115, 206 ),
				            'operator' => 'NOT IN',
				        ),
                                         ),*/
				);
				//print_r(get_the_ID());die;
				$myProducts = new WP_Query( $args );
				// The Loop
				while ( $myProducts->have_posts() ) : $myProducts->the_post();
				?>
				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
				
				<?php
				get_template_part( 'content', get_post_format() );
				// Reset Post Data
				wp_reset_postdata();
				endwhile;
				?>
			   
			   
<div style="float: right; margin: 10px"> <?php the_post_thumbnail( array( 100, 100 ) ); the_post_thumbnail( 'medium' ); // Medium resolution (300 x 300 max height 300px) the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height) // or $movie_box_art = get_post_meta( $post->ID, 'featured_image', TRUE ); if (!empty($movie_box_art)) { ?> <div class="movie-poster-box"> <img src="<?php echo $movie_box_art ?>" alt="<?php single_post_title(); ?> "> </div> <?php } ?> </div> <?php // Generates all custom fields attached to the post in a
    list the_meta(); // Get a specific piece of custom field echo get_post_meta( $post->ID, 'directory', TRUE ); // post id, custom_field_name, $single boolean value // Get advanced custom field field //the_field( 'field_5b69fa7399d20' ); // problem // Get advanced custom image field field ?> <strong>Custom Field Rating: </strong> <?php echo esc_html( get_post_meta( get_the_ID(), 'movie_rating', true ) ); ?> </div> </div> </div> </div> </div> <?php get_footer(); ?>

To view ukm custom post(archive)

Create a page in your theme as archive-village.php

bONEandALL