Monthly Archives: March 2010

Fixing HTML Validation Errors

Several tips on fixing HTML Validation Errors.

Exclude Categories

Exclude categories from home page and more with Advanced Category Excluder.

Code to drop/create/access mysql table

This code is not meant to be used ‘as is’. It is only an example to show how to create a table and insert rows into it.

<?php function test_db() {
echo '<h2>Testing MySQL Inserts</h2>';
global $wpdb;

echo "<p>Dropping mac_test1</p>";
$sql = "DROP TABLE IF EXISTS mac_test1";
$result = $wpdb->query($sql);

echo "<p>Creating mac_test1</p>";
$sql = "CREATE TABLE mac_test1 (
mac_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
mac_text tinytext NOT NULL,
PRIMARY KEY (mac_ID)
) ENGINE=MyISAM AUTO_INCREMENT=289 DEFAULT CHARSET=utf8 ;
";
$result = $wpdb->get_results($sql);
echo "<p>Result:";var_dump($result);echo '</p>';

echo "<p>Dropping mac_test2</p>";
$sql = "DROP TABLE IF EXISTS mac_test2";
$result = $wpdb->query($sql);

echo "<p>Creating mac_test2</p>";
$sql = "CREATE TABLE mac_test2 (
mac_ID2 bigint(20),
mac_text2 tinytext NOT NULL,
PRIMARY KEY (mac_ID2)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
";
$result = $wpdb->get_results($sql);

echo "<p>Inserting mac_test1</p>";
$sql = "INSERT INTO mac_test1 VALUES (null,'first insert')";
$result = $wpdb->get_results($sql);
echo "<p>Result:";var_dump($result);echo '</p>';
$id = mysql_insert_id();
echo "<p>Inserted id:";var_dump($id);echo '</p>';

$sql = "INSERT INTO mac_test2 VALUES ($id,'123')";
$result = $wpdb->get_results($sql);

echo "<p>Inserting mac_test1</p>";
$sql = "INSERT INTO mac_test1 VALUES (null,'2 insert')";
$result = $wpdb->get_results($sql);
$id = mysql_insert_id();
echo "<p>Inserted id:";var_dump($id);echo '</p>';

$sql = "INSERT INTO mac_test2 VALUES ($id,'456')";
$result = $wpdb->get_results($sql);

echo '<p>Retrieve from mac_test1</p>';
$sql = "SELECT * FROM mac_test1";
$rows1 = $wpdb->get_results($sql);
foreach ($rows1 as $row) {
echo "ID: $row->mac_ID  TEXT: $row->mac_text<br/>";
}
}

test_db();
?>

Posts from last n days:

<?php //based on Austin Matzko's code from wp-hackers email list
     function filter_where($where = '') {
    //posts in the last 7 days       $where .= " AND post_date &gt; '" . date('Y-m-d', strtotime('-7 days')) . "'";
   return $where;
}
add_filter('posts_where', 'filter_where');
$args = array(
   'paged'--&amp;gt; $paged,
   'cat' =&amp;gt; '32,14,-18'
);
query_posts($args);
?>

Posts from prior months:

//based on Austin Matzko’s code from wp-hackers email list
function filter_where($where = ”) {
//posts from prior months
$year = date(Y);
$monthnum = date(m);
$where .= ” AND post_date < '$year-$monthnum-01'"; return $where; } add_filter('posts_where', 'filter_where'); $ignore_stickies = 1; // Stickies will mess up the order $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("caller_get_posts=$ignore_stickies&paged=$paged"); [/php]

Image and File Attachments

WordPress Codex: Using Image and File Attachments

http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/

Get Current Category Info

To get the category information while browsing a category, use this code:

<?php
if (is_category()) {
   $cat = get_query_var('cat');
   $catinfo = get_category($cat);
   echo $catinfo->name;
   echo $catinfo->description;
?>

The Object:

stdClass Object ( [term_id] => 32 [name] => bcategory [slug] => bcategory [term_group] => 0 [term_taxonomy_id] => 32 [taxonomy] => category [description] => The B category for testing category alpha list [parent] => 0 [count] => 6 [cat_ID] => 32 [category_count] => 6 [category_description] => The B category for testing category alpha list [cat_name] => bcategory [category_nicename] => bcategory [category_parent] => 0 )