如何将Zend Layout用作WordPress主题?

5

我编写了一个Zend应用程序,并包含了WordPress进行博客。当我第一次安装WordPress时,我将其主题设置为与主应用程序使用相同的标题等。自那以后,我已经两次重新设计了主题,并不得不重新设计WordPress主题以匹配。是否有一种方法让WordPress使用我的Zend布局?我的第一个想法是将我的布局分解成头文件和脚注文件,并使用完整路径从WordPress中包含它们。虽然这样可以工作,但远非理想(我更喜欢将布局文件保持在一个完整的文件中)。


你需要一种“模板包装器”,它可以将你布局中的占位符替换为WordPress的内容。 - opHASnoNAME
2
你可以使用partials,将头部、底部分离出来并在WordPress结构中进行包含。 - digoferra
4个回答

0
如果您有一个页面类,布局/视图脚本使用类似的东西,您可以在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()并不容易。

我不知道这是否是最好的方法,如果有更好的方法,我会很感兴趣。


0

对于许多网站,您可能需要安装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 Layout

在你的新 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_Application绝对是疯狂的!如果您愿意,可以使用Zend_LayoutZend_View作为独立组件,而无需加载Zend_Application附带的所有其他内容。[阅读文档->](http://framework.zend.com/manual/en/zend.layout.quickstart.html#zend.layout.quickstart.standalone) - adlawson

0

我已将我的layout.phtml文件分成了头部和底部的局部文件。

在我的WordPress主题文件中,我正在包含我的局部文件。

一个更优雅的解决方案可能是编写自己的get_header/footer函数来包含这些局部文件?我认为这应该放在主题的function.php文件中?


0

你只需要将视图对象变量(来自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 -->

希望这能有所帮助 ;)

实际上,您必须假设从Zend加载模板与Wordpress的get_template_part()函数是相似的(或多或少)。 - Covi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接