php

WordPress List Category Posts without Breaking Sidebar

Wednesday, July 20th, 2011

query_post codex
As I was creating a page template for wordpress, I wanted to list posts from a specific category on a page when I ran into a interesting problem, the query_posts function I was using was causing breaks in the execution of conditional tags like is_page(’25′) in the sidebar template.  I looked around on the web for a while before finally finding the solution that worked for me. So I am rewriting the solution here for reference and so anyone else may find it easier.

Here is how you list posts from a particular category in a wordpress page template, right before the “while (have_posts()) : the_post(); ” call add:

<?php query_posts('category_name=name&showposts=100'); ?>
<?php /*  change name to the name of the category posts you want to display   */  ?>

The reason the wordpress function query_posts() breaks the sidebar template is because its not a simple database query collecting posts from a specific category, but its resetting the query object as a whole to reflect a normal category query.

So to fix the sidebar template you have to go with creating a new query object through WP_Query() in your page template. So here is the before and after of the loop in a page template.

Page Template Loop BEFORE:

<?php query_posts('category_name=name&showposts=100'); ?>
<?php while (have_posts()) : the_post(); ?>
       <!-- do stuff ... -->
<?php endwhile; ?>

Page Template Loop AFTER:

<?php $cat_posts = new WP_Query('category_name=name'); ?>
<?php if($cat_posts->have_posts()) : ?>
         <?php while($cat_posts->have_posts()) : $cat_posts->the_post(); ?>
                 <!-- do stuff ... -->
         <?php endwhile; ?>
<?php endif; ?>

Hope that helps someone, cya.

New Envato Marketplace API

Wednesday, April 22nd, 2009

The Envato Marketplace, which includes sites like FlashDen and ThemeForest, has just come out with their own API. This API is an  interface which will allow developers to build applications and software that can easily utilize Envato user and item data. This allows me to get information in either XML 0r JSON.  So like my integration with WordPress and Flash, I could make a flash application that displays my sellable items from Envato Marketplace onto my Blog.  Its pretty cool.  Here is an example:

With this link,  it shows the xml created from the API of the newest files I have uploaded to GraphicRiver.net up to 10 (I only have one up so its only showing that one).

Here is the Envato Marketplace API Documentation.

Here is an API example. After clicking the link, look toward the bottom of the side navigation, I have some of my sellable items from the marketplace listed using the API.

Here is the code I inserted into the sidebar.php file in my wordpress files. I added it right after the last ‘<?php endif; ?>’. Don’t forget for it to work you have to download the Envato Recent Files Plugin, upload that to your plugins folder, and activate it.

<h2>Latest Sellable Items</h2>
	<li><?php echo envato_recent_items('your_marketplace_username', 'envato_sitename', 1, true, $ref='your_marketplace_username'); ?></li>
	<li><?php echo envato_recent_items('scottrockers', 'graphicriver', 1, true, $ref='scottrockers'); ?></li>

Cya

Flash and PHP Newsletter Sign Up Form

Friday, February 6th, 2009

newslettersignup

So I had a little extra time this weekend to work on some flash stuff so I createD a newsletter sign up form.  Which brings me to this point,  one of the things that attracts me to Flash so much is that I can easily customize the look and feel of anything I work on really easily. You have to love the Flash IDE, thats all I am saying.  Some of the features that I programmed into this sign up form that I would like to point out are:

  1. Use of the “Enter” key with the keylistener function to make it more user friendly
  2. Tab indexing functionality also to make it more user friendly
  3. Error detection on the input field for those users who are retarded (basically it won’t take any input that is not a valid email address)

I am all about user friendliness, and  I will get more into that at some point.  There is a really simple PHP script that goes along with this, and for right now it just sends the email address the user inputs to a certain email address (right now it just sends it to my email). At some point, I would like for the input to be put directly into a database but thats for later.

Cya