0

If I have this URL for my custom taxonomy term http://example.com/work_type/vehicle-transporting/ how can I show it as the main page http://example.com/ like it is available to do for the static pages at Setings > Reading menu?

.htaccess solutions are acceptable, changing theme's index.php is not.

Update:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/work_type/vehicle-transporting/
RewriteRule ^(.*)$ / [P]
</IfModule>
# END WordPress

Why it didn't help?

4
  • RewriteCond %{REQUEST_URI} ^/work_type/vehicle-transporting/ RewriteRule ^(.*)$ / [P] I tried this but got no success
    – Pavel K
    Jun 28, 2013 at 15:40
  • 1
    You're rewriting it in wrong direction, I guess. But why don't you use static front page and then use front-page.php template to show posts from this taxonomy? Or just use home.php template? Jun 28, 2013 at 15:46
  • Just want to do it in one .php file, especially if it is possible.
    – Pavel K
    Jun 28, 2013 at 15:55
  • It is possible, but it's not nice solution. A lot nicer one would be to use home/front-page template or to modify main wp_query (both solutions are described below in my answer - if you need any details, just ask). Jun 28, 2013 at 16:04

3 Answers 3

2

I agree with @KrzysiekDróżdż While you should probably be able to do this it might be unnecessary work.

However, this works with I test it, just change the category/term name and the template name (the part in get_template_part), and maybe the is_main_query() && $qry->is_home() conditional if you need to:

function pregp_home_cat_wpse_104619($qry) {
  if (is_main_query() && $qry->is_home()) {
    $qry->set('category_name','aciform');
    $qry->set('ignore_sticky_posts',true);
    add_action('template_redirect','tr_home_cat_wpse_104619');
  }
}
add_action('pre_get_posts','pregp_home_cat_wpse_104619');

function tr_home_cat_wpse_104619() {
  get_template_part('category','aciform');
  exit;
}

I have never tried to do this in production, so "when I test it" means that I've spent about 10 minutes and don't see any obvious problems. I make no promises.

Barely tested. Possibly buggy. Caveat emptor. No refunds.

3
  • Using template_include hook would be even nicer, I guess - you don't have to call exit then. Jun 28, 2013 at 16:24
  • @KrzysiekDróżdż : True, seems to work the same using that hook (which, for some reason, I forget about :) ).
    – s_ha_dum
    Jun 28, 2013 at 16:27
  • @s_ha_dum yes, get_template_part() just what I need! My taxonomy-work_type.php and index.php became clear and looks in WP style. Thank you.
    – Pavel K
    Jun 29, 2013 at 10:56
1

Solution 1.

One solution would be to use home.php template file, or use static page as front page and then use front-page.php template file.

You can use get_template_part function to include your taxonomy template inside, or use template_include hook to redirect one of front-page/home template to your custom taxonomy template.

Solution 2.

You can also use pre_get_posts filter to change main WP query in such way, that it shows only posts from this custom taxonomy.

-1
  1. Copy your single taxonomy template code.
  2. Paste that into your home page template. Or create a new one.
  3. Add the following to the top of the page $post->ID = XX (XX being the ID of your post)
  4. Set that template as your homepage in the WP settings.
3
  • Can you please add explanation how this solves the issue? Jul 10, 2019 at 9:53
  • Sorry, I am not the one who downvoted. Jul 11, 2019 at 10:20
  • @Nilambar - The question is how to display a single taxonomy as the homepage. My solution does that... Jul 11, 2019 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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