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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵