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.

get div height using javascript when div height is auto

We need to get the div height and width when these properties not defined. Here in this article, get div height using javascript when div height is auto. Many times we need to get the div or any DOM elements height and width when these properties not defined.

get div height using javascript when div height is auto

Lets say I kept div height auto and width as 200px fix and data is dynamic for eash user. So how we can get the div height.

take div height from javascript
take div height from javascript

We can use two javascript functions for fetching the div height.
1. offsetHeight – this will return the value which are specified in your CSS

2. clientHeight – this function will return real inner height of element that does not matter about CSS.
I recomend to use this function.

Exmaple code as follows:

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

<style>
#wordpressapidiv{ width:200px; height:auto;border:#ccc 1px solid}
</style>
wordpressapidiv">
This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content.
This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content.

This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content. This is dummy content.
This is dummy content. This is dummy content.
</div>

<script>
function getDivHeight(divID){
gd = document.getElementById(divID).clientHeight;
alert(gd);
}

function getDivWidth(divID){
gw = document.getElementById(divID).clientWidth;
alert(gw);
}

</script>

<input name="test" type="button" onclick="getDivHeight('wordpressapidiv')" >

[/viral-lock]

Function above code you can fetch the div height and width using the javascript.

Solved : fopen and fread not working with url in php

Many times we normally face problems while reading a file using url. Due to wrong PHP configuration we are facing fopen and fread not working with url issue.

fopen and fread not working with url

Using following solution we can easily fix this PHP issue.
For e.g :
$url = ‘http://somesite.com/somefile.txt’;
$fp = fopen($url,”r”);

To allow the fopen to get the contents of the file through url, do the following settings in php.ini file :

allow_url_include = On

By setting above 2 directives to ‘On’, fopen will be able to read the contents from url.

If you are using wamp than we can change this setting by on clicking on wamp icon and than go to PHP and click on setting > and allow_url_fopen option setting.

For linux users, you need to change /etc/php.ini file and do not forget to restart to apache server.

How to execute linux commands using php

Many times we need to run the linux commands using PHP language. We are going to give you code to, execute linux commands using php. This is my favorite question about PHP when I take an interview of new PHP developer.

execute linux commands using php
execute linux commands using php

In this article I will show you how to execute the linux command using PHP language syntax or script.

You can execute linux commands within a php script – all you have to do is put the command line in backticks (”) ,exec() , and shell_exec().

$command = exec('linux command');
echo $command;
echo shell_exec('ll');

How to read xml using php

Many new PHP developer looking for how to easily read the xml file. In this tutorial I will show you how to read the xml file using PHP language. Here I given the sample code for parsing the XML using PHP.

How to read xml using php


This is my xml file format and file name is readxml.xml file


<?xml version="1.0"?>

<!-- our XML-document describes a purchase order -->
<purchase-order>

 <date>2005-10-31</date>
 <number>12345</number>

 <purchased-by>
 <name>My name</name>
 <address>My address</address>
 </purchased-by>

 <!-- a collection element, contains a set of items -->
 <order-items>

 <item>
 <code>687</code>
 <type>CD</type>
 <label>Some music</label>
 </item>

 <item>
 <code>129851</code>
 <type>DVD</type>
 <label>Some video</label>
 </item>

 </order-items>

</purchase-order>

This is one php file called test.php and code as follows


<?php
 //create new document object
 $dom_object = new DOMDocument();
 //load xml file
 $dom_object->load("test.xml");

 $item = $dom_object->getElementsByTagName("item");

 foreach( $item as $value )
 {
 $codes = $value->getElementsByTagName("code");
 $code  = $codes->item(0)->nodeValue;

 $types = $value->getElementsByTagName("type");
 $type  = $types->item(0)->nodeValue;

 $labels = $value->getElementsByTagName("label");
 $label  = $labels->item(0)->nodeValue;

 echo "$code - $type - $label <br>";
 }
?>

When you run the readxml.php file. you will see the following output.

687 – CD – Some music
129851 – DVD – Some video

Solved : Kill command is not working on linux or Fedora

Many times this issue is happened with me I tried the kill command to stop the process. Solved, Kill command is not working on linux.
But that is really not responding. For Example, following command executed but not worked.

Kill command is not working on linux

[siwan@siwank-pc ~]$ ps -ef|grep skype
siwan     3140     1  1 09:50 ?        00:04:46 skype
siwan    13857 13818  0 17:24 pts/1    00:00:00 grep skype
[siwan@siwank-pc ~]$ kill 3140

There are really very nice unix commands present in Linux like:

[siwan@siwank-pc ~]$ killall firefox

[siwan@siwank-pc ~]$ ps -ef|grep skype
siwan     3140     1  1 09:50 ?        00:04:46 skype
siwan    13857 13818  0 17:24 pts/1    00:00:00 grep skype

[siwan@siwank-pc ~]$ kill -9 3140

I specially like the kill -9 processid command. Using kill -9 on a process is like just hitting the power button on your running PC. You already know that powering off a running PC can damage your filesystem because of run-time inconsistent data, why would you do the same thing to your applications? You risk the _exact same_ data corruption.

Kill command is not working on linux
Kill command is not working on linux

How to setup Firefox Profile Manager on Fedora or Linux

How to set up Firefox Profile Manager on Fedora or Linux

Firefox has new functionality called Profile manager which will allow you to switch with multiple Users.
You can create many user profiles in Firefox and save that as per your choice.

setup Firefox Profile Manager on Fedora

Many times on same Firefox some user don’t want some addons or theme. That time we got some difficulties.
For avoiding this issue we can create the firefox profile in firefox.

In windows creating Firefox profile is very easy. Just open windows run window and type “firefox -P”. That sit!

You will see following screens.

Using this you can create the firefox profiles.

Now I am going to show you how to create the firefox profile in Fedora.

Open terminal and just type following command

[siwan@siwank-pc ~]$ /usr/lib/firefox-3.5.6/firefox -ProfileManager
[siwan@siwank-pc ~]$

setup Firefox Profile Manager on Fedora
setup Firefox Profile Manager on Fedora

See following screens.
Note: when you got “choose user profile” window. Don’t forget to uncheck the “Don’t ask startup” checkbox.

Note:  I am using the Fedora 11 here for this example.

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.

 

How to create golden text in photoshop

Here I am going to show you how we can make a Photoshop gold effect in few steps. Easily create golden text in photoshop using our example.

First select text tool & type here I am type gold select impact font so it will look dark.

create golden text in photoshop

Give background layer black color

Now we have to give stroke to our text

Select layer >> layer style >> blending option

In stroke select position outside & fill type gradient

And select gradient overlay & bevel & emboss

Our gold effect is almost create now

We have to give now golden color

Select

Layer >> new adjustment layer >> curves

Now select second layer

Go to layer >> new adjustment layer >> Hue/saturation

Your gold effect is done now

-But now we make some different it

We add diamonds in gold

For that select a diamond image+

Select edit >> define pattern

Now go to our first text layer create it duplicate

& this duplicate layer position it above of two adjustment layer

Now select blending option & uncheck all option

Select pattern overlay your diamond will perfectly added

create golden text in photoshop
create golden text in photoshop

Ruby performance improvement of 63% in 1.9.1

Ruby is very powerful object-oriented language. new Ruby performance improvement. Working with pure Ruby is really fun and interesting to me. I am the real fan of Ruby language.

Ruby performance improvement

I am working Ruby on Rails for around last four years. There is always I am thinking and all ROR developers are thinking about performance of application.

We always compare Ruby on Rails with PHP, Java, Python, dot net. We came to know that where we are lacking in ROR. Just one issue performance and memory issue.

When we got news about Ruby 1.9 is comming with performance improvement of 63%. Yes, It is not a joke. This is really great news for every ROR developers. Ruby 1.9 and Rails 3.0 is the future of internet.

Now I will talk about Ruby Software engineer who developed and worked on the Ruby 1.9.

Masahiro Kanai, who improved the performance of several methods in Ruby 1.9. He is the age of high school, just 17.

This year, Ruby 1.9 by the Fibonacci sequence of operations (multiple-precision addition) is,
Ruby 1.8 is slower than I realized Mr. Kanai is a challenge to freedom of choice camp Ruby faster.

In front of all the participants had a similar problem occurring, Ruby of type String, Array types (structures that may have an embedded data structure type object itself) and some of the faster method, attention collection. Oden’s is known to be well-covered tongue teachers. Ruby continued even after the camp’s efforts to speed up to approximately some methods to remove the macro in a constant loop of 63 percent to about 8 percent of success in speeding. This patch has been adopted by the community of developers Ruby, Ruby has been incorporated into the trunk (October 5, 2009).

many professional engineers and developers, with patience to hold out that the problem is resolved, probably as a programmer in one or two talents.

Ketai that builds skills and career tips for the professional engineer. Interviewers are already familiar with this series, the Mr. Ikuo Takeuchi, Professor Department of Creative Informatics, Graduate School of Information Science and Engineering, University of Tokyo. This time, who undertook the voluntary and discover if training for a talented programmer.

His mentor was Koichi Sasada (ko1). The performances of the methods he worked have been bumped up 63% in maximum, 8% in average. His patches were applied to Ruby trunk in Oct. 5 this year.

What he did for Ruby Performance tuning?

He took unnecessary macro references out from a loop. Masahiro spotted macros below in array.c, string.c, and struct.c were referred every time Ruby checked whether data was hold in a structure or not. Even though data were constants, Ruby saw the macros to judge data’s presence in every loop.

What I say now more… I am really happy and I can say this is the biggest news of 2010 in IT world.

One thing I want to mention here about Ruby on Rails. This thing is in mind of all ROR developers.

“Thank God, No need to look Jruby”

He took unnecessary macro references out from a loop. Masahiro spotted macros below in array.c, string.c, and struct.c were referred every time Ruby checked whether data was hold in a structure or not. Even though data were constants, Ruby saw the macros to judge data’s presence in every loop.