How to add custom post type to wordpress menu

WordPress tutorial, for developer, Many people wondering How to add custom post type to wordpress menu. Best idea is first create page with custom post type.

For example you created custom post type art then you need to create page name with art. Then create template with name of art.

How to add custom post type to wordpress menu

Add following code in that file.

<?php /* Template Name: Art */
$loop = new WP_Query( array( 'post_type' => 'art', 'posts_per_page' => 10 ) );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
the_content();
global $post;
$custom = get_post_custom($post->ID);
echo '<div>';
the_content();
echo '</div>';
endwhile;
?>

Than open your appearance-Menu and add the Art page in navigation or main menu.

Using this trick you can easily add the custom post type in wordpress navigation.

How to add custom post type to wordpress menu
How to add custom post type to wordpress menu

implement jquery UI datepicker in wordpress

wordpress and jquery both are powerful tool. In plugin we need datepicker sometime. In this article i showed how to implement jquery UI datepicker in wordpress

implement jquery UI datepicker in wordpress

Some WP developers use the plugins to add the datepicker in wordpress. But you can add the Jquery datepicker in wordpress. How can use the Jquery UI in your wordpress theme and plugin using following code.  For adding the datepicker in theme you need to just add the following code in functions.php file.

[viral-lock message=”Some Source code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]


add_action( 'init', 'wpapi_date_picker' );
function wpapi_date_picker() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-ui-core' );
    wp_enqueue_script( 'jquery-datepicker', 'http://jquery-ui.googlecode.com/svn/trunk/ui/jquery.ui.datepicker.js', array('jquery', 'jquery-ui-core' ) );
}

add_action( 'wp_footer', 'wpapi_print_scripts');
function wpapi_print_scripts() {
    ?>
<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#datepicker').datepicker();
    })
</script>
    <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
}

[/viral-lock]

For showing the datepicker control in theme or plugin use following code.

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#datepicker').datepicker({
        dateFormat : 'yy-mm-dd'
    });
});
</script>
implement jquery UI datepicker in wordpress
implement jquery UI datepicker in wordpress

If you have any issue for adding the datepicker control then please add comment or email me.

add css and javascript file to admin in wordpress theme

WordPress Tutorial, add css and javascript file to admin in wordpress theme. Many developers want to add the javascript and css files into wordpress themes.

add css and javascript file to admin in wordpress theme

You just need to add following code into functions.php file.

// Register your javascript for properties
function admin_your_javascript(){
    global $post;
    if($post->post_type == 'post-type' && is_admin()) {
        wp_enqueue_script('YOUR-JS', WP_CONTENT_URL . '/themes/YOUR-THEME/js/YOUR-JS.js');

    }
}
add_action('admin_print_scripts', 'admin_your_javascript');

// Register your styles for properties
function admin_your_styles(){
    global $post;
    if($post->post_type == 'post-type' && is_admin()) {
        wp_enqueue_style('YOUR-CSS', WP_CONTENT_URL . '/themes/YOUR-THEME/css/YOUR-CSS.css');
    }
}
add_action('admin_print_styles', 'admin_your_styles');

You just need to replace Your theme name and javascript and css file name.

add css and javascript file to admin in wordpress theme
add css and javascript file to admin in wordpress theme

How to include jquery ui in wordpress

When you enqueue your script, it enqueues for the whole site including admin panel. Using wordpress hook you can include jquery ui in wordpress website. If you don’t want the script in the admin panel, you can only include them for the site in frontend.

How to include jquery ui in wordpress

This following code you need to add in functions.php file.

function my_add_frontend_scripts() {
    if( ! is_admin() ) {
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui-core');
    }
}
add_action('init', 'my_add_frontend_scripts');
How to include jquery ui in wordpress
How to include jquery ui in wordpress

Then you are able to see the jquery-ui in your wordpress theme.

Send email through wp_mail in html format with wordpress

If you have mail server then you should use the wp_mail wordpress plugin. Information about Send email through wp_mail in html format with wordpress is easy. Emails are very important feature for now in the world so you need to understand better the wordpress mail functionality.

In wordpress we use the wp_mail function for sending email. Following parameters we use for wp_mail. WP developers wants to send email through wordpress.

Send email through wp_mail in html format with wordpress

Here is some brief and introduction about wp_mail method of wordpress. wordpress itself uses the PHPmailer for sending emails.


Parameters

$to
    (string or array) (required) The intended recipient(s). Multiple recipients may be specified using an array or a comma-separated string.

$subject
    (string) (required) The subject of the message.

$message
    (string) (required) Message content.

$headers
    (string or array) (optional) Mail headers to send with the message. (advanced)

$attachments
    (string or array) (optional) Files to attach: a single filename, an array of filenames, or a newline-delimited string list of multiple filenames. (advanced)

Simple example:

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

<?php
   $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip');
   $headers = 'From: My Name <myname@mydomain.com>' . "\r\n";
   wp_mail('test@test.com', 'subject', 'message', $headers, $attachments);
?>

[/viral-lock]

Send email through wp_mail in html format with wordpress
Send email through wp_mail in html format with wordpress

We can use this code for contact form or in functions.php file

For html format email from wordpress. we just need to add following line in functions.php file.

add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));

Sending HTML email has risks. It could be caught in spam filters. The client may not support HTML formatting (although that’s rare). The client may disable email HTML from using javascript, CSS or grabbing remote assets like images.

In wordpress add tags and categories to custom post type

Many times we use the custom post type in wordpress. Wordpres tutorial, In wordpress add tags and categories to custom post type.  Some times we need to category and tags for custom post type which associated.

In wordpress add tags and categories to custom post type

you can use the following code in functions.php file.

// === CUSTOM POST TYPES === //
function create_my_post_types() {
	register_post_type( 'Services',
		array(
			'labels' => array(
				'name' => __( 'Services' ),
				'singular_name' => __( 'Services' ),
				'add_new_item' => 'Add New Services',
				'edit_item' => 'Edit Services',
				'new_item' => 'New Services',
				'search_items' => 'Search Services',
				'not_found' => 'No Services found',
				'not_found_in_trash' => 'No Services found in trash',
			),
			'_builtin' => false,
			'public' => true,
			'hierarchical' => false,
			'taxonomies' => array( 'website_type', 'service'),
			'supports' => array(
				'title',
				'editor',
				'excerpt',
                                'thumbnail'
			),
			'rewrite' => array( 'slug' => 'services', 'with_front' => false ),

		)
	);
}
add_action( 'init', 'create_my_post_types' );

// === CUSTOM TAXONOMIES === //
function my_custom_taxonomies() {
	register_taxonomy(
		'website_type',		// internal name = machine-readable taxonomy name
		'Services',		// object type = post, page, link, or custom post-type
		array(
			'hierarchical' => true,
			'label' => 'Website Types',	// the human-readable taxonomy name
			'query_var' => true,	// enable taxonomy-specific querying
			'rewrite' => array( 'slug' => 'website_type' ),	// pretty permalinks for your taxonomy?
		)
	);
	register_taxonomy(
		'service',
		'post',
		array(
			'hierarchical' => false,
			'label' => 'Service',
			'query_var' => true,
			'rewrite' => array( 'slug' => 'service' ),
		)
	);

}
add_action('init', 'my_custom_taxonomies', 0);

Please consult with your developer.

In wordpress add tags and categories to custom post type
In wordpress add tags and categories to custom post type

 

……………………..

solved wordpress custom post type pagination not working

From wordpress 3.0 version, wordpress started the custom post type functionality. There are API provided by wordpress for adding the pagination for custom post type. Some WP developers asked me about pagination of custom post type. That is very easy to add the pagination. Here in this article I given the code snippet for showing the pagination in custom post type.

solved wordpress custom post type pagination not working

For showing the custom post type we always use the query_post method.

Just use the following code in your template or theme file.

<?php get_header(); ?>

            <?php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

//loop here

              			<?php endwhile; ?>
				  <div id="nav-below" class="navigation">
					<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
					<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
				  </div><!-- #nav-below -->
				  <?php endif; wp_reset_query(); ?>

<?php get_footer(); ?>
solved wordpress custom post type pagination not working
solved wordpress custom post type pagination not working

Have fun!

How to remove first image from wordpress post with caption

I struggled lot for this issue. Some times I only removed the first image. some times I removed first caption. Using following code you can easily remove the post first image and caption.

How to remove first image from wordpress post with caption if caption is present

I used the following code for first image removing.

/*
 * Removing the first image from post
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
		 $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
 	}
	return $content;
   }
}

But I faced the some issues with that code. When first image has caption then only image get removed and caption will remain there.

After that I used the following code for caption remove.

/*
 * Removing the first caption from post with image
 */
function remove_first_image_caption ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
 $content = preg_replace("(\)", "", $content, 1);
 } return $content;
}
add_filter('the_content', 'remove_first_image_caption');

But if first image has no caption and second image image has caption then both the images will be get removed from post.

After some struggle I written following code. Following is the solution:

Final Code.

/*
 * Removing the first image from post
 * functionality added to delete caption is present to first image.
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
    //find first image URL
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $content, $matches);
     $first_img = $matches[1][0];

    //find first image caption inner text
     $output = preg_match_all("/caption=['"](.*)/", $content, $matches);

     //find first image present in first caption text or not
        $pos = strpos($matches[0][0], $first_img);

    // if image URL found in caption array then delete the caption and image
        if ($pos !== false) {
           $content = preg_replace("(\)", "", $content, 1);
        } else {
           $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
        }
    }

 } return $content;
}
add_filter('the_content', 'remove_first_image');

solved: pagination for Custom post type not working

Some people asked me about pagination of custom post type. That is very easy.

For showing the custom post type we always use the query_post method.

Just use the following code in your template or theme file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_header(); ?>

            <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

loop here

              			<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>
				  <div id="nav-below" class="navigation"></pre>
<div class="nav-previous"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?></div>
<pre></pre>
<div class="nav-next"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
<pre>
				  </div><!-- #nav-below -->
				  <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; wp_reset_query(); ?>

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_footer(); ?>
remove first image from wordpress post with caption
remove first image from wordpress post with caption
How to remove first image from wordpress post with caption if caption is present

wordpress pagination with query posts

wordpress pagination with query posts, Mostly people use query_posts function for fetching posts. But most of all people face issue with pagination.

wordpress pagination with query posts

Just use following code for pagination. It is simple and easy.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'post_type=documents&paged='.$paged );
query_posts( 'post_type=post&paged='.$paged );
if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php the_excerpt();  ?>

 <?php endwhile; ?>
<div id="nav-below" class="navigation">
                                        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
                                        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
                                </div><!-- #nav-below -->
                                  <?php endif; wp_reset_query(); ?>

wordpress pagination with query_posts
wordpress pagination with query_posts

show posts to only registered users in wordpress

Some people want to show posts to only registered users in wordpress in site. You just need to create category and publish your posts in private category.

How to show posts to only registered users in wordpress

Use the following code in index.php and single.php and page.php file.


$cat_id = get_cat_ID('private');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

if(!is_user_logged_in()){
    $cat_id = get_cat_ID('private');
   $args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

} else {
    $cat_id = get_cat_ID('public');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

}
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
	echo '<li>';
	the_title();
	echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
How to show posts to only registered users in wordpress
How to show posts to only registered users in wordpress