Articles

Writings from the summit

Category Archives: Uncategorized

Load wordpress posts by category Slug from the URL

By | Uncategorized | No Comments

Here is a quick wordpress development tip to help you load posts into the wordpress loop based off the category slug in the URL. This assumes you are using the postname permalink structure for your url, e.g. mysite.com/category/testcategry.

In your functions file add this function:
[php] function getLastPathSegment($url) {
$path = parse_url($url, PHP_URL_PATH);
$pathTrimmed = trim($path, ‘/’);
$pathTokens = explode(‘/’, $pathTrimmed);
if (substr($path, -1) !== ‘/’) {
array_pop($pathTokens);
}
return end($pathTokens);
}
[/php]

This function will process the slug we will pass to it from the template file. In your template file:

[php] $slug = getLastPathSegment($_SERVER[‘REQUEST_URI’]); // Get the last slug item
$category = get_category_by_slug($slug); // Load the category
query_posts(‘showposts=-1&post_status=publish&cat=’.$category —>cat_ID); // Load the posts

// WordPress Loop as normal
[/php]

PHP Includes for Non-Developers

By | Uncategorized | 6 Comments

I usually will not write tutorials as there are many great ones available on any given topic; but this one I have not often seen, and would have loved to have known about it along time ago.

Any HTML/CSS web designer who doesn’t know PHP can certainly make good use of this PHP include statement. PHP includes are very useful for making global changes on smaller HTML/CSS websites, just as CSS is a tool to easily make global changes to styles and layouts.

code

Read More