WordPress.org

Ready to get started?Download WordPress

Forums

How to pass a year query string to a Page? (14 posts)

  1. roadie
    Member
    Posted 4 years ago #

    I am developing a blog in WordPress on my computer (currently not yet on the Internet - sorry) and I have incorporated the Archive Page structure of theme "Monotone" into another theme ("Neo_Sapien").

    I have done this by creating a Page called "picture-archive" which shows thumbnails of images belonging to posts in the most recent year. The page also has a couple of drop-down menus which allow you to select either thumbnails of images belonging to posts of a different year or thumbnails of images belonging to posts belonging to a particular category.

    In addition to this, my revised theme also has traditional Category and Archive links in the sidebars which link to urls such as http://localhost/wordpress/category/animal/ or http://localhost/wordpress/2010/

    When you are at my picture-archive page and select a category, WordPress redirects to a url such as http://localhost/wordpress/picture-archive/?cat=3 and the picture archive page reappears, this time just showing thumbnails from the chosen category. This works fine.

    Having had a look at the query_posts function, I decided to make the year drop-down menu redirect to urls such as http://localhost/wordpress/picture-archive/?year=2010. This does not work at all. My url displays what looks like the 404 page with the message "Not Found
    Sorry, but you are looking for something that isn't here." (Hard coding a query string such as $query_string='year=2010' in my page's custom template php file works as expected.)

    It now occurs to me there are constraints on what Query Strings can be used with the urls of Pages in WordPress which I do not know about. Can anyone suggest a way of passing a year argument to a Page? or some other work-around for this problem? I am also wondering what will happen when I try and implement the "Earlier Posts/Later Posts" links for selections with more posts than the posts_per_page= option.

    Thanks in advance if you can help.

  2. Haven't tried it, but maybe you get an yearly archive using Template Tags/wp get archives?

  3. roadie
    Member
    Posted 4 years ago #

    songdogtech

    Haven't tried it, but maybe you get an yearly archive using Template Tags/wp get archives?

    Yes, but that would lead to the links pointing to urls like http://localhost/wordpress/2010/ which would not have the thumbnails.

    I could change the category and archive templates to do thumbnails, but then I would not have the normal views of archives.

  4. roadie
    Member
    Posted 4 years ago #

    Perhaps I should change the architeture of these archives - get rid of the page, just use category.php and archive.php and provide a checkbox for the user to choose thumbnail view or normal view - using functions get_option, add_option, update_option?

  5. vtxyzzy
    Member
    Posted 4 years ago #

    Just a thought: if 'year' is a restricted url parameter, try using something like 'myyear'. Check for it in the template and set the query accordingly.

    If this works, I think the previous/next posts link will work also, since I believe that user parameters get passed along in those links.

  6. DonGaspari
    Member
    Posted 3 years ago #

    @vtxyzzy I had the same problem and that fixed it. ?year= is restricted url parameter.

  7. cableb
    Member
    Posted 3 years ago #

    @DonGaspari: Would you mind sharing your solution?

  8. Mike Schinkel
    Member
    Posted 3 years ago #

    @roadie: Would you consider a URL structure like this?

    http://localhost/wordpress/picture-archive/2010/

    Not that it really matters, you can do them either way. But first I need to know, what code were you using to get and use the year parameter? I need that before I can create a complete working example.

    -Mike
    P.S. And if you want to ask the question over at http://wordpress.stackexchange.com it is much easier to provide good working examples as an answer because WA on SE allows a lot more formatting and preview as you type, fwiw.

  9. cableb
    Member
    Posted 3 years ago #

    @MikeSchinkel: I don't want to hijack roadie's thread, but I for one would actually prefer that URL structure... but being the newb I am, I don't even have anything in place to get and use the year parameter.

  10. Mike Schinkel
    Member
    Posted 3 years ago #

    @cableb - But you need to have a concrete use-case otherwise I wouldn't know what to implement. What are you trying to accomplish from a user perspective, not a technical one?

  11. cableb
    Member
    Posted 3 years ago #

    @MikeSchinkel: If this is even possible...

    I have my archive page (http://mydomain/archive-page/) that has years listed on it. Ideally, these would link to http://mydomain/archive-page/2003/, etc., and at those URLs, I'd like to grab the year and use it as a variable (like $yr = the tail end of the URL).

    But those URLs aren't yearly archives, so what exactly is their purpose? One can append any number of random digits to any page and it'll still show up.

    I've tried using simply the_time('Y'); and a URL structure like http://mydomain/2003/category/mycategory/, but that didn't work (it always returned 2010) because apparently with the category parameters in there, it's not considered a yearly archive...

    Thanks for taking the time to at least hear me out!

  12. Mike Schinkel
    Member
    Posted 3 years ago #

    @cableb: Let me see if I understand. Currently you have:

    • http://example.com/archives/ and
    • http://example.com/2010/

    But instead you want:

    • http://example.com/archives/ and
    • http://example.com/archives/2010/

    Is that correct?

  13. Mike Schinkel
    Member
    Posted 3 years ago #

    @cableb - Based on the above (hope it's what you needed) you can put the following in your theme's functions.php file and it will support the URL designs I showed above. The flush_rules() is only needed once and should really just go into a plugin activation hook but that's more work than I want to document tonight (google it instead):

    add_action('init','assign_yearly_archives_url');
    function assign_yearly_archives_url() {
    	global $wp_rewrite;
    	$wp_rewrite->add_permastruct('archives-by-year', 'archives/%year%');
    	$wp_rewrite->flush_rules(false);
    }

    Note this only works because WordPress already has an internal %year% variable. If you were doing something with a variable that WordPress doesn't already assign you'd have a lot more work to do.

    This will load the archives.php template in your theme. If for example you wanted a specific theme file for your yearly archives you could use this following code and then create a archives-year.php file based on archives.php but modified to be specific to year:

    add_action('archive_template','specify_yearly_archive_template');
    function specify_yearly_archive_template($template) {
    	global $wp_query;
    	if (!empty($wp_query->query_vars['year'])) {
    		$ssd = get_stylesheet_directory();
    		$template = "{$ssd}/archive-year.php";
    	}
    	return $template;
    }

    Hope this helps.

    -Mike

  14. cableb
    Member
    Posted 3 years ago #

    It does indeed help! Thanks a bunch, Mike.

Topic Closed

This topic has been closed to new replies.

About this Topic