如何解决如何使用Zend布局作为Wordpress主题?
| 我编写了一个Zend应用程序,并包括用于博客的Wordpress。当我第一次安装Wordpress时,我以它为主题,因此它使用的标题与主应用程序相同。此后,我已经重做了两次主主题,并且不得不重做Wordpress主题以进行匹配。 Wordpress是否可以使用我的Zend布局?我的第一个想法是将布局分解为页眉/页脚文件,并使用完整路径将其包含在Wordpress中。尽管它可以工作,但远非理想(我希望将布局文件保存在一个文件中)。解决方法
如果您拥有布局/视图脚本使用的页面类或类似的页面类,则可以在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进行博客集成。当涉及到对博客进行外观设置时,您几乎只能将html从Zend_Layout复制到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,strpos($layout,\'<div id=\"container\">\'));
?>
<div id=\"container\">
这将从先前的脚本中加载$ layout变量,并将所有内容回显到您的主容器div中。
页脚
footer.php与之类似:
</div><!-- #main -->
<?php
include(\'zend_layout.php\');
echo substr($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 -->
希望能帮助到你 ;)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。