Disable the content editor for a specific page template


Use this snippet in your theme’s functions.php file to disable the editor for a specific page template.

add_action( 'admin_init', 'hide_editor' );

function hide_editor() {
    // Get the Post ID.
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;

    // Get the name of the Page Template file.
    $template_file = get_post_meta($post_id, '_wp_page_template', true);
    
    if($template_file == 'contact.php'){ // edit the template name
        remove_post_type_support('page', 'editor');
    }
}

Date picker required doesn’t work in Contact Form 7


Date picker required doesn’t work in Contact Form 7

You need to edit core plugin files before that you need to save contact-form-7-datepicker folder for backup

You go to  wp-content/plugins/contact-form-7-datepicker/modules/date.php

Normal Code (Line Number :124)


if ('datetime*' == $type && empty($value)) {
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message('invalid_required');
}
if (! empty($value) && ! self::is_valid_date($value)) {
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message('invalid_datetime');
}

Change to below code

if ('date*' == $type && empty($value)) {
    $result['valid'] = false;
    $result['reason'] = array($name => wpcf7_get_message('invalid_required'));
}
if (! empty($value) && ! self::is_valid_date($value)) {
    $result['valid'] = false;
    $result['reason'] = array($name => wpcf7_get_message('invalid_datetime'));
}

Then I changed also time.php (from line 119) and datetime.php (from line 119) files in that similar way.
I hope this helps to you

How to get Custom Menu in Query Posts WordPress


<?php
    $args = array(
    'post_type' => 'nav_menu_item', 'orderby'   => 'menu_order',
    'order'  => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'nav_menu', 'field' => 'slug', 'terms' => 'my'        
        )
    )
);
query_posts($args);
while( have_posts()):the_post();
echo $post->ID;
endwhile; wp_reset_query();?>

Getting a Post’s Featured Image URL By Post ID


Getting a Post’s Featured Image URL By  Post ID

<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>

<img src="<?php echo $feat_image; ?>" width="80" height="100" />

If Featured Image else your default image:

check below code:

<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>

<?php if($feat_image) {?>
<img src="<?php echo $feat_image; ?>" width="80" height="100" />
<?php } else {?>
<img src="default.jpg" width="80" height="100" />
<?php } ?>

 

Display Child Pages title & content on Parent Page


Display all child and sub child pages using parent ID

This way the page that you are on will show in a post type the child pages of that page

 

<?php
$pages = get_pages('child_of=7&sort_column=post_date&sort_order=desc'); //Replace your parent id with 7
$count = 0;
foreach($pages as $page)
{
$content = $page->post_content;
?>
<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
<p><?php echo $content ?></p>
<?php
}
?>



Back to Top in Html(Jquery)


HTML wise all we need to do is add a “back to top” link at the bottom of the blog post

Below Jquery code add in your header file:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
jQuery(document).ready(function() {
    $(".top").hide();
    var offset = 820;
    var duration = 700;
    jQuery(window).scroll(function() {
        if (jQuery(this).scrollTop() > offset) {
            jQuery('.top').fadeIn(duration);
        } else {
            jQuery('.top').fadeOut(duration);
        }
    });

    jQuery('.top').click(function(event) {
        event.preventDefault();
        jQuery('html, body').animate({scrollTop: 0}, duration);
        return false;
    })
});
</script>

Below code add in footer file means end of page:
<a href="#" title="Back to Top" >Back to top</a>

Contact form 7 really simple captcha permission


After really simple captcha, if entered captcha correctly when click on submit button it’s take very long time to submit your details, because some of permission reason it is not creat captcha image in temp folder . Add below change in really simple captcha file.

Open really simple captcha folder from plugin folder -wp-content/plugin/really-simple-captcha/really-simple-captcha.php

really-simple-captcha.php:

old:

/* Image type. 'png', 'gif' or 'jpeg' */
        $this->img_type = 'png';

        /* Mode of temporary image files */
        $this->file_mode = 0444;

        /* Mode of temporary answer text files */
        $this->answer_file_mode = 0440;

Change to:

/* Image type. 'png', 'gif' or 'jpeg' */
        $this->img_type = 'png';

        /* Mode of temporary image files */
        $this->file_mode = 0777;

        /* Mode of temporary answer text files */
        $this->answer_file_mode = 0770;

Ajax not working in contact form 7


If ajax validation not woking in your theme, add below code into header.php above close  head [</head>]

 

<script type='text/javascript' src='/wp-content/plugins/contact-form-7/includes/js/jquery.form.min.js?ver=3.40.0-2013.08.13'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var _wpcf7 = {"loaderUrl":"\/wp-content\/plugins\/contact-form-7\/images\/ajax-loader.gif","sending":"Sending ..."};
/* ]]> */
</script>
<script type='text/javascript' src='/wp-content/plugins/contact-form-7/includes/js/scripts.js?ver=3.5.2'></script>

How to Display Post related Category Names


How to Display  Post related Category Names

<?php if ( is_object_in_taxonomy( get_post_type(), 'category' ) ) : // Hide category text when not supported ?>
            <?php
                /* translators: used between list items, there is a space after the comma */
                $categories_list = get_the_category_list( __( ', ', 'theme' ) );
                if ( $categories_list ):
            ?>
            <h4>
                <?php printf( __( '%2$s', 'theme' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list);
                  ?>
            </h4>
<?php endif; // End if categories ?>
 <?php endif; // End if is_object_in_taxonomy( get_post_type(), 'category' ) ?>

Query post page navigation in wordpress


Problem : Page navigation problem with custom query ?

Solution :  Find below

wp_Pagenavi    in query posts:

  1.         Download the Pagenavi plugin from the wordpress
  2.         Install plugin & active plugin
  3.         finally  use the below code     
<?php query_posts('showposts=6'.'&paged='.get_query_var('paged') ;?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content();?>
<?php endwhile; // end of the loop. ?>
<?php wp_pagenavi() //call pagenavi?>

How To Limit Words and Characters in content WordPress


How To Limit Words and Characters in content WordPress

1. Words Length:

Remove  the <?php the_content();?>  insert the below code

 <?php $content = get_the_content();
  $trimmed_content = wp_trim_words( $content, 60, '<a href="'. get_permalink() .'">...[ read more ]</a>' ); ?>
  <p><?php echo $trimmed_content; ?></p>
(Change the 60 to your desire words length)

 2. Character Length :

Remove  the <?php the_content();?>  insert the below code

<?php echo substr(strip_tags($post->post_content), 0, 46);?>
//(Change the 46 to your desire string length)

How to get custom values outside Loop


 How to get custom values outside Loop

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>

Example:

<?php 
global $wp_query;
$postid = $wp_query->post->ID;
$img = get_post_meta($postid, 'sidebar_image', true);
if($img)
{
echo "<img src ='$img'>";
}
else{
?>
<img src="<?php bloginfo("template_url")?>/images/care.jpg" />
<?php
}
wp_reset_query();
?>

How to Get the Dynamic Categories related Posts


How to  Get the Dyanmic Categories:

Here small snippet code is avialble for get categories related  posts

1. You have to give the page name and category name should be same

2. Add below code where you want..and than refresh page . you will get the post related categories.

<?php query_posts('category_name='.get_permalink().'&post_status=publish,future');?>

Function Reference – Content url


The content_url template tag retrieves the url to the content area for the current site with the appropriate protocol, ‘https’ if is_ssl() and ‘http’ otherwise.

WordPress Directories:
home_url() Home URL http://www.example.com
site_url() Site directory URL http://www.example.com or http://www.example.com/wordpress
admin_url() Admin directory URL http://www.example.com/wp-admin
includes_url() Includes directory URL http://www.example.com/wp-includes
content_url() Content directory URL http://www.example.com/wp-content
plugins_url() Plugins directory URL http://www.example.com/wp-content/plugins
theme_url() Themes directory URL
http://www.example.com/wp-content/themes
wp_upload_dir() Upload directory URL
http://www.example.com/wp-content/uploads

Query Posts


query_posts() is the easiest, but not preferred or most efficient, way to alter the default query that WordPress uses to display posts. Use query_posts() to display different posts than those that would normally show up at a specific URL. (The preferred way is hooking into ‘pre get posts’ and altering the main query that way using is main query)

For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don’t care about pagination), you can use query_posts() like so: query_posts( 'posts_per_page=5' );

Examples:

query_posts( 'cat=1&tag=apples' );

Exclude Categories From Your Home Page

Placing this code in index.php file will cause the home page to display posts from all categories except category ID 3.

<?php
if ( is_home() ) {
	query_posts( 'cat=-3' );
}
?>

You can also add some more categories to the exclude-list (tested with WP 3.3.1):

<?php
if ( is_home() ) {
	query_posts( 'cat=-1,-2,-3' );
}
?>

Retrieve a Particular Post

To retrieve a particular post, you could use the following:

query_posts( 'p=5' );

Note: If the particular post is an attachment, you have to use attachment_id instead of p:

query_posts( 'attachment_id=5' );

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

<?php
// retrieve one post with an ID of 5
query_posts( 'p=5' );

// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;

// the Loop
while (have_posts()) : the_post();
	the_content( 'Read the full post »' );
endwhile;
?>

WP-Quey – Post & Page Parameters


Display content based on post and page parameters.

  • p (int) – use post id.
  • name (string) – use post slug.
  • page_id (int) – use page id.
  • pagename (string) – use page slug.
  • post_parent (int) – use page id. Return just the child Pages.
  • post__in (array) – use post ids. Specify posts to retrieve.
  • post__not_in (array) – use post ids. Specify post NOT to retrieve.

Show Post/Page by ID

Display post by ID:

$query = new WP_Query( 'p=7' );

Display page by ID:

$query = new WP_Query( 'page_id=7' );

Show Post/Page by Slug

Display post by slug:

$query = new WP_Query( 'name=about-my-life' );

Display page by slug:

$query = new WP_Query( 'pagename=contact' );

Show Child Posts/Pages

Display child page using the slug of the parent and the child page, separated by a slash (e.g. ‘parent_slug/child_slug’):

$query = new WP_Query( 'pagename=contact_us/canada' );

Display child pages using parent page ID:

$query = new WP_Query( 'post_parent=93' );

Display only top-level pages, exclude all child pages:

$query = new WP_Query( 'post_parent=0' );

Multiple Posts/Pages Handling

Display only the specific posts:

$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );

Display all posts but NOT the specified ones:

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

Note: you cannot combine ‘post__in’ and ‘post__not_in’ in the same query.

WP Query – Category Parameters


  • cat (int) – use category id.
  • category_name (string) – use category slug (NOT name).
  • category__and (array) – use category id.
  • category__in (array) – use category id.
  • category__not_in (array) – use category id.

Show Posts for One Category:

Display posts that have this category (and any children of that category), using category id:

$query = new WP_Query( 'cat=4' );

Display posts that have this category (and any children of that category), using category slug:

$query = new WP_Query( 'category_name=staff' );

Display posts that have this category (not children of that category), using category id:

$query = new WP_Query( 'category__in=4' );

Show Posts From Several Categories

Display posts that have these categories, using category id:

$query = new WP_Query( 'cat=2,6,17,38' );

Display posts that have these categories, using category slug:

$query = new WP_Query( 'category_name=staff,news' );

Exclude Posts Belonging to Category

Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.

$query = new WP_Query( 'cat=-12,-34,-56' );

Multiple Category Handling

Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

You can also exclude multiple categories this way:

$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

WP Query in wordpress


Show Posts for one Author

Display posts by author, using author id:

$query = new WP_Query( 'author=123' );

Display posts by author, using author ‘user_nicename’:

$query = new WP_Query( 'author_name=rami' );

Show Posts From Several Authors

Display posts from several specific authors:

$query = new WP_Query( 'author=2,6,17,38' );

Exclude Posts Belonging to an Author

Display all posts except those from an author(singular) by prefixing its id with a ‘-‘ (minus) sign:

$query = new WP_Query( 'author=-12' );