最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

templates - How can I make front page to display custom taxonomy page?

programmeradmin0浏览0评论

If I have this URL for my custom taxonomy term / how can I show it as the main page / 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?

If I have this URL for my custom taxonomy term http://example/work_type/vehicle-transporting/ how can I show it as the main page http://example/ 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?

Share Improve this question edited Jun 28, 2013 at 15:51 Pavel K asked Jun 28, 2013 at 15:13 Pavel KPavel K 1012 silver badges7 bronze badges 4
  • RewriteCond %{REQUEST_URI} ^/work_type/vehicle-transporting/ RewriteRule ^(.*)$ / [P] I tried this but got no success – Pavel K Commented 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? – Krzysiek Dróżdż Commented Jun 28, 2013 at 15:46
  • Just want to do it in one .php file, especially if it is possible. – Pavel K Commented 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). – Krzysiek Dróżdż Commented Jun 28, 2013 at 16:04
Add a comment  | 

3 Answers 3

Reset to default 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.

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. 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.
发布评论

评论列表(0)

  1. 暂无评论