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 一分十不等功分器阻设计问题,请问这个56Ω怎么得到的
  • ¥15 (标签-matlab)
  • ¥100 求看看这个数学建模,有偿
  • ¥15 深度学习目标检测现在框架加注意力的创新可以投几区?
  • ¥15 PdfiumViewer pdf转图片
  • ¥15 利用Java连接API接口总是出问题
  • ¥15 请教一个关于镜头标定,棋盘格格子大小的问题(畸变测试)
  • ¥15 安装GroudingDINO RuntimeError: Error compiling objects for extension
  • ¥15 急!C++指针编写相关的问题