我一直在使用 Laravel 进行我所有的应用程序开发,但最近发现 Wordpress 非常擅长通过大量令人惊叹的主题、小部件和自定义选项引导高质量 View 。
由于出色的指南@ http://grossi.io/2014/working-with-laravel-4-and-wordpress-together/,让 Laravel 和 Wordpress 并排工作很容易设置
问题是如何连接到 WP 的 HTML/CSS/Js 生成函数,以便我可以通过 post_id 将内容带回 Laravel View 。这是我想要转换的典型数据:
post_content = <code>[fullwidth backgroundcolor="" backgroundimage="" backgroundrepeat="no-repeat"] </code>
<h1 style="text-align: center; font-size: 30px !important;">Test test<span style="color: #e9a825;"> #1 </span>testing testing</h1>
<p style="text-align: center; margin-top: -10px; font-size: 17px !important;">With over [tooltip title="Stack Overflow is awesome"]<strong>yeah!</strong>[/tooltip]more [tooltip title="abc"]<strong>test</strong> [/tooltip] test.</p>
[/fullwidth]
目标是尽可能在 Laravel 内部完成所有生成,因为理想情况下,我想在生成 HTML/CSS/JS 之前将自定义数据从我的 Laravel 应用程序注入(inject)到查询的帖子内容中,然后我将传递到我的查看。
最佳答案
我找到了适合我的解决方案。我将此代码添加到我的 BaseController。 使用如下:
echo $this->_get_wp_post(123);
public function _get_wp_post($post_id) {
ob_start();
get_header();
$header = ob_get_contents();
ob_end_clean();
ob_start();
$content = '';
global $post;
$post = get_post($post_id);
setup_postdata($post);
the_content();
$content = ob_get_contents();
wp_reset_postdata();
ob_end_clean();
ob_start();
get_footer();
$footer = ob_get_contents();
ob_end_clean();
return $header . $content . $footer;
}
警告:自定义主题中的某些元素可能需要进行一些调整才能按预期工作和/或消除 Laravel 异常。
希望对大家有帮助。
关于php - 如何将 Wordpress post_content 转换为 Laravel View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946844/