get recent comments without wordpress plugin

Showing the most recent comments is great for SEO purpose. Our code snippet will able to get recent comments without wordpress plugin in your wordpress site.

get recent comments without wordpress plugin

You can display the most recent comments of your entire blog without any wordpress plugin. Showing the most recent comments is great for SEO purpose. Your site will increase the visibility and people interaction. Socially you and your blog will became more famous. Using following code snippet you will be able to add the recent comments in your wordpress site.

I want to display the comments in footer area. I created following code. You can copy paste the code in your sidebar.php or footer.php file.

Using following code you are able to display recent comments with author’s gr-avatar also.


<?php
$total_comments = $wpdb->get_results("SELECT comment_date_gmt, comment_author, comment_ID, comment_post_ID, comment_author_email FROM $wpdb->comments WHERE comment_approved = '1' and comment_type != 'trackback' ORDER BY comment_date_gmt DESC LIMIT 10");
$comment_total = count($total_comments);
echo '<ul>';
for ($comments = 0; $comments < $comment_total; $comments++) {
echo "<div style='clear:both;width:355px;padding-top:3px;'><div style='float:left;width:35px;'>";
echo get_avatar($total_comments[$comments]->comment_author_email,$size='32',$default='<path_to_url>' );
echo "</div> <div style='width:320px;'>";
echo '<li>';
echo $total_comments[$comments]->comment_author . ' says ';
echo '<a href="'. get_permalink($total_comments[$comments]->comment_post_ID) . '#comment-' . $total_comments[$comments]->comment_ID . '">';
echo get_the_title($total_comments[$comments]->comment_post_ID);
echo '</a></li></div></div>';
}
echo '</ul>'
?>

My footer is looking like as follows:

get recent comments without wordpress plugin
get recent comments without wordpress plugin

how to remove default post and comment from wordpress

remove default post and comment from wordpress, When we install the wordpress then wordpress put the dummy post and comment called “hello world”.

how to remove default post and comment from wordpress

If we want to remove that you can easily done this. Using following wordpress plugin.

Delete Default Post

Download the wordpress plugin

This plugin will delete the default post and comments which is inserted by wordpress.

how to remove default post and comment from wordpress
how to remove default post and comment from wordpress

How to use pagination in wordpress post without plugin

There is difference between post pagination and in post pagination.If you want to keep pagination in wordpress post without plugin, Than use our code

When we are developing the new wordpress theme then we need to always need to think about post pagination and in post pagination. Yes, there is difference between post pagination and in post pagination.

How to use pagination in wordpress post without plugin

How to use pagination in wordpress post without plugin
How to use pagination in wordpress post without plugin

If you want to keep single post and pagination between in single post that is also possible through wordpress api. In this article I will show you hwo to use the in page or post pagination.

Open your single.php file from wordpress theme folder and put following code in that.

<?php wp_link_pages( ); ?>

We can pass the following parameter the this function.

<?php $args = array(
    'before'           => '<p>' . __('Pages:'),
    'after'            => '</p>',
    'link_before'      => ,
    'link_after'       => ,
    'next_or_number'   => 'number',
    'nextpagelink'     => __('Next page'),
    'previouspagelink' => __('Previous page'),
    'pagelink'         => '%',
    'more_file'        => ,
    'echo'             => 1 ); ?>
<?php wp_link_pages( $args ); ?>

This is the one of the sample code.

<?php wp_link_pages('before=</pre>
&after=
<pre>&next_or_number=number&pagelink=page %'); ?>

Now I will explain What wordpress doing for using this function. WordPress is using simple explode php function for using the next page tag. Like following way.

$content = get_the_content();
$pages = explode('<!--<span class="hiddenSpellError" pre="" data-mce-bogus="1">nextpage</span>-->', $content);

you can also use the above code in your loop.

how to display all months wordpress posts on homepage

In this article, we will show you how to retrieve wordpress posts within specific time frame and display all months wordpress post. code snippet with detail.

how to display all months wordpress posts on homepage

how to display all months wordpress posts on homepage
how to display all months wordpress posts on homepage

WordPress is providing as full proof wordpress api so we can achieve the this using wordpress api only.

You can use the query_posts() function for fetching the wordpress posts. Open your index.php from your wordpress template folder. Before loop just use the following code for in index.php file.


$current_date = getdate();
query_posts(monthnum=' .$current_date["mon"] );

You can pass the multiple parameters to query_posts function as per your requirement.

  • hour= – hour (from 0 to 23)
  • minute= – minute (from 0 to 60)
  • second= – second (0 to 60)
  • day= – day of the month (from 1 to 31)
  • monthnum= – month number (from 1 to 12)
  • year= – 4 digit year (e.g. 2009)
  • w= – week of the year (from 0 to 53)

Now I will show the another example. which will show only current date post on home page.


$current_date = getdate();
query_posts('year=' .$current_date["year"] .'&monthnum=' .$current_date["mon"] .'&day=' .$current_date["mday"] );

Another good example for fetching the 30 days latest post from wordpress

<!--?php
//based on Austin Matzko's code from wp-hackers email list
  function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date --> '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Using above code you can display all months wordpress posts.

How to customize the comments template or wp_list_comments()

In single.php file we have function to add comments functionality. Here in this article I will give the code for customize the comments template.

From wordpress 2.7 version we got the functionality of changing or customizing the comments template as per your theme. As we all know in single.php file we called following function to add comments functionality. wordpress comments is very important functionality. Here in this article I will give the code for customize the comment template.

customize the comments template


<?php comments_template(); ?>

This function basically calls the comments.php file and add the data from wordpress database.

If we check the comments.php file. Usually we are having following content in comments.php file

[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

// Do not delete these lines
 if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
 die ('Please do not load this page directly. Thanks!');

 if ( post_password_required() ) { ?>
 <p>This post is password protected. Enter the password to view comments.</p>
 <?php
 return;
 }
?>

<!-- You can start editing here. -->
<div id="comment">
<?php if ( have_comments() ) : ?>
 <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>

 <ol>
 <?php wp_list_comments('callback=wordpressapi_comments'); ?>

 </ol>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>
 <?php else : // this is displayed if there are no comments so far ?>

 <?php if ('open' == $post->comment_status) : ?>
 <!-- If comments are open, but there are no comments. -->

 <?php else : // comments are closed ?>
 <!-- If comments are closed. -->
 <p>Comments are closed.</p>

 <?php endif; ?>
<?php endif; ?>

<?php if ('open' == $post->comment_status) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

<div>
 <small><?php cancel_comment_reply_link(); ?></small>
</div>

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( $user_ID ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : ?>

<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>

</form>

<?php endif; // If registration required and not logged in ?>
</div>

<?php endif; // if you delete this the sky will fall on your head ?>
</div>

In the comments loop for showing the comments we used the wp_list_comments() function. Use this for customize the comments template.

We can customize this function as per our requirement. We can pass the following parameters to this function. use following for customize the comments template.


[/viral-lock]

For customize the comments template following parameter is important.

callback (string) The name of a custom function to use to display each comment. Defaults to null. Using this will make your custom function get called to display each comment, bypassing all internal WordPress functionality in this respect. Use to customize comments display for extreme changes to the HTML layout. Not recommended.

In my theme I used that in following way. I changed and passed parameter in comments.php file.


<?php wp_list_comments('callback=wordpressapi_comments'); ?>

Then Open the functions.php file put following code in that. This code I am using as per wordpress api.


function wordpressapi_comments($comment, $args, $depth) {
 $GLOBALS['comment'] = $comment; ?>
 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
 <div id="comment-<?php comment_ID(); ?>">
 <div>
 <?php echo get_avatar($comment,$size='32',$default='<path_to_url>' ); ?>
 <?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>
 </div>
 <?php if ($comment->comment_approved == '0') : ?>
 <em><?php _e('Your comment is awaiting moderation.') ?></em>
 <br />
 <?php endif; ?>

 <div><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),&nbsp; get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'&nbsp; ','') ?></div>

 <?php comment_text() ?>

 <div>
 <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
 </div>
 </div>
<?php
 }

Many times some people add the full URL of website in the name parameter. That time showing author name of comment writer is looks so ugly. So I used following trick in this function. Using this line of code we can show only 20 characters of author.


<?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>

I changed the this function as per my choose. You can customize this function as per your theme requirement.

customize the comments template
customize the comments template

If you found any issue in this article “customize the comments template” about this. please write comments.

 

wordpress migration to new hosting service or domain

how to do wordpress migration to new hosting service – This question always came to web developers. After searching on Internet I really did not found satisfied answer for this question. Using our steps wordpress migration to new hosting service or domain made easy

wordpress migration to new hosting service or domain

Earlier I did wordpress migration to new hosting service or new domain so many times. It was like 5 minutes job for any web developer. Don’t be afraid about wordpress migration at all.

Many people suggest to use wordpress utility which import and export xml. This is given by wordpress itself. But I strongly say don’t use that tool. that is not really full proof.

Just follow my steps to do wordpress migration to new hosting or domain.

1. First take backup of file system of your wordpress website using FTP credential.

2. Open wp-config.php file and change database name(what you want)

3. wp_options -Table name
option_name column – change(siteurl and home)

4. wp_blogs- Table name
domain column – change to your domain name

5. wp_site- Table name
domain column – change to your domain name

6. wp_usermeta
meta_key column – change(source_domain)

Most important part of migration. Fixing the image path of wordpress website

7. Just run following query using phpmyadmin or command prompt and any sql editor.

UPDATE wp_posts SET post_content = REPLACE(post_content, ’NEW_DOMAIN.com’, ‘OLD_DOMAIN.com’);

For changing the mysql queries you can use the following URL:

https://purabtech.in/mysql-queries-wordpress-website-migration/

Follow above steps and your are done with migration.

wordpress migration to new hosting service or domain
wordpress migration to new hosting, question always came to web developers. Using our steps wordpress migration to new hosting service or domain made easy

That sit!

fetch wordpress posts which starting with specific word or alphabet

If you want to fetch wordpress posts which is starts with specific word or alphabet. That is really easy to achieve in wordpress.

Article for fetch wordpress posts which starting with specific word.

Just follow my steps:

Open your functions.php file from your wordpress theme folder.


add_action( 'posts_where', 'starting_word' );
function starting_word( $sql ){
 global $wpdb;
 $starting_word = get_query_var( 'starting_word' );

 if( $startswith ){
 $sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $starting_word.'%' );
 }

 return $sql;
}

Open your index.php file or any file where you want show the wordpress posts which is starting with specific word or alphabet.

Use following code in that file.


<?php  query_posts('paged='.$paged.'&starting_word=wordpressapi&category_name=Your Category&posts_per_page=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

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

<br />
<?php the_content(); ?>
<?php endwhile; ?>

fetch wordpress posts which starting with specific word
fetch wordpress posts which starting with specific word

How to find wordpress posts by category name

You can fetch the wordpress posts by category name. We have given simple code snippet for find wordpress posts by category name.  Many developer want to do this. I will show how easily achieve this.
You can control the per page post showing and pagination also. You can use following code in any of your page.

How to find wordpress posts by category name

Use following code :

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php  query_posts('paged='.$paged.'&category_name=Your Category&posts_per_page=10'); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php while ($my_query->have_posts()) : $my_query->the_post(); ?>

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

<br />
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php the_content(); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>

find wordpress posts by category name
find wordpress posts by category name

how can we execute custom queries on wordpress

We can use the custom queries in wordpress. Using the custom queries we can fetch the wordpress table data as per our custom requirement.

how can we execute custom queries on wordpress

We you create your table then you can fetch values from that table using following command.

$ourdata = $wpdb->( "SELECT id, name FROM yourtable" );

If you want to create the table in wordpress then use following article.

https://purabtech.in/create-add-tables-wordpress-theme/

More example about executing the own custom quries.

$wpdb->query("
	DELETE FROM $wpdb->postmeta WHERE post_id = '123'
	AND meta_key = 'google'");

another example about adding the data into table which is from wordpress:

$table_name = "your_table";
 $welcome_name = "Mr. WordPressapi";
  $welcome_text = "Congratulations, you just completed the installation!";

  $insert = "INSERT INTO " . $table_name .
            " (time, name, text) " .
            "VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";

  $results = $wpdb->query( $insert );

Getting the single value from any table

$user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));

Get the single row value from wordpress table

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);
how can we execute custom queries on wordpress
how can we execute custom queries on wordpress

you can use inner join, outer join and multiple join in wordpress custom queries.

Some best WordPress Hacks for theme developer

there are many wordpress theme deverlopers who are looking for wordpress hacks for theme developer. we have best collection of wordpress hacks for theme developer. Some best WordPress Hacks for theme developer.

wordpress hacks for theme developer

 wordpress hacks for theme developer
wordpress hacks for theme developer

WordPress themes market has grown incredibly. Due to lot of amazing new functionality an synchronization with all social sites.

1. Create TinyURLs with your wordpress site
Open your single.php file and paste the following in the loop:


<?php
echo 'Tiny Url for this post: <a href="'.bloginfo('siteurl').'?p=.'$post->ID.'">'.bloginfo('siteurl').'?p=.'$post->ID.'</a>'
?>

2. Use The Loop To Create An “Archive” Page Template

Create the php page called archive.php and create archive page through wordpress admin and select the archive page template from sidebar option.

In archive.php file you can following code:

<?php
/*
Template Name: Archives
*/
?>

<?php get_header(); ?>

<h2><?php $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts); ?>
<h2><?php echo $numposts.' recipes published since October 06, 2008'; ?>
</h2>

<ul id="archive-list">
<?php
$myposts = get_posts('numberposts=-1&');
foreach($myposts as $post) : ?>
<li><?php the_time('m/d/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

3. Create A “Share the article with Facebook” Button
Use the following code in your loop where you want show the facebook share button.

<a href="http://www.facebook.com/sharer.php?u=<?php get_permalink();?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>

4. Use More Than One Loop On A Page, Without Printing Duplicate Posts

If you want to use two query post in page or post. you can use following code:

<?php
query_posts('showposts=8');
$ids = array();
while (have_posts()) : the_post();
$ids[] = get_the_ID();
the_title();
the_content();
endwhile;
?>

<?php
query_posts(array('post__not_in' => $ids));
while (have_posts()) : the_post();
the_title();
the_content();
endwhile;
?>

5. Insert Ads After The First Post
If you want show the ad after first post then use the following code:

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 2) : ?>
//Paste your ad code here
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

There are so many wordpress tricks and hack you can found in this website. Search more.