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.
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.”]
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.
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.
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]
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.
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.
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.
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.
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.
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.