
cc licensed flickr photo shared by M.H.ick9s
Arrgh, just spent about an hour and half chasing myself in silly code circles. Not that it matters, but just to document my sanity/lack thereof…
I have a few WordPress pages hanging off the top banner, and I’d previously had them use PHP code in the pages, using one of the plugins that allow you to put executable PHP in your stuff (yes all warnings apply, you need to know what you are doing, security panic yadda yadda). I think I was using PHP Execution which, like Exec PHP just lets you put the code in the page including all the <?php … ?gt; stuff and let it go.
However, I often write posts that have PHp code in them, and I really like using the way the Syntax Highlghiter Plus plugin displays code, e.g.
<?php
// array of all years data to display, with most recent first
$all_years = array('2009', '2008','2007', '2006','2005','2004','2003');
// check that we got a year as a parameter
$year = $all_years[0];
?>
The problem is, with any of those PHP plugins, WordPress tries to run my code.
When all I want to do is display it. And if I convert all the angle brackets to things like < it is first a pain, and second, the Syntax Highlghiter Plus plugin does not render as the element they are.
I then thought to try one of the PHP plugins that require a special tag or shortcode wrapper, like Inline PHP but that was a nightmare for one of my pages that had about 14 different PHP chunks, and it does not behave like executed PHP (variable values are not kept alive).
So I had to brute force it- ditched all the PHP plugins. I narrowed it down to to key WP pages that needed to do some PHP action. I ended up creating special template files for these pages, and moving all the code, essentially, everything that was inn the content of my pages — into the template.
Huh?
Well, I started with the basic page template (page.php)
<?php get_header(); ?>
<div id="content" class="page">
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="entry" id="post-<?php the_ID(); ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-byline">
<?php edit_post_link('Edit', '[', ']'); ?>
</div>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages('before=<p><strong>' . __('Pages:') . '</strong>&after=</p>'); ?>
<div class="clear"></div>
</div>
</div><!--.entry-->
<?php endwhile; ?>
<?php endif; ?>
</div><!--#content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
And I remove everything between <div class=”entry-content”> and its closing </div > — essentially lobotomizing the page – whatever I wrote in the wordpress editor, was not used. I saved this template for the Last 100 Barks Page as anew file, page-bark.php in my template directory.
The first thing to do is add a little line at the top that gives it a readable name in the WP editor:
<?php /* Template Name: Last Barks */ ?>
And I cut all the code/content from the page in WordPress, and insert it into the template:
<?php /*
Template Name: Last Barks
*/ ?>
<?php get_header(); ?>
<div id="content" class="page">
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="entry" id="post-<?php the_ID(); ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<ol>
<?php $my_query = new WP_Query('showposts=100'); ?>
<?php while ($my_query->have_posts()) :
$my_query->the_post();?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>""><?php the_title(); ?></a> <small><?php the_time('M j, Y') ?> <?php the_time('h:ia') ?> <?php edit_post_link(' Edit', '<img src="' . get_bloginfo('template_directory') . '/images/edit.gif" alt="" align="absmiddle" /> ', ''); ?> shorter: <a href="<?php echo twitter_link()?>"><?php echo twitter_link()?></a></small> </li>
<?php endwhile; ?>
</ol>
<div class="clear"></div>
</div>
</div><!--.entry-->
<?php endwhile; ?>
<?php endif; ?>
</div><!--#content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is the code I use to generate a page with my last 100 blog posts (its really to help my find my own stuff), but I’ve hard coded all of the content and code logic into a template.
When I edit the page in WordPress, it can have whatever I want in the text content area, cause that is never used in the template, but the key is selecting te right template to use:
I’m sure some more clever person will point a much simpler solution…. but that person was not around tonight! So it has hack hack hack til something worked.

The WordPress Code Circles by CogDogBlog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 3.0 United States License.





