我需要其他人提供一些严肃的Wordpress专业知识。 我正在构建一个自定义主题,阅读了get_template_part()
方法,并决定这将有助于清理和组织我的代码。 我更新了我的主题以在几个地方使用这种方法,现在网站完全坏了......没有一个东西会加载!!:( 我真的很感激任何帮助! 我将复制并粘贴下面的一些 php 文件。 也许对使用此方法有更多经验的人可以指出问题所在。 谢谢!
页.php
<?php get_header(); ?>
<div id="content" class="content">
<?php while ( have_posts() ) : the_post(); ?>
<!-- About page -->
<?php if (is_page('about')) ?>
<?php get_template_part( 'page', 'about' ); ?>
<!-- Media & Gallery page -->
<?php if (is_page('media-gallery')) ?>
<?php get_template_part( 'page', 'mediagallery' ); ?>
<?php endwhile; // end of the loop. ?>
</div> <!--/end content -->
<?php get_footer(); ?>
页面关于.php
<div id="about">
<div class="text">
<h2 class="about"><?php echo get_the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
页面媒体库.php
<div id="pic-gallery">
<?php include_once 'includes/pwaplusphp/albums.php'; ?>
</div>
索引.php
<?php get_header(); ?>
<div class="content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<br class="clr"/>
<?php endwhile; ?>
<?php content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h3 class="blog"><?php _e( 'Nothing Found' ); ?></h3>
</header> <!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.'); ?></p>
</div> <!-- .entry-content -->
</article> <!-- #post-0 -->
<?php endif; ?>
</div> <!--/end content -->
<?php get_footer(); ?>
内容库.php
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-date"><?php the_time(get_option('date_format')); ?></div>
<h3 class="blog"><?php the_title(); ?></h3>
<div class="post-content"><?php the_content(); ?></div>
</div>
有什么想法为什么这些可能不起作用吗? 所有这些文件都在主题的根目录下,因此get_template_part()
方法应该是查找所有文件。 奇怪的是,Wordpress没有吐出任何代码。即。自从我开始使用此方法以来,当我检查源代码时,我的浏览器中没有吐出一行代码。
我正在使用最新版本的Wordpress和Google Chrome浏览器作为我的浏览器。
我在每个.php文件的顶部也有这段代码,但这不应该搞砸任何东西,因为它的注释如下所示:
<?php
/**
* I put a description of what the file does here.
*
* @package Keynote_WP_Themes
* @subpackage Rhymz_Suhreal
* @copyright Rhymz Suhreal, 2012
* @author Kyle Affolder <myemail@example.com>
*/
?>
我对自己做错了什么一无所知。 :(
想法编码的朋友?!
以你的page-about.php为例,你应该在要放置它的模板中这样调用它:
<?php get_template_part( "page-about" ); ?>
模板需要位于主题目录的根目录中,以便您可以在没有.php
的情况下使用"page-about"
,而不必指示文件所在的位置/目录。
我以前也遇到过几次 - 我无法真正解释为什么,因为我仍在挖掘文档,但似乎你不能以这种方式调用模板部分当你在后循环;)
任何时候你在循环内,尝试改用这个:
<?php include(get_query_template('page-about')); ?>