Archive

Posts Tagged ‘list’

Displaying WordPress Authors

February 26th, 2009 2 comments

WordPress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn’t be confused with the the_author_link() function that prints out the link in the author’s profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

<ul>
<?php wp_list_authors(); ?>
</ul>

This should print out a list of the authors on your blog. There is one very important thing I should mention here. This function will only print out an authors name if they have written a post and are not the default admin account. I tried to print out the authors on a test blog and after a few minutes of frustration I looked at the function call itself on line 460 of the file author-template.php in the directory wp-includes. The function specifically excludes authors that haven’t written any posts but you can turn this on if you give it the right parameter.

The wp_list_authors() function takes a number of arguments in the form of a string of arguments. The available arguments are as follows:

  • optioncount: Print out the number of posts for each author. The default is 0 (false).
  • exclude_admin: Print out the blog admin user. The default is 1 (true)
  • show_fullname: Use the full name of the author instead of their nickname. Default is 0 (false).
  • hide_empty: Show users who don’t have any posts. The default is 1 (true). Note that even if a user with no posts is printed they will not be linked.
  • echo: Immediately echo the list of authors or return as a variable. The default is 1 (true).
  • feed: If set then a link to an RSS feed for that author is printed, along with the text given. The default is not to print anything.
  • feed_image: This is a link to a RSS icon, it works in the same way as the feed parameter, but will override it.

To use the arguments you need to pass them to the function in the form of a string or arguments like this.

<?php wp_list_authors('exclude_admin=0&hide_empty=0&feed=Feed'); ?>

Now that you have a link of authors being printed out it is time to look at where the links go to. Each author has an ID associated with them, and this is appended to the URL using the author tag. If you have permalinks turned on then the authors name is used.

When clicked on WordPress will attempt to display the author information using a template file called author.php. If this is not present then it will display a list of the posts using the archive.php template file. Finally, if this is not present then WordPress will default to the index file.

Your author.php file should contain the following lines so that you can access the author informaiton.

<?php
if(isset($_GET['author_name'])) :
 $curauth = get_userdatabylogin($author_name);
else :
 $curauth = get_userdata(intval($author));
endif;
?>

The $curauth variable now contains an author object that can be used to access the various different data fields associated with authors. The following snippet will print out the authors name, profile and homepage, if this information is not filled in then nothing will be printed.

<h2><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?> (<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>)</h2>
<p><?php echo $curauth->user_description; ?></p>

You can also include a normal loop on this page to print out every post that the user has written.

For more information on the author templates (and a full list on the attributes available in the author object) take a look at the author templates section on the WordPress site.

Creating A Personalised Events List With SimplePie

November 28th, 2008 No comments

One nice feature for any blog or site (especially news sites) is to have a little list of forthcoming events that would interest your readers. One way of collating this information is to scour the web and update the list manually. However, this is time consuming and tedious, especially as there is an easier way to do it.

Have a look at the Yahoo site called Upcoming. On this site you can search for events on lots of different subjects in lots of different locations all over the world. Also, if you have an event you can put it up on the site.

Because all of the searches can be accessed via RSS it is possible to use SimplePie to collate these results together. The following script will take two arrays, one of locations and the other being the subjects. It will then create the necessary RSS URLs and use the multi-feed aggregator feature of SimplePie to collate all of the different events into a single array. All the script needs to do after this is run through the array and remove duplicates and sort the data by date.

<?php
include('simplepie.inc');
 
// enter a list of areas
$locations = array('england','britain','europe');
// enter a list of subjects
$subjects = array('php','web development','code','javascript');
 
// initialise feed array
$feeds = array();
 
// set up feed array
foreach($locations as $location){
  foreach($subjects as $subject){
    $feeds[] = 'http://upcoming.yahoo.com/syndicate/v2/search_all/?q='.urlencode($subject).'&loc='.$location.'&rt=1';
  }
}
 
// create SimplePie object
$feed = new SimplePie();
// add feeds to SimplePie
$feed->set_feed_url($feeds);
// turn off feed ordering by date
$feed->enable_order_by_date(false);
 
// get the feed contents
$feed->init();
 
// initialise previously done feed ID array
$prevIds = array();
 
// initialise item array
$list = array();
 
// for each feed item
foreach ( $feed->get_items() as $item ) {
  // check if we have not done this item before
  if ( !in_array($item->get_id(true), $prevIds) ) {
 
    // extract and convert the xCal:dtstart variable
    $when = $item->get_item_tags('urn:ietf:params:xml:ns:xcal', 'dtstart');
    $date = $when[0]['data'];
    $sortDate = SimplePie_Misc::parse_date($date);
    $gCalDate = date('j M Y', $sortDate);
 
    // check if the date is already in the list
    if ( isset($list[$sortDate]) ) {
      // try 10 attempts to increase the sort date timestamp and insert date
      for ( $i=0 ; $i < 10 ; ++$i ) {
        ++$sortDate;
        if ( !isset($list[$sortDate]) ) {
          $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
          break;
        }
      }
 
    }else{
    // store item in list array using the date as a
    $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
    }
    // store a hash of the ID so that the same thing isn't added twice.
    $prevIds[] = $item->get_id(true);
  }
}
 
// sort array by keys
ksort($list);
 
$list = array_slice($list,0,10);
 
echo '<ul>';
echo implode(' ',$list);
echo '</ul>';
 
?>

Rather than explain every step in turn I have just added comments to explain what is going on. Also, in this example I have searched for events in England and Europe that deal with php, web development, code and javascript. However, it seems that PHP isn’t such a good keyword to use as it comes up with any event that contains a link with a .php extension. I’m sure this case would be similar with things like HTML so it is best to avoid those keywords for now.

Categories: PHP Tags: , , , , , ,

Swap Values Without Temporary Varaibles In PHP

October 28th, 2008 No comments

I have talked about swapping values without a temporary third variable before, but there is another way to do this which doesn’t make the code unreadable. This is by using the list() function in the following way.

$a = 1;
$b = 2;
list($a,$b) = array($b,$a);

This swaps the two values over. The good thing about this function is that you can swap any number of values over without the need to create lots of confusing temporary variables or using complicated looking bitwise operators. Take 4 variables.

$a = 1;
$b = 2;
$c = 3;
$d = 4;

These values can be entirely swapped using the list() function in the following way.

list($a,$b,$c,$d) = array($d,$c,$b,$a);

It should be noted that this code isn’t faster than using temporary variables and so should only really be used to make it clear what is going on.

Categories: PHP Tags: , , , , ,

Loop Through All Files In A Directory With DOS Batch

September 16th, 2008 No comments

DOS Batch is the Windows equivalent of shell scripting and can be used to perform all sorts of different actions. Anything that you type into a DOS prompt on a Windows machine can be used in a bat file to quickly do something that you would otherwise have to repeat many times over.

To create a bat file just make a file and give it the extension "bat". If you run a DOS prompt and navigate to the directory that the bat file exists in you can type the name of the file to get it to do certain actions. If you called your file "action.bat" you can run it by typing "action" or "action.bat".

Starting with a simple example, if you want to print the contents of a file to screen then you need the type command, followed by the file.

type file.txt

However, this puts a lot of rubbish on the screen. If you wanted to create a backup of that file then you would write the following.

type file.txt > file_back.txt

This takes the contents of one file and puts it in another.

To loop through every file in a directory you need to use the following line.

FOR %%i IN (*.*) DO echo %%i

This code will loop through the contents of a directory and print out each file name to screen. This will also list the bat file that you put in the directory so another solution might be to run the bat file from the directory above and use the following code.

FOR %%i IN (directory\*.*) DO echo %%i

The following snippet of code takes the previous example and does something useful. It loops through the directory and puts every file name that it finds into a file called list.txt.

FOR %%i IN (directory\*.*) DO echo %%i >> list.txt

The >> symbol will append any content to the file, so for every iteration of the loop the list.txt file gets one line bigger, until all of the files have been listed. In an example directory the following would be seen in the list.txt file.

directory\directory.html
directory\match.xls
directory\directory.xls
directory\file.txt
directory\file_back.txt

Categories: DOS Tags: , , , , ,

Clean Up A Comma Separated List In PHP

August 1st, 2008 No comments

Getting users to enter a list of items is a normal practice, but getting users to do it properly is sometimes a challenge in itself. Some users will put spaces in between the commas, others will not, some will even put a trailing comma at the end of the list.

Here is some code that can be used to clear up a comma separated list using some simple regular expressions. It works using the preg_replace() function, and by passing this function an array of options patterns and an array of replacements.

$list= trim($list);
$patterns[0] = '/\s*,\s*/';
$patterns[1] = '/,{2,}/';
$patterns[2] = '/,+$/';
$patterns[3] = '/^,+/';
$replacements[0] = ',';
$replacements[1] = ',';
$replacements[2] = '';
$replacements[3] = '';
$list= preg_replace($patterns, $replacements, $list);

Take the following string, entered by a user at 2 o’clock in the morning, whilst surfing the net drunk after coming home from the pub.

, ,,,item 1,,, ,, ,,, , item 2, item 3 ,item4, ,,,,, item 5,,, ,, ,, , ,

This awful string gets converted to the following neat and tidy string.

item 1,item 2,item 3,item 4,item5

The code works by first replacing any spaces before or after commas, then by replacing multiple commas with single commas. Finally the code detects any commas at the start or end of the string and removes them.

Categories: PHP Strings Tags: , , , , , ,