-
Categories
-
Tags
awards Boulder Code code formatting Colorado CSS Denver design discount web design discount web development featured sites freelance Free Theme Grass Valley great web design articles green business HTML jQuery magento Nevada City News non-profit Northern California Post_ pr Premium Wordpress Theme psd to magento recognition Sierra Mountains small business testimonial theme developer Web Design Web Design Resources web developer web development web devleopment wordpreee theme development wordpress wordpress CMS wordpress plugin development wordpress template development wordpress templates Wordpress Theme wordpress theme customization
Load wordpress posts by category Slug from the URL
2012
Dec 4th
published in
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:
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); }
This function will process the slug we will pass to it from the template file. In your template file:
$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
comments
0