The OneNews Premium Theme already allows you to create multiple pages of feeds. With this hack you can create daily snapshots for each onenews feeds page. In a year you will have 365 pages for each page. Automagically. View live demo at http://ericulous.com/onenews/archives/. The idea is to copy the index.html files generated by WP Supercache and archive them in an “Archives” directory daily.

1. First you need to install and activate the WP Supercache plugin.

2. In themes/onenewspremium/functions.php, add the following just before the last line “?>”

function dailyarchives() {
	global $wpdb;
	$siteurl = trailingslashit(strtolower(str_replace('http://', '', site_url())));
	$filedir = WP_CONTENT_DIR . '/cache/supercache/' . $siteurl;
	
	//remove if don't want to display homepage archives
	//archive homepage
	if (!is_dir(ABSPATH . 'archives/')) {
		mkdir(ABSPATH . 'archives/');
		mkdir(ABSPATH . 'archives/homepage/');
	}

	$oldfile = $filedir . 'index.html';
	if (file_exists($oldfile)) {
		$newfile = ABSPATH . 'archives/homepage/homepage_' . strtolower(date('Y_m_d')) . '.html';
		copy($oldfile, $newfile) or die("Unable to copy $old to $new.");
	}
	//end archive homepage
	
	//archive onenews subpages
	$onenewspages_query = 'SELECT ' . $wpdb->prefix . 'posts.post_name
		FROM ' . $wpdb->prefix . 'posts,' . $wpdb->prefix . 'postmeta
		WHERE ' . $wpdb->prefix . 'posts.post_type = "page"
		AND ' . $wpdb->prefix . 'postmeta.meta_key = "_wp_page_template"
		AND ' . $wpdb->prefix . 'postmeta.meta_value = "page_onenews.php"
		AND ' . $wpdb->prefix . 'postmeta.post_id = ' . $wpdb->prefix . 'posts.ID
		ORDER BY ' . $wpdb->prefix . 'posts.ID ASC
		';

	$onenewspages = $wpdb->get_results($onenewspages_query);

	if ($onenewspages) {
		foreach ($onenewspages as $onenewspage) {
			$oldfile = $filedir . $onenewspage->post_name . '/index.html';
		
			if (file_exists($oldfile)) {
				if (!is_dir(ABSPATH . 'archives/' . $onenewspage->post_name . '/')) {
					mkdir(ABSPATH . 'archives/' . $onenewspage->post_name . '/');
				}
			
				$newfile = ABSPATH . 'archives/' . $onenewspage->post_name . '/' . $onenewspage->post_name . '_' . strtolower(date('Y_m_d')) . '.html';
				copy($oldfile, $newfile) or die("Unable to copy $old to $new.");
			}
		}
	}
	//end archive onenews subpages
}
add_action('dailyarchives_hook', 'dailyarchives');

if (!wp_next_scheduled('dailyarchives_hook')) {
	wp_schedule_event(time(), 'daily', 'dailyarchives_hook');
}

3. In themes/onenewspremium/ direcotry, create a new file named “page_archives.php” and paste the following code (skip this step if you have OneNews v3.2)

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

<?php get_header(); ?>
<div id="postbody">
	<div id="postwrapper">
	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
          	<div class="postcontent">
				<h2 class="posttitle"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( 'Permalink to %s', 'onenews' ), the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h2>
				<hr />
				<?php
				if ($_GET['i']) {
					$dir = ABSPATH . 'archives/' . $_GET['i'];
					
					if (is_dir($dir)) {
						$files = scandir($dir, 1);
						
						if($files) {
							foreach ($files as $file) {
								if ($file != '.' && $file != '..') {
									$linktitle = str_replace('.html', '', $file);
									$linktitle = str_replace('_', ' ', $linktitle);
									$linktitle = str_replace('- ','-',ucwords(str_replace('-','- ',$linktitle)));
									echo '<p>&raquo; <a href="' . network_home_url('/') . 'archives/' . esc_attr($_GET['i']) . '/' . $file . '">' . $linktitle . '</a></p>';
								}
							}
						}
					} else {
						_e('No archives found.', 'onenews');
					}
				} else {
					?>
					<!-- remove if don't want to display homepage archives -->
					<p>&raquo; <a href="<?php echo get_permalink() . '?i=homepage'; ?>">Homepage</a></p>
					<!-- display homepage archives -->

					<?php
					$onenewspages_query = 'SELECT ' . $wpdb->prefix . 'posts.post_title, ' . $wpdb->prefix . 'posts.post_name
						FROM ' . $wpdb->prefix . 'posts,' . $wpdb->prefix . 'postmeta
						WHERE ' . $wpdb->prefix . 'posts.post_type = "page"
						AND ' . $wpdb->prefix . 'postmeta.meta_key = "_wp_page_template"
						AND ' . $wpdb->prefix . 'postmeta.meta_value = "page_onenews.php"
						AND ' . $wpdb->prefix . 'postmeta.post_id = ' . $wpdb->prefix . 'posts.ID
						ORDER BY ' . $wpdb->prefix . 'posts.post_title ASC
						';
				
					$onenewspages = $wpdb->get_results($onenewspages_query);

					if ($onenewspages) {
						foreach ($onenewspages as $onenewspage) {
						?>
						<p>&raquo; <a href="<?php echo get_permalink() . '?i=' . $onenewspage->post_name; ?>"><?php echo esc_attr($onenewspage->post_title); ?></a></p>
						<?php
						}
					}
				}
				?>
			</div>
		</div>
        
        <div class="clearer"></div>
		
		<?php endwhile; endif; ?>
	</div><!-- END POSTWRAPPER -->
    
	<?php get_sidebar(); ?>
	
	<div style="clear:both"></div>
</div> <!-- END POSTBODY -->
<?php get_footer(); ?>

4. Go to WP-Admin > Pages > Add New (Enter “Archives” for Title and select “OneNews Archives” in the Template dropdown menu. If you get a “Forbidden Error” viewing the Archives page, change the slug to “archive” (see pic).

5. Go to WP-Admin > Appearance > Menus (Add the newly created Archives page to your top menu)

Notes: For this hack to work properly, WP Supercache must have the super cached index.html files generated for copying. When your site is new and not many visitors yet, WP Supercache may not have the files ready. Try turning on the Preload Mode to automatically generate the super cached files. May have to wait a day or two for the archives to start showing.


Browse and Share


» Subscribe to Feed
» Leave a Comment

Related Posts:
» Theme Update: OneNews 3.2
» Theme Update: OneNews 2.0
» WP Theme: OneNews – Popurls/Alltop Clone

» Newer Post:
« Previous Post: