【问题标题】:Wordpress: On get template, Parts not showingWordpress:在获取模板时,部分未显示
【发布时间】:2019-11-07 15:41:35
【问题描述】:

我正在尝试使用以下代码将我的一些帖子从 Single.php 模板重定向到我的 training-v2 自定义模板,但 get_template_part 不起作用。

我的模板是按顺序排列的

themes/theme/templates/training-v2.php

themes/theme/single.php

有人可以帮忙解决get_template_part 结构吗?

<?php
if (have_posts()) : while (have_posts()) : the_post();

    $custom_arr = array(6647,6794,6796);
    if (in_array(get_the_ID(), $custom_arr)) {
        get_template_part( 'themes/theme/templates', 'training-v2' );
    }
    else {
       get_template_part( 'theme/content', 'single' ); 
    }

endwhile; else :

    get_template_part( 'parts/content', 'index' );

endif;
?>

【问题讨论】:

  • 能否请您分享根文件夹中文件的完整路径,以便我可以帮助您解决此问题。
  • 嗨,我的完整路径是 wp-content->themes->theme->templates->training-v2 我的 single.php 路径是 wp-content->themes->theme->single .php

标签: php wordpress php-7


【解决方案1】:

假设您说的路径(即 www.example.com/wp-content/themes/theme/ 为您的主题根目录),它必须是:

get_template_part( 'templates/training', 'v2' );

Codex wordpress get_template_part

第一个参数是主题根目录的路径,第二个参数用于版本控制。第二个是可选的。 页脚或页眉是一个很好的例子,为什么您需要对文件进行不同的版本控制。允许更好的模板结构。

【讨论】:

  • 你能解释一下为什么training和v2..之间有逗号分隔吗?
  • 假设你的WP主题的路径是:www.example.com/wp-content/themes/theme/templates/training-v2.php那么get模板一定是:get_template_part( 'templates/training', 'v2' );因为你的主题的根是/wp-content/themes/theme/
  • 由于 single.php 是单个帖子的默认文件,您必须将问题的代码以及我回答的更改放在 single.php 文件中的 get_header 之后
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
相关资源
最近更新 更多