The Making of Version 3: Homepage

09 February 2007

My favorite ExpressionEngine customization on Geek & Mild is found on the homepage; the displaying of article and link entries. They’re both listed chronologically, as you can see, but are displayed uniquely. Out of the box the {exp:weblog:entries} tag doesn’t have a category_id variable, so I have to query the database to get one. Thankfully EE does have a query tag so no PHP code is needed. Here’s what I do:

  1. {exp:weblog:entries weblog="myblog" limit="20" paginate="bottom"}
  2. {exp:query sql="SELECT cat_id AS mycategory FROM exp_category_posts WHERE entry_id = '{entry_id}'"}
  3. {if mycategory == '#'}
  4. <div class="link">
  5. {if summary}{summary}{if:else}{body}{/if}
  6. {if:else}<div class="post">
  7. <h2><em>{entry_date format="%M %j%S"} </em><a href="/entry/{url_title}">{title}</a></h2>
  8. {summary}
  9. {if body}{body}{/if}
  10. <p class="post-links"><a href="/entry/{url_title}#comments">{comment_total} comment{if comment_total != '1'}s{/if}</a><span> | </span><a href="/entry/{url_title}">permalink</a></p>
  11. {/if}
  12. </div>
  13. <hr />
  14. {paginate}<p id="pagination">{if next_page}<span><a href="{auto_path}">&#171; Previous entries</a></span> {/if}{if previous_page}<a href="{auto_path}">Newer entries &#187;</a>{/if}</p>{/paginate}
  15. {/exp:query}
  16. {/exp:weblog:entries}

Firstly, everything’s wrapped in the {exp:weblog:entries} tag, allowing 20 entries (regardless of entry type, or category), and pagination is set at the bottom. Pretty simple and standard. If you plan on implementing or testing this code block out for your own, just replace the “myblog” value with your own blog short-name.

Line 2 is where the query magic happens:

  1. {exp:query sql=”SELECT cat_id AS mycategory FROM exp_category_posts WHERE entry_id = ‘{entry_id}’”}

Here I’m grabbing the category ID (cat_id) from the table exp_category_posts for each entry and naming it mycategory. After that I have that precious variable {mycategory} available to me.

Note: I think if multiple categories have been assigned to an entry, every ID will be listed when the variable is parsed. So, for instance, if categories 5, 11, and 14 have been selected, the {mycategory} variable will render 51114. I’m not sure about that since I’m careful to only assign one category for each of my entries, but you should know if you plan on trying this out yourself.

Now that I have the category ID available as a variable, I use a conditional statement to display entries based on their categories. That’s where the first if comes into play. My “links” category ID happens to be 21, so my conditional looks like:

  1. {if mycategory == ‘21’}Content for links{if:else}Content for other types of entries{/if}

Simple, eh? Once the initial query is done, the rest … scrolls like butter, as Steve would say.

One other note about my homepage code: You’ll notice I don’t use {url_title_path} for any of my entry links. I opted against that based on the simplicity of my website and the fact that linking to a specific anchor (e.g. #comments) doesn’t work that well when using the EE path variables. I’ll gladly change my ways if someone can explain to me why that is or how to avoid it.

UPDATE: It’s like pulling teeth to get EE to not parse code in this entry! Until I can get it worked out, I’ve added “VARIABLE” to the category ID tag.

UPDATE 2: I’ve changed the variable name altogether.

UPDATE 3: Sorry for the outage all day! That’s what I get for messing around with templates during lunch break. Showing EE code blocks when using the Markdown plugin is proving more trouble than it’s worth, but I’ve been getting help from the pMachine Ninjas. Until a proper solution is found, the code blocks above will be backslash-escaped and have some parsed variables (on the homepage). Best not to copy/paste; just ask me if you want the original source.

UPDATE 4: Finally got it working! I was provided a new version of the Markdown plugin (much thanks to Paul Burdick) that encodes EE tags and doesn’t allow them to be parsed when inside an entry. Certain tags on my homepage still get parsed somehow (notice the {url_title} and {entry_date} tags) because I’m using query. I’m not sure if there’s a way around that, so I am considering other solutions for displaying linked entries versus other types.