从短代码函数内部调用 WordPress get_template_part 会首先呈现模板 [英] Calling WordPress get_template_part from inside a shortcode function renders template first

查看:21
本文介绍了从短代码函数内部调用 WordPress get_template_part 会首先呈现模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我需要允许用户输入一段文本.然后在该文本之后,插入一个将呈现帖子列表的短代码,然后在此之后添加更多自由格式的文本.我的想法是他们应该能够插入一个短代码来输出帖子.这样他们就可以简单地在他们希望帖子出现的位置添加短代码.

I have a page where I need to allow the user to enter a paragraph of text. Then after that text, insert a shortcode that will render a list of posts, then add more free-form text after that. My thought was that they should be able to insert a shortcode which will output the posts. This way they can simply add the shortcode where they wish the posts to appear.

我目前拥有检索在其自己的文件中分隔的帖子的逻辑.目前,我只需使用 get_template_part() 函数将其包含在页面中:

I currently have the logic which retrieve the posts separated in its own file. Currently I include it in a page by simply using the get_template_part() function:

get_template_part('donation', 'posts');

我研究了如何创建短代码并将以下代码包含在我的 functions.php 文件中以创建短代码:

I looked into how to create a shortcode and included the following code into my functions.php file in order to create the shortcode:

add_shortcode('donation-posts', 'fnDonatePosts');   
function fnDonatePosts($attr, $content)
{
    get_template_part('donation', 'posts');    
}

donation-posts.php 正在正确执行并且帖子正在出现,但是,它们始终位于内容之前,而不是放置短代码的位置.

The donation-posts.php is being properly executed and the posts are appearing, however, they are always positioned BEFORE the content and not in the location that the shortcode was placed.

我已尝试删除 get_template_part() 函数并仅输出一些文本,效果很好.所以我知道 get_template_part() 可能不是正确的方法,但是,我无法找到一种方法来做我正在尝试做的事情(我确信有是一种方式......我只是没有找到它).

I have tried removing the get_template_part() function and just output some text and that works fine. So I understand that the get_template_part() may not be the right way to do this, however, I have not been able to uncover a way to do what I am attempting to do (I am sure that there is a way... I just haven't found it).

我试过了:

include(get_template_directory(). '/donation-posts.php');
include_once(get_template_directory(). '/donation-posts.php') : 

但是一旦它们命中包含文件中的 PHP 代码,它们就会停止处理.

But these stopped processing once they hit the PHP code in the included file.

我也试过:

$file = file_get_contents(get_template_directory(). '/donation-posts.php');  
        return $file;

但这仅返回文件的内容(如函数名称所示),这意味着它不会执行 PHP 脚本来返回帖子.

But this only returns the contents of the file (as the function name indicates) which means it doesn't execute the PHP script to return the posts.

以前有人做过吗?

推荐答案

你可以试试这个,它可能会解决你的问题,因为 get_template_part 基本上就像 PHP 的 require,它不会返回,而是在调用它的地方立即回显内容.

You may try this, it may solve your problem because get_template_part basically reacts like PHP's require, it doesn't return but echos the content immediately where it's been called.

add_shortcode('donation-posts', 'fnDonatePosts');   
function fnDonatePosts($attr, $content)
{        
    ob_start();  
    get_template_part('donation', 'posts');  
    $ret = ob_get_contents();  
    ob_end_clean();  
    return $ret;    
}

这篇关于从短代码函数内部调用 WordPress get_template_part 会首先呈现模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆