【问题标题】:custom post page template自定义帖子页面模板
【发布时间】:2012-06-18 11:43:46
【问题描述】:

我遇到了这个脚本的问题,它应该在查看自定义帖子页面时加载自定义模板。

我已经放置了一个 echo 命令来确保 url 是正确的,但我什至没有回显 url。

function da_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'include') {

          $single_template = PLUGIN_PATH. 'template/custom_template.php';
     }
     echo $single_template;
     return $single_template;
}
add_filter( "single_template", "da_custom_post_type_template" ) ;

请帮忙

【问题讨论】:

  • 尝试回显“test”;并检查它是否在回显......我们将知道它在函数内部
  • 不,它看不到在函数内部回显..... :(
  • @DavidAllen 你如何将 $single_template 传递给你的方法??? ;)
  • 刚试了一下还是不行...:(

标签: php wordpress


【解决方案1】:

把这个放到single.php中:

<?php
      global $wp_query;

      $post = $wp_query->post;
      $post_type = get_query_var('post_type');

      if($post_type == 'include'){
        include(TEMPLATEPATH.'/my_post_template.php');
      }else{
        include(TEMPLATEPATH.'/default_post_template.php');
      }#end else

?>

【讨论】:

  • 我正在制作一个插件,所以我不能这样做。
【解决方案2】:

实际上,您仍然可以过滤单个模板。它仍然存在于wp-includes/template.php

我看不出你的功能有什么问题。你确定模板文件存在吗?

编辑:

试试这个:

function da_custom_post_type_template( $template ) {
    global $post;

    if ($post->post_type == 'include') {
        $template = dirnamr( __FILE__ ) . '/template/custom_template.php';
    }
    return locate_template( $template );
}
add_filter( "single_template", "da_custom_post_type_template" ) ;

【讨论】:

  • 我的 wp-includes 文件夹中没有 template.php。
  • 我已经重新下载了wordpress,现在有了一个template.php
【解决方案3】:

终于搞定了……

我重新下载了 wordpress 并将我的代码更改为以下...

gobal $post 似乎没有设置任何内容,所以我改用了 $wp_query。感谢 CroiOS 指出这一点。

还要感谢 CMF 为我在 word press 问题上的安装指明了正确的方向。我通过 SSH 安装了 WP,所以我想知道我是否下载了夜间构建或类似的东西..

function da_custom_post_type_template( $template ) {
    global $wp_query;
    if ($wp_query->post->post_type == 'include') {
      $template = PLUGIN_PATH . '/template/custom_template.php';
    }
    return $template ;
}
add_filter( "archive_template", "da_custom_post_type_template" ) ;

由于某种原因,它的类也是 archive_template :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2013-12-06
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多