Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im trying to create a custom image galleri that retrieves images from posts as can be seen here

http://kurdaktuellt.se/galleri/

problem in hand is that I'm trying to make the url somewhat user friendly and I've searched the internet for an answer and the closest thing I've come up with is

add_action( 'init', 'wpse12065_init' );
function wpse12065_init()
{
    global $wp;
    $wp->add_query_var('images');

    add_rewrite_rule(
        '^galleri/images/([^/]*)/?',
        'index.php?pagename=galleri&images=$matches[1]',
        'top'
    );

}

This url works just fine

http://kurdaktuellt.se/?pagename=galleri&images=1452

but this one

http://kurdaktuellt.se/galleri/images/1598/

shows nothing

Any help is very much appreciated Regards

share|improve this question
    
@toscho for starters I got some code that's working and beeinb shown and validated by monkeyman-rewrite-analyzer, and before when i typed http://kurdaktuellt.se/galleri/images/1598/ I got a page not found, now i just get to the http://kurdaktuellt.se/galleri/ –  Breezer Oct 3 '12 at 5:04
    
your code works fine in default twentyeleven theme. var_dump the $wp_query global and verify that it exists. have you flushed rules since adding the query var? you'd get a 404 if the rewrite hasn't been picked up, but the fact that the rewrite doesn't 404 suggests the issue is elsewhere in your theme. –  Milo Oct 3 '12 at 15:06
    
@Milo I'm suspecting the rewrite is working as mentioned in the Q:n but what my issue is to be more precise to get get_query_var('images') working which is not... –  Breezer Oct 3 '12 at 16:00
1  
again, it works fine in twentyeleven, echo get_query_var('images') outputs the correct value, your issue is not with the above bit of code you've posted. –  Milo Oct 3 '12 at 16:07
    
@Milo hmmm have to take alook at it ... thanks for the help –  Breezer Oct 3 '12 at 16:11
add comment

1 Answer

My initial thought without being able to do further testing is that your custom galleri page is looking for the URL parameters in order to determine which post to pull images from. However, since you're rewriting the URL, those URL parameters don't technically exist (I could be wrong about this - I'm not at a spot where I can do testing and I don't know your exact code).

If your goal is to have user friendly URLs, you could go with a different approach than URL parameters. You could get the value of the $_SERVER["REQUEST_URI"] variable, split it on the "/" and determine the post from there.

[EDIT]

<?php
$currentUrl = $_SERVER["REQUEST_URI"];
$arrUrlParts = explode( "/" , $currentUrl );

#Assuming that your URL is "http://kurdaktuellt.se/galleri/images/1598/", the variables would end up looking like this:

# $currentUrl contains "/galleri/images/1598/"
# $arrUrlParts[0] contains ""
# $arrUrlParts[1] contains "galleri"
# $arrUrlParts[2] contains "images"
# $arrUrlParts[3] contains "1598"
?>

In the case above, $arrUrlParts[3] would contain the number that you're looking for.

share|improve this answer
    
I'm using this solution atm but it's not what I'm after, I mean why doesnt get_query_var('images') work? –  Breezer Oct 3 '12 at 6:35
    
When you use the URL that has the URL parameters, it does work from what you're saying. However, when you use the code that you listed above to rewrite the URL, the URL parameters don't technically exist for get_query_var to pull. Hence, you'd need to change the way you're trying to get the number, per my answer above. –  Phil Erb Oct 3 '12 at 13:56
    
It's definitely an interesting issue and not one that I would have foreseen when planning out to do what you're doing. –  Phil Erb Oct 3 '12 at 13:56
    
I added some PHP code to my answer that should help you in reading the "friendly URL" to get the information you need. –  Phil Erb Oct 3 '12 at 14:56
    
I appreciate you helping but as i was saying i already have that solution and to add a bit to it if someone ever cares to use it, you should count the array cause its not definitive that someone will use a trailing slash at the end I'll give you a point for the try but its not the answer im looking for im sorry –  Breezer Oct 3 '12 at 15:58
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.