I have a custom single page single-ENG.php
. I want to use this page for posts with a taxonomy language=>english
. Is this possible?

Wordpress使用自定义single.php
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- dongyin8991 2012-11-14 15:30关注
Yes, this is possible, but I think you need to take a look at the Wordpress Template Hierarchy first.
There are a few issues with your approach:
You shouldn't name your Custom Page Templates 'single-xxxx.php'. The 'single' prefix is used for a Single Post view. This could confuse Wordpress and cause it to only load the template when you are viewing a Single Post of Post Type 'ENG' (which probably doesn't exist in your theme).
Using Pages as a Shell for any kind of Post content is not advised. The reason for this is that you're essentially circumventing the existing tools Wordpress provides in order to force it to do something it can already do using its own built-in defaults.
Instead of creating a whole new Page Object to house your Posts of a given Taxonomy, why not just create a taxonomy-language-english.php file, and set up its navigation in your theme's Menu (Dashboard->Appearance->Menus)?
Provided you've actually Registered your Language Taxonomy, Wordpress will automatically recognize the new Taxonomy template and query all appropriate data in its default loop.
This details how you can query your Posts using two methods. The first is the one I advise using, provided you change your structure to fit as an exercise in good practice. The second is your approach by applying a Custom Template to a given Page. I've taken the liberty using a new file name to avoid confusing Wordpress:
Using taxonomy-language-english.php
<?php if(have_posts()) : while(have_posts()) : the_post(); echo get_the_title().'<br/>'; //Output titles of queried posts endwhile; else : echo 'No posts were found'; //No posts were found endif; ?>
Using pagelang-english.php
<?php /** * @package WordPress * @subpackage MyWordpressThemeName * Template Name: Single English */ $args = array('tax_query' => array( array( 'taxonomy' => 'language', 'field' => 'slug', 'terms' => 'english' ) )); $q = new WP_Query($args); if($q->have_posts()) : while($q->have_posts()) : $q->the_post(); echo get_the_title().'<br/>'; //Output titles of queried posts endwhile; else : echo 'No posts were found'; //No posts were found endif; ?>
This should be enough to get you started. Good luck.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报