Friday, 23 October 2020

WordPress Post Views Count Without Plugin

 Step1"
Add this code in current WordPress active theme in function.php file

<?php

function get_PostViews($postID){
    $count_key = 'post_views_count';
    $post_count = get_post_meta($postID, $count_key, true);
    if($post_count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $post_count.' Views';
}

function set_PostViews($postID) {
    $count_key = 'post_views_count';
    $post_count = get_post_meta($postID, $count_key, true);
    if($post_count==''){
        $post_count = 0;
       delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $post_count++;
        update_post_meta($postID, $count_key, $post_count);
    }
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>

 Step2:
Now add this function code in active themes in single.php file within the loop. It will manage the views and set the post views of each post.

<?php
 set_PostViews(get_the_ID());
?>

 Step3:
Add this code line in inside the post loop where you want to display post count.

<?php
 echo get_PostViews(get_the_ID());
 ?>


Saturday, 12 September 2020

How to Custom Pagination For Custom Post in WordPress?

 

There are very simple steps for Pagination for any type of Post in WordPress by Shortcode with Ajax.

  1.  Write this below code in you active WordPress theme's funcntion.php file.
    • ie: \wp-content\themes\twentytwenty\functions.php
  2.  Put this shortcode in your page where you want to display WordPress Custome Post or Post with Ajax Pagination.
    • <?php echo do_shortcode('[php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"]'); ?> OR
    • [php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"]
### Code: ###
<?php
function function_php_kishan_wp_portfolio_post_list($atts, $content = null) {
    ob_start();
    $atts = shortcode_atts(
    array(
        'per_page' => '',
        'total' => '',
        'post_type' => '',
    ), $atts);
    $per_page = $atts['per_page'] ? $atts['per_page'] : '9';
    $total_post = $atts['total'] ? $atts['total'] : '-1';
    $post_type = $atts['post_type'] ? $atts['post_type'] : 'post';

    global $wpdb;
    $post_type = $post_type; // define your custom post type slug here
    // A sql query to return all post titles
    $results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = '$post_type'  and post_status = 'publish' order by post_title ASC limit 0,$per_page");
.
.
.
.
return ob_get_clean();
}
add_shortcode('php_kishan_wp_portfolio_post_list', 'function_php_kishan_wp_portfolio_post_list');
#### [php_kishan_wp_portfolio_post_list per_page="30" total="90" post_type="post"] ####

Click here for download full code.

Saturday, 29 August 2020

How To Fix Email Spam Issue In Contact Form 7?

 Some times Contact Form 7 gives 

Error: An error occurred while trying to send your message. Please try again later.

on form submit.

That time you need to add this belove filter in functions.php which themes is active in your WordPress site.

       add_filter('wpcf7_spam', '__return_false');




Tuesday, 11 August 2020

How To Search In PHP Array Element Containing String | Like Binod


$example = array(
        "who" => "Who is Binod?",
        "name" => "Dose Binod Tharu is Binod?",
"where" => "Where is Binod?",
"whose" => "Whose name is binod?",
"YouTube" => "Dose Binod in the YouTube?",
"meaning" => "What is Binod meaning?",
"fullform" => "What is Binod full form?",
"pronunciation" => "What is Binod pronunciation?",
"english" => "Dose Vinod in english",
"how" => "How to write Vinod in sanskrit?",
"nickname" => "Which best nickname for Vinod?",
"kon" => "Binod kon he?",
"kaha" =>"Binod kaha he?",
"kiska" =>"Binod kiska name he?"
);

$searchword = 'Binod';

$search_results = array_filter($example, function($var) use ($searchword) {
return preg_match("/\b$searchword\b/i", $var); 
});

echo "Search Word: ".$searchword;
echo "<br>Search Results";
echo "<pre>";
echo "Total Search: ".count($search_results);
echo "<hr>";
print_r($search_results);
echo "</pre>";

OutPut:

Wednesday, 5 August 2020

How To Create A Navigation Menu In WordPress?

There are very simple steps to Create A Navigation Menu in WordPress.

1. You need to go in the Appearance » Menus page in your WordPress admin dashboard. 2. You can choose the pages you want to add to the menu. 3. Menu Settings » Select Display location. 4. Save menu. 5. You can see on your WordPress Website after refreshing any page. 

You can see Create A Navigation Menu tutorial on the below Video.