Tag Info

Hot answers tagged

5

This is the approach to add a rewrite rule for swapping out the template that WP wants to render (usually the 404 template) with something else. In your case you're just wanting to die with some JSON data so there are a bunch of different ways to do this. This is the "by the book" approach to adding a rewrite rule instead of a hack to check the URL on your ...


2

The permastruct /%category%/%postname%/ will include the categories and subcategories in the URL from top to first assigned child. So, if you want the URL be site.com/source/books/moby-dick/*postname*, you have to assign the post only to "Moby Dick". Assigning the post only to "Moby Dick" category will still show the post under "source" and "books" category ...


2

The easiest way I can think of (and the only one) is create a page with the permalink you're indicating, create a custom template in your theme folder, let's say it's json_output.php with the following content: <?php /* * Template Name: JSON Output */ header('Content-Type: application/json'); $cats = array('Hobbes', 'Simba', 'Grumpy Cat'); echo ...


1

It seems to me you could use pre_get_posts action hook to modify the query, but it feels a bit of a hack. Anyways, something like this ? add_action( 'pre_get_posts', 'cats_endpoint' ); function cats_endpoint($query ){ if ( !is_admin() && $query->is_main_query() ) { if ($query->query_vars['post_type'] == 'cats'){ ...


1

You can use pre_get_posts as suggested but simply check the global $wp object for the request url.. like so: add_action( 'pre_get_posts', function ($query ){ global $wp; if ( !is_admin() && $query->is_main_query() ) { if ($wp->request == 'cats'){ header('Content-Type: application/json'); $cats = ...



Only top voted, non community-wiki answers of a minimum length are eligible