Close

How to Create a Custom Loop in WordPress Using WP_Query

In this tutorial, I’ll show you how to create a custom loop in WordPress using WP_Query. Plus, I’ll explain why you want to use this instead of query_posts when creating custom loops like these.

<?php
/*** Custom Loop ***/
function demo_loop() {
$args = array(
'cat' => 3,
'posts_per_page' => 1
);

$demo_posts = new WP_Query($args);

if ( $demo_posts->have_posts() ) {
while( $demo_posts->have_posts() ) {
$demo_posts->the_post();

$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
}

return $output;
}

add_shortcode( 'demo_custom_loop', 'demo_loop' );

February 11, 2013

How to Create an ics Import File Using PHP

In this video, you’ll learn how to create an .ics import file using PHP. This allows you to dynamically generate the .ics file based on stored event data. Users can then download the file and import the event information in their calendar of choice… Outlook, Thunderbird(Lightning), Apple Calendar, etc.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
/**
* Get the event ID
*/
$event_id = @$_GET['event_id'];
/**
* If no event ID or event_id is not an integer, do nothing
*/
if ( !$event_id || !is_numeric( $event_id ) ) {
die();
}
/**
* Event information
*/
//$event = get_event($event_id);
$event = array(
'event_name' => 'Test Event',
'event_description' => 'This is a test event. This is the description.',
'event_start' => time(),
'event_end' => time() + 60*60*2,
'event_venue' => array(
'venue_name' => 'Test Venue',
'venue_address' => '123 Test Drive',
'venue_address_two' => 'Suite 555',
'venue_city' => 'Some City',
'venue_state' => 'Iowa',
'venue_postal_code' => '12345'
)
);
$name = $event['event_name'];
$venue = $event['event_venue'];
$location = $venue['venue_name'] . ', ' . $venue['venue_address'] . ', ' . $venue['venue_address_two'] . ', ' . $venue['venue_city'] . ', ' . $venue['venue_state'] . ' ' . $venue['venue_postal_code'];
$start = date('Ymd', $event['event_start']+18000) . 'T' . date('His', $event['event_start']+18000) . 'Z';
$end = date('Ymd', $event['event_end']+18000) . 'T' . date('His', $event['event_end']+18000) . 'Z';
$description = $event['event_description'];
$slug = strtolower(str_replace(array(' ', "'", '.'), array('_', '', ''), $name));
header("Content-Type: text/Calendar; charset=utf-8");
header("Content-Disposition: inline; filename={$slug}.ics");
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:-//LearnPHP.co//NONSGML {$name}//EN\n";
echo "METHOD:REQUEST\n"; // requied by Outlook
echo "BEGIN:VEVENT\n";
echo "UID:".date('Ymd').'T'.date('His')."-".rand()."-learnphp.co\n"; // required by Outlok
echo "DTSTAMP:".date('Ymd').'T'.date('His')."\n"; // required by Outlook
echo "DTSTART:{$start}\n";
echo "DTEND:{$end}\n";
echo "LOCATION:{$location}\n";
echo "SUMMARY:{$name}\n";
echo "DESCRIPTION: {$description}\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";

October 22, 2012

Plugins, Platforms, and My Plan for World Domination

I’ve been thinking a lot lately about WishList Member and its future. Specifically, how the web as a whole is evolving and what role WLM will play in that web. Looking into my magic “crystal ball”, I’m seeing some exciting things ahead if we take the right approach and develop the right mindset for the changes that are occurring with the greater internet.

Of course, none of this makes sense unless you know my Secret Plan for World Domination (add in ominous tone). I don’t mind sharing it, because while it’s simple to understand… it’s pretty damn difficult to implement. So, knowing it is like 1/100 of the battle.

However, I’ve personally never seen this put together in one grand strategy before… and, if you’ve never seen it, it can be quite enlightening (if I do say so myself). So, here it is…

(more…)

August 9, 2012

How to Destroy Your Business Legacy… And Why You Shouldn’t

Ethics in business today suck. Entrepreneurs are too focused on profit as the ultimate goal.

Profit is NOT the ultimate goal.

Business Is a Game

In baseball, a power hitter is often measured by how many homeruns he hits. As fans, we idolize and adore good hitters.

But, hitting homeruns isn’t his ultimate goal.

It’s a means to an end. The end is winning games… and winning enough games to be called a champion.

Often times, his legacy depends on whether or not he won a championship… regardless of how many homeruns he hits.

And, if he cheats to win, his legacy will be forever taintedregardless of how many homeruns he hits.

(more…)

June 28, 2012

7 Reasons Why You Should Start Using the Official Facebook for WordPress Plugin (and 3 Reasons Why You Shouldn’t)

Facebook just launched its official WordPress plugin which allows you to “easily” integrate Facebook features into your WordPress website.

I’ve spent the last day or so playing around with it and here’s my initial thoughts.

Why You Should Install Facebook for WordPress

1. Easy connection to your Facebook account. If you’ve ever setup one of the many Facebook plugins for WordPress, you’ll know that you typically have to create a new application in Facebook, grab the API Key and API Secret from your app, and paste them into your plugin.

This plugin is no different, except you only need to do it once and it gives you access to several of Facebook’s social plugins. Having an integrated Facebook plugin like this keeps you from having to create multiple Facebook applications for a bunch of different plugins or a bunch of PHP coding… which is very handy.

(more…)

June 13, 2012

This Is MY Blog!

If you’ve been following this site for a bit, you’ll know it’s been primarily about coding and contained all my various coding tutorials and such. Well, that’s changed.

I’ve officially moved all my coding tutorials over to http://www.learnphp.co and I’m taking this blog back as my personal blog. If you want the coding tutorials, head over there.

Here, I’ll be posting about pretty much whatever I want. You can follow me here if you’d like to keep in touch with me personally.

(photo by a2gemma)

June 13, 2012

How to Sort Multidimensional Arrays Using PHP

In this video, you’ll learn how to sort multidimensional arrays in PHP. You’ll not only learn basic sorting using array_multisort(), but you’ll also see a more advanced custom function for sorting multidimensional arrays by a specific key.

Multidimensional arrays can be one of the more confusing parts about learning PHP, but this video will help you get your head around it and give you some tools for sorting your multidimensional arrays that are simple and straight-forward.

<?php
$arr = array(
array('name' => 'John', 'age' => 30, 'website' => 'http://learnphp.co'),
array('name' => 'Joe', 'age' => 28, 'website' => 'http://johnmorrisonline.com'),
array('name' => 'Amy', 'age' => 32, 'website' => 'http://amy.com'),
array('name' => 'Alex', 'age' => 22, 'website' => 'http://thealex.com'),
array('name' => 'Pat', 'age' => 40, 'website' => 'http://patsjourney.com'),
);

?>

<pre><?php print_r($arr); ?></pre>

<?php
array_multisort($arr);
?>

<pre><?php print_r($arr); ?></pre>

<?php
function val_sort($array,$key) {

//Loop through and get the values of our specified key
foreach($array as $k=>$v) {
$b[] = strtolower($v[$key]);
}

print_r($b);

asort($b);

echo '<br />';
print_r($b);

foreach($b as $k=>$v) {
$c[] = $array[$k];
}

return $c;
}

$sorted = val_sort($arr, 'website');
?>

<pre><?php print_r($sorted); ?></pre>

December 29, 2011

How to Create an HTML Select Box From a MySQL Results Array Using PHP

This is a question I get quite a bit, so I figured I’d write a post on it:

How do I create an HTML select box with options from data in my database?

Here’s how:

The Query

First, let’s look at our query. Yours might look something like this:

<?php
$sql = "SELECT * FROM queried_table";
$query = mysql_query($sql);
while ( $results[] = mysql_fetch_object ( $query ) );
array_pop ( $results );
?>

That will select everything in our table and loop it into an array of objects containing the results. Now, we can take that array and create a HTML select box with it.

The HTML Select Box

Now, that we have our array of objects from our database we can use it to populate an HTML select box dynamically with PHP. Here’s how that might look:

<select name="the_name">
<?php foreach ( $results as $option ) : ?>
<option value="<?php echo $option->desired_value; ?>"><?php echo $option->desired_label; ?></option>
<?php endforeach; ?>
</select>

That’s it. The code above will loop through the array creating a new option for each element in the array… and populating each option with the desired values from your database. Of course, this is just sample code, you’ll need to work with your database structure and code to make it work for you… but, that should give you the framework for getting this kind of thing done.

Here’s a video walk-through of this, as well:

August 10, 2011