<?php
if( ! function_exists( 'create_quote_post_type' ) ):
function create_quote_post_type() {
$labels = array(
'name' => __( 'Quote' ),
'singular_name' => __( 'Quote' ),
'add_new' => __( 'Add quote' ),
'all_items' => __( 'All quotes' ),
'add_new_item' => __( 'Add quote' ),
'edit_item' => __( 'Edit quote' ),
'new_item' => __( 'New quote' ),
'view_item' => __( 'View quote' ),
'search_items' => __( 'Search quotes' ),
'not_found' => __( 'No quotes found' ),
'not_found_in_trash' => __( 'No quotes found in trash' ),
'parent_item_colon' => __( 'Parent quote' )
//'menu_name' => default to 'name'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title',
'thumbnail',
'editor',
'author',
'excerpt',
//'trackbacks',
//'custom-fields',
//'comments',
'revisions',
//'page-attributes', // (menu order, hierarchical must be true to show Parent option)
//'post-formats',
),
'taxonomies' => array( 'category', 'post_tag' ),
'menu_position' => 5,
'register_meta_box_cb' => 'add_quote_post_type_metabox'
);
register_post_type( 'quote', $args );
//flush_rewrite_rules();
register_taxonomy( 'custom_category', // register custom taxonomy
'quote',
array( 'hierarchical' => true,
'label' => 'Custom category'
)
);
}
add_action( 'init', 'create_quote_post_type' );
endif;
function add_quote_post_type_metabox() { // add the meta box
add_meta_box( 'quote_metabox', 'Meta', 'quote_metabox', 'quote', 'normal' );
}
function quote_metabox() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="quote_post_noncename" id="quote_post_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the data if its already been entered
$quote_post_name = get_post_meta($post->ID, '_quote_post_name', true);
$quote_post_desc = get_post_meta($post->ID, '_quote_post_desc', true);
// Echo out the field
?>
<div class="width_full p_box">
<p>
<label>Name<br>
<input type="text" name="quote_post_name" class="widefat" value="<?php echo $quote_post_name; ?>">
</label>
</p>
<p><label>Description<br>
<textarea name="quote_post_desc" class="widefat"><?php echo $quote_post_desc; ?></textarea>
</label>
</p>
</div>
<?php
}
function quote_post_save_meta( $post_id, $post ) { // save the data
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if( !wp_verify_nonce( $_POST['quote_post_noncename'], plugin_basename(__FILE__) ) ) {
return $post->ID;
}
// is the user allowed to edit the post or page?
if( ! current_user_can( 'edit_post', $post->ID )){
return $post->ID;
}
// ok, we're authenticated: we need to find and save the data
// we'll put it into an array to make it easier to loop though
$quote_post_meta['_quote_post_name'] = $_POST['quote_post_name'];
$quote_post_meta['_quote_post_desc'] = $_POST['quote_post_desc'];
// add values as custom fields
foreach( $quote_post_meta as $key => $value ) { // cycle through the $quote_post_meta array
// if( $post->post_type == 'revision' ) return; // don't store custom data twice
$value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
if( get_post_meta( $post->ID, $key, FALSE ) ) { // if the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // if the custom field doesn't have a value
add_post_meta( $post->ID, $key, $value );
}
if( !$value ) { // delete if blank
delete_post_meta( $post->ID, $key );
}
}
}
add_action( 'save_post', 'quote_post_save_meta', 1, 2 ); // save the custom fields
if( ! function_exists( 'view_quotes_posts' ) ): // output
function view_quotes_posts( $num = 4, $do_shortcode = 1 ) {
$args = array(
'numberposts' => $num,
'offset' => 0,
//'category' => ,
'orderby' => 'menu_order, post_title', // post_date, rand
'order' => 'DESC',
//'include' => ,
//'exclude' => ,
//'meta_key' => ,
//'meta_value' => ,
'post_type' => 'quote',
//'post_mime_type' => ,
//'post_parent' => ,
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = get_posts( $args );
$html = '';
foreach ( $posts as $post ) {
$meta_name = get_post_meta( $post->ID, '_quote_post_name', true );
$meta_desc = get_post_meta( $post->ID, '_quote_post_desc', true );
$img = get_the_post_thumbnail( $post->ID, 'medium' );
if( empty( $img ) ) {
$img = '<img src="'.plugins_url( '/img/default.png', __FILE__ ).'">';
}
if( has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$img_url = $image[0];
//the_post_thumbnail( 'thumbnail' ); /* thumbnail, medium, large, full, thumb-100, thumb-200, thumb-400, array(100,100) */
}
//$content = $post->post_content;
if( $do_shortcode == 1) {
$content = do_shortcode( $post->post_content );
}else{
$content = strip_shortcodes( $post->post_content );
}
$html .= '
<div>
<h3>'.$post->post_title.'</h3>
<div>
<p>Name: '.$meta_name.'</p>
<p>Description: '.$meta_desc.'</p>
</div>
<div>'.$img.'</div>
<div>'.$content.'</div>
</div>
';
}
$html = '<div class="wrapper">'.$html.'</div>';
return $html;
}
endif;
?> |
How would you output this to an xml file?
You may check out the code of the Sitemap.xml plugin generator.
I think you may find solution there.
You forget to close php tag in the very end of first code section
Thank you for the feedback. I fixed it.
It is not necessary to close the final PHP tag. In fact, I would recommend not closing it unless you have to.
That way, a stray linebreak at the end of your source code won't cause the white screen of death.
Hi,
I am wondering how I can enable people to read my posts in their native language (Hindi). Is creating custom post type good idea? Will it enable me to post and get comments in the native language?
Do you know a better alternative?
At present I am using google translate box and I tell they can send comments to my email using gmail in their language.
Do you have better ideas to take this forward?
Dr. Ashok Koparday
Custom post types will not help you to force people to read posts in Hindi. Custom post types are needed to create completely new type of posts, like products or services with another structure than in default posts.
If you want for people to read posts on Hindi than just write your posts on Hindi and when Google will index that posts than Google will send users to your site who will search on Hindi.
If users will like the articles than they will return to you more frequently.
So the best advice to promote your native Hindi language for users is to write useful articles for users on that language. Nothing else will not help you with it.
Where do we place the php code for register_post_type to create the custom post type? Is it its own file? If so, where does this file go? Do we add it to an existing file?
you should insert this code into functions.php of active theme or you may create a new plugin and insert code there