Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Article list from same category / section as current article

From a post on the Textpattern support forums.

<ul><txp:asy_wondertag><txp:article_custom limit="9999" form="listarticles" section="<txp:section />" category="<txp:category1 />" /></txp:asy_wondertag></ul>

Return a message on blank search form submit

From a post on the Textpattern support forums.

<txp:php> if(gps('q') == '') echo 'Hey, remember to search something!'; </txp:php>

Post link to POOKMARK with Textpattern

Simple link to post each individual article to Japanese social bookmark "POOKMARK".

POOKMARK Airlines
http://pookmark.jp/

<a href="http://pookmark.jp/post?url=<txp:permlink />&title=<txp:title />" title="">POOKMARK THIS</a>

Technorati Link Count Widget for Textpattern

Technorati tracks when other bloggers link to your blog and this widget makes it possible for you to display the number of links on every blog post. We call them "reactions" to encourage readers to follow the conversation. To add the widget, copy and paste the code below into your blog's template.

<script src="http://embed.technorati.com/linkcount" type="text/javascript"></script>
<a href="http://technorati.com/search/<txp:permlink />" rel="linkcount">View blog reactions</a>

Adding "post to" links for del.icio.us, digg, and ma.gnolia in Textpattern

Here's how to add "post to" links for del.icio.us, digg, and ma.gnolia to individual articles in Textpattern

del.icio.us

post to <a href="http://del.icio.us/post?url=<txp:permlink />&amp;title=<txp:title />">del.icio.us</a> 


digg

post to <a href="http://www.digg.com/submit?phase=2&amp;url=<txp:permlink />">digg</a>


ma.gnolia

post to <a href="http://ma.gnolia.com/bookmarklet/add?url=<txp:permlink />&amp;title=<txp:title />">ma.gnolia</a>


Clicking one of the those links will automatically post a permanent link for an article and, in the case of del.icio.us and ma.gnolia, the title to one of the above services.

Photoblogging with Textpattern: atom feeds

To textpattern/publish/atom.php somewhere around line 131 (in 4.0.2), or after
else {
$Body = '';
// If there's no excerpt, use body as content instead of body as summary
if (!trim($Excerpt))
$Body = fixup_for_feed($thisarticle['body'], permlinkurl($a));
}

add this:
// hack to get pictures to syndicate
if ($a['Section'] == "photo")
{
$img_url = hu."images/".$a['Image'].'.jpg';
$Body = doSpecial('<a href="'.permlinkurl($a).'"><img src="'.$img_url.'" alt="the latest and greatest" /></a>');
}
// /hack

Photoblogging with Textpattern: rss feeds

Add this to textpattern/publish/rss.php somewhere around line 62 (in 4.0.2), or after "$Body = (!trim($Body)) ? $thisarticle['body'] : $Body;":
// hack to get pictures to syndicate
if ($a['Section'] == "photo")
{
$img_url = hu."images/".$a['Image'].'.jpg';
$Body = doSpecial('<a href="'.permlinkurl($a).'"><img src="'.$img_url.'" alt="the latest and greatest" /></a>');
}
// /hack

Testing if this is NOT a category listing

The new releases of textpattern tend to use the new "yoursite.com/category/ZZZ" url format for category listing links. Which means that those category-filtered archives use the "default" template for formatting & so forth.

To handle this on the front page, you need to be able to test if the current view IS a category search, and you also need to test if this ISN't a category search (to display normal front-page content). To do this I had to modify the glx_if plugin - very easy really.

In Textpattern, go to admin > plugins, and click the "edit" link for glx_if. Add the following function anywhere it would seem to fit:

function glx_if_not_category_list($atts, $thing)
{
	global $pretext, $is_article_list;
	return (empty($pretext["c"]) && $is_article_list == true) ? parse($thing) : "";
}


A use for this is shown in this snippet.

Low-Overhead Archives By Month

You used to have to use rss_suparchive to get an archive of articles broken out by month, but this plugin produces an ungodly number of db queries when dealing with large archives (e.g., hundreds or thousands of articles).

Textpattern 4.0.2 allows you to get the same effect using the good ol' <txp:article> tag, which uses a LOT FEWER database queries.

You can see this in action at Not Far North.

First, you need the glx_if plugin installed.

put this in your "article" page template:

<txp:glx_if_section_frontpage>
   <h2>Archives</h2>
   <dl class="archivelist">
     <txp:article section="article" form="article_archive" />
   </dl>
</txp:glx_if_section_frontpage>

<txp:if_individual_article>
   <txp:article />
</txp:if_individual_article>


If you were attentive, you see that we also need an article_archive form. Create that form and put this in it:

<txp:if_different>
  <dt><txp:posted format="%B %Y" /></dt>
</txp:if_different>

<dd><txp:permlink><txp:title /></txp:permlink>
<br /><txp:excerpt /></dd>


Now, "yoursite.com/article" will display a list of articles broken out by month, and "yoursite.com/article/2/helloworld" will display only that article using the "default" form.

To handle category listings, install the upm_category_title plugin and add this to your front page ("default") template:

<txp:glx_if_category_list>
   <h2>Articles in <txp:upm_category_title /></h2>
   <p><a href="/article/">View all articles</a></p>

   <dl class="archivelist">
     <txp:article section="article" form="article_archive" />
   </dl>
</txp:glx_if_category_list>

<txp:glx_if_not_category_list>
   <txp:article limit="2" form="article_newest" />
</txp:glx_if_not_category_list>


Note that using the above bit of code requires you to modify the glx_if plugin a bit - more about that in another snippet (it's really easy).

Plugin development workflow improvements

By Sencer on the TXP forums
-> http://forum.textpattern.com/viewtopic.php?id=1043&p=2

Make your life developing plugins significantly easier:

1. Use zem’s tamplate.
2. insert row into txp_prefs table with

name = ‘plugin_cache_dir’ and val = ’/full/path/www/textpattern/tmp’
(you can let the other ones default to their respective default value)
3. create a directory /full/path/www/textpattern/tmp
4. drop your plugins (uncompiled) in that directory (make sure plugin name and plugin-filename correspond, e.g. zem_test should be in zem_test.php)
(Note: do not put zem_tpl.php in the same directory)