admin管理员组文章数量:1419249
I've encountered an issue that I can't seem to figure out.
I am using require_once to place content before and after the page content on my homepage. The before content works perfectly and displays fine. If I use text for the after content such as $aftercontent: 'this is my text';
However, if I attempt another require_once call the after content is situated directly beneath my before content, and before The $content. Code below.
// -----------------------------------------
// HOMEPAGE HERO & FOOTER CALL
function wpdev_before_after($content) {
if ( is_user_logged_in() && is_front_page() ) {
$beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-registered-include.php' );
$aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-registered-include.php' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-unregistered-include.php' );
$aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-unregistered-include.php' );
$fullcontent = $beforecontent . $content . $aftercontent;
}
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
I've encountered an issue that I can't seem to figure out.
I am using require_once to place content before and after the page content on my homepage. The before content works perfectly and displays fine. If I use text for the after content such as $aftercontent: 'this is my text';
However, if I attempt another require_once call the after content is situated directly beneath my before content, and before The $content. Code below.
// -----------------------------------------
// HOMEPAGE HERO & FOOTER CALL
function wpdev_before_after($content) {
if ( is_user_logged_in() && is_front_page() ) {
$beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-registered-include.php' );
$aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-registered-include.php' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$beforecontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/hero-unregistered-include.php' );
$aftercontent = require_once ( WP_CONTENT_DIR . '/themes/moon-child/includes/footer-unregistered-include.php' );
$fullcontent = $beforecontent . $content . $aftercontent;
}
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
Share
Improve this question
asked Aug 1, 2019 at 4:07
CongoDeuceCongoDeuce
31 bronze badge
1 Answer
Reset to default 0If the file you want to attach (eg. footer-registered-include.php
) print/display some text, this text will be displayed in the time you include the file.
To assign to the variable the content displayed by the included file you should:
- turn on output buffering
- include file
- contents of the output buffer and end output buffering
function wpdev_before_after($content)
{
if ( is_user_logged_in() && is_front_page() )
{
ob_start();
require_once( get_stylesheet_directory() . '/includes/hero-registered-include.php' );
$beforecontent = ob_get_contents();
ob_clean();
// -- OR --
// $beforecontent = ob_get_clean();
// ob_start();
include_once( get_stylesheet_directory() . '/includes/footer-registered-include.php' );
$aftercontent = ob_get_clean();
$fullcontent = $beforecontent . $content . $aftercontent;
}
// else {
// ...
// }
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
References:
- get_stylesheet_directory()
本文标签: phpBefore amp After ContentAfter Content directly below Before Content when using requireonce
版权声明:本文标题:php - Before & After Content - After Content directly below Before Content when using require_once 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745279340a2651348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论