$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - URL rewrites af|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - URL rewrites af

matteradmin10PV0评论

I'm trying to make some pretty URLs that show content from a category page. An example would be this: (not a real WP page) would show content from /?type=white-paper. /resources is an archive page of a custom post type, and the archive page template pulls the query string and displays posts of the respective taxonomy. Any tips on how to approach this?

I'm trying to make some pretty URLs that show content from a category page. An example would be this: https://example/resources/white-paper (not a real WP page) would show content from https://example/resources/?type=white-paper. /resources is an archive page of a custom post type, and the archive page template pulls the query string and displays posts of the respective taxonomy. Any tips on how to approach this?

Share Improve this question asked Jan 15, 2019 at 13:18 JeffreyPiaJeffreyPia 1011 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

Use add_rewrite_rule().

function wpse325663_rewrite_resource_type() {
    add_rewrite_rule('^resources\/(.+)/?', 'resources/?type=$matches[1]', 'top');
}
add_action('init', 'wpse325663_rewrite_resource_type');

An important note from the codex:

Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

As long as you're only dealing with one CPT, when you register your post type, make sure to set has_archive to true and rewrite to array('slug' => 'resources'). That way you are programmatically creating an archive.

(If you did not set up has_archive when you first registered the CPT, you may need to unregister_post_type() which won't delete your posts but will clear it out, and then re-register it with the new settings. Finally, visit the permalinks settings page to flush permalinks.)

Then, in a child theme or custom theme, create a file called archive-resources.php. This is where you can intercept the query string and respond appropriately. Here's the basic structure:

<?php
// if the query string is present
if($_GET['type']) {
    get_header();
    // run your tax query here
    $args = array(
        'post_type' => 'post',
        // tax query is an array of arrays
        'tax_query' => array(
            array(
                'taxonomy' => 'type',
                // you can pull various ways; this uses the slug i.e. 'white-paper'
                'field' => 'slug',
                'terms' => $_GET['type']
            )
        )
    );
    $posts = new WP_Query($args);
    if($posts->have_posts()) {
        while($posts->have_posts()): $posts->the_post();
        // your loop here
        endwhile;
    }
    get_footer();
} else {
    // what to do if no query string is present
    // example: you could redirect elsewhere, or show a default taxonomy query
}

The downside is your archive will run a default query, so you're basically throwing the results of that query out the window and then running your own. You could just run a regular Loop in the else condition and make use of it since it will run anyway.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far