我已经编写了一个 Zend 应用程序,并包含了用于博客的 Wordpress。当我第一次安装 Wordpress 时,我为它设置了主题,以便它使用与主应用程序相同的标题等。从那以后,我重做了两次主题,不得不重做 Wordpress 主题以匹配。Wordpress 有没有办法使用我的 Zend 布局?我的第一个想法是将我的布局分解为页眉/页脚文件,并使用完整路径从 Wordpress 中包含它们。虽然它可以工作,但它远非理想(我更愿意将布局文件保持在一个整体)。
4 回答
如果您有布局/视图脚本使用或类似的页面类,您可以在 wordpress 主题文件中执行以下操作:
$page = new Page;
$page->setTitle(get_the_title());
$content = '';
if (have_posts())
{
while (have_posts())
{
ob_start();
the_post();
$content .= ob_get_clean();
}
}
$page->setContent($content);
...
$view = new Zend_View();
$view->page = $page;
...
Wordpress 的输出而不是返回的功能并不容易,因此 ob_start()。
我不知道这是否是最好的方法,我很想看看是否有更好的方法。
我已将 layout.phtml 文件分成页眉和页脚部分。
在我的 Wordpress 主题文件中,我包含了我的部分内容。
一个更优雅的解决方案可能是编写我自己的包含这些部分的 get_header/footer 函数?我认为这会放在主题的 function.php 文件中吗?
对于许多网站,您可能需要安装 Wordpress 以进行博客集成。在为博客设置皮肤时,您几乎只能将 Zend_Layout 中的 html 复制到 Wordpress 的 header.php 和 footer.php 文件中。这是重复,如果您对布局进行任何更改,您也需要更新博客主题。好吧,还有另一种方法!
更改 Zend 应用程序
为您的 Zend 应用程序创建一个单独的引导文件(例如,按照本指南:从其他应用程序访问 Zend 应用程序资源)。
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
在您的 index.php 文件中,运行您的应用程序:
include('bootstrap.php'); //where bootstrap.php is the file of the new bootstrap file
$application->run();
WordPress 魔术
现在来了 Wordpress 的魔力。
获取 Zend 布局
在您的新 Wordpress 主题文件夹中,创建一个名为“Zend_Layout.php”的文件并将此代码复制/粘贴到其中:
<?php
//load Zend_Layout
$layout = new Zend_Layout();
//add the blog's stylesheet to the header
$view = $layout->getView();
$view->headLink()->appendStylesheet(get_bloginfo( 'stylesheet_url' ));
// Set a layout script path:
$layout->setLayoutPath(APPLICATION_PATH . "/modules/default/views/scripts");
$layout = $layout->render();
标题
将 header.php 文件更改为:
<?php
include('zend_layout.php');
echo substr($layout, 0, strpos($layout, '<div id="container">'));
?>
<div id="container">
这将从先前的脚本加载 $layout 变量并将所有内容回显到您的主容器 div。页脚
footer.php 类似:
</div><!-- #main -->
<?php
include('zend_layout.php');
echo substr($layout, strpos($layout, '<div id="footer">'));
您只需将视图对象变量(来自 Wordpress 的模型)传递给您的布局。
这是我在项目中如何使用它的一个示例(我使用完整的 Zend_Layout:headLink、headTitle、headStyle...),但第 1 点是混合示例:
<?php
// Layout: New; or get from registry if it's stored in;
// or set your own new Layout with ->setLayout().
$Layout = new Zend_Layout();
$view = $Layout->getView(); ?>
<!-- Header -->
<?php echo $Layout->render('header.phtml'); ?>
<!-- /Header -->
<?php // 1. Now assume you have an usual Wordpress loop:
// (but IMHO I prefer use $wp_query->posts and partialLoop)
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$view->post = get_post(get_the_ID());
$view->render('partials/article.phtml');
// article.phtml works with $this->post as model.
}
}
// 2. Example out of loop with $wp_query:
if ( $wp_query instanceof WP_Query && $wp_query->have_posts() ) {
// Now, we can render your partial article template
// passing the posts as model:
$view->partialLoop()->setObjectKey('post'); // $this->post in article partial template.
$view->posts = $view->partialLoop( 'partials/article.phtml',
$wp_query->posts);
// You can render here your $view->posts var or passing anything.
}
?>
<!-- Sidebar -->
<?php echo $Layout->render('sidebar.phtml');
// or call your Wordpress sidebars: get_sidebar('my_sidebar');
// but, again, I prefer use placeholders for that. ?>
<!-- /Sidebar -->
<!-- Footer -->
<?php echo $Layout->render('footer.phtml'); ?>
<!-- /Footer -->
希望能帮助到你 ;)