【问题标题】:WordPress: Displaying whole template, not just the_contentWordPress:显示整个模板,而不仅仅是 the_content
【发布时间】:2013-05-30 09:45:52
【问题描述】:

我有一个带有 front-page.php 的单页网站,其中应包含我所有的自定义模板(每个都是静态页面)。

<?php get_header();

$args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'menu_order',
    'post_type' => 'page',
    'post_status' => 'publish'
);

$pages = get_pages($args);

foreach ($pages as $page_data) {
    $content = apply_filters('the_content', $page_data->post_content);
?>

<?php echo "$content" ?>

<?php
}
get_footer();
?>

使用“the_content”,它仅引用我在后端的文本内容,而不是模板本身中围绕我的 the_content 标签的所有标签,例如:

<?
/*
    Template Name: Features
*/

    the_post()
?>

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

<section>
    <? the_content() ?>
</section>

<?php endif; ?>

在这种情况下,周围的标签 section 未被选中,也不会出现在标记中。

我必须在我的 front-page.php 中进行哪些更改才能选择整个模板,而不仅仅是它的模板?

提前非常感谢!

【问题讨论】:

    标签: wordpress-theming wordpress


    【解决方案1】:

    您可以将每个页面模板的循环部分放在单独的模板部分中,并在首页上使用get_template_part动态调用这些部分:

    foreach ( $pages as $page_data ) {
        $template = get_post_meta( $page_data->ID, '_wp_page_template', true ); // get page template path
        $template = substr( $template, 0, -4 ); // chop off .php from the string
        get_template_part( 'loop', $template );
    }
    

    对于模板“功能”(文件名features.php),它将查找loop-features.php 等。后备将是loop.php

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 1970-01-01
      • 2015-03-02
      • 2018-09-02
      • 1970-01-01
      • 2017-03-26
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多