Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You can use the Rewrite API to define new perma-structures. Be careful how you set this up, the parser needs some way to know that the first path element is a category and not something like a post slug. One way is to force all permalinks to have a category parameter as the first element. Another is to precede any category with an identifying term, like example.com/cat/politics/page-name/.

    Without doing anything differently, by default WP will ignore most preceding terms if a page slug is provided since the page slug is all that is needed to locate a post. There cannot be duplicate slugs, so a category specification is superfluous. The exception is if WP recognizes the preceding term as something, such as a year. The term is then made part of the query, if there is a mismatch (specifying 2013 for a post made in 2014, for example) then no post will be returned.

    Thread Starter clementt

    (@clementt)

    Thank you for your reply bcworkz. I understand what you mean and that is what I want. Nevermind if each page or post has an url like: http://www.example.com/cat-name/page-or-post-name. That would actually be awesome.

    I will have a look through the Rewrite API. Although, I am quite new in WordPress so this sounds very unclear to me. What will I have to do? Redefine functions with filters? Configure the permalink format in the admin settings? Change my htaccess file?

    Thank you.
    Regards,

    Thread Starter clementt

    (@clementt)

    I managed to get the URL format working thanks to the function add_rewrite_rule such as:

    function add_page_url()
    {
        add_rewrite_rule(
                '^([^/]*)/([^/]*)/?',
                'index.php?category=$matches[1]&pagename=$matches[2]',
                'top'
        );
    }

    Although, I now don’t know how to define the pages’ permalinks so they follow this new URL format.

    Moderator bcworkz

    (@bcworkz)

    I’m glad you figured that out! I’m not too sure I would have explained it adequately, I struggle with it myself. It’s still not entirely clear to me either 🙁

    Altering permalinks is more straight forward, as long as you grasp hooking filters. What follows assumes the permalinks are coming from template tags like the_permalink(), get_the_permalink(), or any of it’s relatives that eventually call get_permalink(). (Most themes do this) That last function has three different filters which can be useful to alter permalinks, I’m not sure which would be the best.

    To see how filter callback return is used in forming a permalink, it’s best to just review the source code. Hook into the one that would work best for you.

    Thread Starter clementt

    (@clementt)

    I am not sure I understand..
    Everything works fine with the blog posts since I was able to configure the permalinks such as /%category%/%postname%. Although, I am not sure about to overload the get_permalink function so I can have the sane structure as for the posts. Plus, I also have to take account of the language thing, which means adding the fr for french, at the beginning of the url if needed.

    I don’t understand what you mean with the three different filters available for get_permalink(). Do I have to overload this function by creating a new one and apply the filters to get_permalink()?

    Thank you.

    Thread Starter clementt

    (@clementt)

    There we go!

    I created a filter that I hooked on the page_link function. The good thing is, this function is no only called when fetching the pages’ URLs but also when creating a new page. Therefore, when the page is added to the database, the modified permalink is too (something that I didn’t expect at all).

    Anyway, there is probably still a few things to fix in this code. I will make some tests now.

    function add_page_url()
    {
        add_rewrite_rule(
                '^([^/]*)/([^/]*)/page/([^/]*)?',
                'index.php?category=$matches[1]&pagename=$matches[2]&paged=$matches[3]',
                'top'
        );
        add_rewrite_rule(
                '^([^/]*)/([^/]*)/?',
                'index.php?category=$matches[1]&pagename=$matches[2]',
                'top'
        );
    }
    add_action('init', 'add_page_url');
    
    function add_cat_page_link($link, $id) {
        // get page
        $page = get_pages(array('include'=>$id));
        // get category slug
        $category = get_the_category($page[0]->ID);
        if (!empty($category)) {
            $slug = $category[0]->slug;
        } else {
            $slug = 'uncategorized';
        }
        // ret new URL
        return site_url().'/'.$slug.'/'.$page[0]->post_name;
    }
    add_filter('page_link', 'add_cat_page_link', 0, 2);
    Moderator bcworkz

    (@bcworkz)

    It seems that if I let you be for a while you manage to figure things out on your own! 🙂 Let us know how your testing goes and if there are any remaining issues you are unable to resolve.

    Don’t worry, I will not let you be to figure things out on your own on purpose. If I have anything useful say, I will do so as soon as I see your post no matter how recent it is.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Categories for Pages and permalink’ is closed to new replies.