Change default Url of Category-slug
-
Hi
Is it possible to change the default url of a category-{slug} page from https://www.mysite.com/category/{slug} to https://www.mysite.com/category/{slug}/{year} where ‘year’ is the year of latest post in that category .?I have already rewritten rules so that the content is accessible
ie; category/{slug}/{year} gives the posts in that category for the particular year. The problem now is to access the page( list of archives of latest year) i have to add a custom menu link with hardcoded url. Is there any other way to do thiswith regards
-
I’m not sure I understand. it sounds like you answered your own question. You ask if X is possible, then say you have rewritten rules to get X.
So now all you want to do is add a hard coded link to the archive list you already have? Yes? If so, identify the template page being used and add your link, inside a conditional if necessary to prevent display during other archive lists.
If I’ve misunderstood, my apologies. Please try to rephrase your question and I will try to give you a better answer.
Hi bcworkz,
Thanks a lot for your fast reply. Let me try to explain it a bit more , i have a “news” category and added that category to my menu and made a template category-news.php on which i needed to show all “news” category posts of the the latest posts year ie; if we don’t have posts in 2013 it should show posts of 2012 and links to other years ( almost a yearly category archive)
2013 | 2012 | 2010 | Previous
All posts of 2013 in category “News” goes here
I rewrote my rules so that
myste.com/category/news/2013
Gives me the required result (Still the pagination part is missing)
But the menu link gives mysite.com/category/news which doesn’t pass the year and lists all posts regardless of year, i did a nasty hack passing the max year value if the year =0 and got the results somehow .. just wanted to know is there any way i can change the default category menu URL structure mysite.com/category/news to myste.com/category/news/2013
where the last arg is the latest post year using pre_get_posts or something elseThanking you once more for showing interest
with regards
Well, I understand more, but not enough I’m afraid. I’m not sure if you want to auto populate your template menu with years or change the category permalink structure or filter the results to get just the particular year.
Sorry for being a little thick, I recognize that I sometimes have cognition issues. Let’s try this. Using plain English, not code, list the steps you think you would need to code in order to accomplish your desired task. I know you cannot do this accurately or you would not be here, but I think if you describe what you want to do using computer like logic that I will be able to answer your question. Even if you do not know how WP works internally, if you describe things according to how you think it works, I think I will understand.
As you can now imagine, I am not always so fast, the first time was luck. But I am fairly consistent. If you can be patient, I do believe I can help you. Cheers.
Hi bcworz
Thanks a lot for your patience. Sorry for my description going from bad to worse. Let me try again1) I have a “news” category
2) when i click the “news” link in my menu i should get the “news” posts of the latest year (latest year only, if they don’t have current years news then it should show last years posts) listed
3) This template should have a pagination( don’t know whether i can call it a pagination), so that i can go to previous( as well as next) years and get the post listings
2013 | 2012 | 2010 | PreviousWhat i have tried till now
1) made a category-news.php template and got the years using wp_list_archives(type=”yearly”) and confined it to “news” category
The Problem with this
a)wp_list_archives is giving a long list of years …. i don’t know how to confine them to a “Next” “Previous” showing only three at a time.b)the “Landing Page” (mysite.com/category/news/) wont give me the latest year post listings but will only show the year listings from wp_list_archives.
I made a really ugly hack just checking $year =0 and if true made a custom query to display latest year posts.( wp_list_archives will take care of preceding years ). I know it is really weird way of doing things but i was desperately trying
After i found it difficult in populating the wp_list_archives list i went for another Method
2)Got the “Distinct” years from “posts” db and loopped them with custom url “category/news/$year” and rewrote the category template rules so that i am getting my required results through URL category/news/2013.Still the default URL is not giving anything and i have to repeat the ugly hack
Hope my descriptions haven’t done any damage to you ??
with regards
AnoopPS: I am not from an English speaking country .. sorry if my explanations seems a bit auckward
I think I got it now! Thank you for your patience. I had trouble separating what you have, what you want, and what you tried. The problem was not any awkward English, your English is fine.
To accomplish your desire to show either the current year or the year requested, you need to hook into the ‘pre_get_posts’ action to alter the query as needed. You action callback function should incorporate the following logic.
First, check if the query is for a news archive, if not, return without doing anything. Next check if a ‘year’ query var contained in the query object passed is set with
$query->get('year')
. An empty string is returned if it is not set.If it is set, your action callback can return, the query should work properly without intervention, as it sounds like you have rewrite rules working correctly. The only thing you’re looking for is when the year does not exist, as this results a list of all news instead of the current year. To get the query to return the only current year, simply add the current year to the query vars with
$query->set('year', date('Y'))
The one trick here I’m not quite sure about is how to return the previous year if the current year query returns no results. One option is to run a count query with the current query vars and decrement the year value if 0 is returned. Another is to set up your news archive template to
query_posts()
for the prior year whenhave_posts()
at the beginning of the “loop” is false.This should work properly if your category-news.php template is based on a normal archive template that runs the “loop”. The fact you are getting only years listed and no posts leads me to believe you need to change what it is based on, as wp_list_archives() (not a WP function?) is not meeting your needs.
Which leads us to where you now need a way to accomplish the “pagination” to access other years. I think you may need to build your own paging function to list only a few adjacent years, then earlier and later options beyond that. There’s two issues with doing this. One is determining terminal conditions. The current year is easy, the earliest year can be hard coded if this is for one blog, otherwise a count query needs to be run to check for earlier posts. The other issue, which may not even apply, is to check for intermediate years with no posts. Again, count queries can take care of this if need be.
You can look at other pagination functions for ideas on how to build in the required logic, it’s just that you’re using year numbers instead of page numbers and the count queries change accordingly.
I’m not sure how well this matches up with what you already have, but it is a viable approach requiring no queer hacks.
- The topic ‘Change default Url of Category-slug’ is closed to new replies.