Posts Tagged ‘exec-php’

Creat an “Archives” Page in Your WordPress Blog

July 18th, 2009 No comments

[ Note: You need to have “Exec-PHP” plugin activated in order for the code in this post to work. ]

I planned to create an “Archives” page in my blog. On this “Archives” page, I wanted to list all my posts and organize them in categories. In each category, posts would be sorted by date.

Obviously I needed to write some PHP code and use a few WordPress functions to achieve this goal. The first step was to retrieve all the categories from my blog. This was done using get_categories() function:

$categories = get_categories('orderby=name');
foreach ($categories as $cat)
    echo $cat->cat_name;
    echo ' (';
    echo $cat->category_count;
    echo ')';
    echo '<br />';

For every category retrieved from my blog, I needed to get all the posts in that category and list them in a table. To do this, I needed to create my own “Loop” in my code. There is a post on talking about creating customized “Loop” in three ways. The first two methods, which involve using query_posts() and get_posts(), did not work for me. When I tried them, for some reason, the_permalink() and the_title() functions did not return the permalinks and the titles of the posts. Instead, they only returned the permalink and the title of the page -:( The third method involves using WP_Query class, which worked nicely for me. There is another good article on WP_Query.

To create a customized “Loop”, I made an instance of the WP_Query class and did it from scratch:

$my_query = new WP_Query($query_string);
if ($my_query->have_posts())
    while ($my_query->have_posts())
        echo the_permalink();
        echo the_title();

After adding some HTML formatting tags and putting all together, I got the complete source code for my “Archives” page:


Exec-PHP Plugin for WordPress

July 8th, 2009 No comments

In order to execute PHP code in your wordpress post, you will need a plugin that does that for you. I am using Sören Weber’s Exec-PHP plugin.


To install the Exec-PHP plugin:

  1. Download the Exec-PHP plugin archive and extract the files.
  2. Copy the resulting exec-php directory into /wp-content/plugins/ directory.
  3. Activate the plugin through the “Plugins” menu of WordPress (after a fresh installation only the Administrator is allowed to execute PHP code).
  4. Configure blog and user settings if needed.


With Exec-PHP you can execute PHP code in the excerpt and the content portion of your posts and pages (in the following called articles) as also as in text widgets. To execute code, just type in the PHP code as you usually would, encapsulated in <?php ?> tags.

What if I just want to print out PHP code and don’t execute it?

If you just want to print out code and don’t want to execute it, you have to make sure to convert your code to the correct XHTML representation. To do so you have to escape the following characters: < to &lt;, > to &gt;, and & to &amp;.

Categories: Wordpress Tags: , ,