Solved

Wordpress permalinks with custom pages and category titles

Posted on 2009-05-20
2
429 Views
Last Modified: 2012-05-07
Hi everyone,

On my wordpress site (I am using this as a CMS) i have the following structure for a post:
http://www.domain.com/news/categorytitle/54/title-of-post/. I can get to the post without problems and I can get to the http://www.domain.com/news/ without problems.

How dit I do this:

I have created a few pages with custom query's in them to display all the posts that have a corresponding custom field. Example:

- news.php pagetemplate with in it a custom query that only displays posts where the custom field "news" is checked.
- Page named "news" which uses the news.php as a page template. This page has a permalink: http://www.domain.com/news/ which works great.

The problem is that I cannot get the url http://www.domain.com/news/categorytitle/ to work properly so I can list all news items in that specific category.
The url works but the page is displaying ALL the posts in the specific category and not just the posts within that category that also have the "news" field checked.

I have tried to use the code below in my functions.php file but it does not seem to work.

I hope someone can point me in the right direction?


function flush_rewrite_rules() 
{
   global $wp_rewrite;
   $wp_rewrite->flush_rules();
}
 
function add_rewrite_rules($wp_rewrite) 
{
  	$new_rules = array( 
    	'news/(.+?)/' => 'index.php?pagename=news&category_name='.$wp_rewrite->preg_index(1)
	);
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
 
/******
 Adding rewrite rules
******/
add_action('init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');

Open in new window

0
Comment
Question by:WouterN
2 Comments
 
LVL 1

Accepted Solution

by:
link2caro earned 500 total points
ID: 24506977
Hint:

You can create a page template for category-listing.php (this will be a subpage for news) with this method to define which category to list and just put it in your custom query for this category-listing.php:

$___category = split('%2F',$query_string);
echo $___category[count($___category)-1];

Use this to put in your custom query if you need to manage pagination :)
preg_match("@paged=+(.*?)&@", $query_string, $___page);
echo $___page[1];
0
 

Author Closing Comment

by:WouterN
ID: 31583565
Thank you very much. This solved my problem!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Like there are many different versions of Wordpress, there are also a handful of related side projects WordPress users should be familiar with.   All of these projects are open source and GPL licensed, which means there are few restrictions to us…
WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question