doukan6564 2012-11-14 14:34
浏览 184
已采纳

Wordpress使用自定义single.php

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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?