如何将post ID传递给像edit_post_link这样的Twig/Timber函数?
在https://timber.github.io/docs/guides/functions/#function-with-arguments阅读文档
像edit_post_link这样的函数将尝试从循环中的当前帖子中猜测要编辑的帖子的ID。相同的函数需要对像archive.twig或index.twig这样的文件进行一些修改。在那里,您需要显式地传递post ID。
这就是发生的事,当我用这个
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>', post.ID) }}
在index.twig中,所有编辑链接都有显示自定义post类型循环的页面的post ID,而不是循环中每个自定义post类型的post ID。
我在functions.php中使用了下面的函数,它还强制编辑链接上的target="_blank":
add_filter( 'edit_post_link', 'newwindow_edit_post_link', 10, 3 );
global $post;
$post_id = $post->ID;
function newwindow_edit_post_link( $link, $post_id, $text ) {
if( !is_admin() )
$link = str_replace( '<a ', '<a target="_blank" ', $link );
return $link;
}这是index.twig的基本循环。"people“是标准的WordPress自定义post类型:
{% if people %}
{% for person in people %}
<a href="{{ person.link }}">{{ person.name }}</a>
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>', post.ID) }}
{% endfor %}
{% else %}
{% endif %}这将导致指向该页面的所有编辑链接,而不是每个定制的post类型“person”。
那我该怎么打邮政身份证呢?是否需要在自定义post类型函数中调用Post ID?
主index.php文件具有标准的Twig函数:
$context = Timber::get_context();
$context['posts'] = Timber::get_posts();
$templates = array( 'index.twig' );
Timber::render( $templates, $context );发布于 2018-10-29 06:49:18
那我该怎么打邮政身份证呢?
如果您的index.twig模板中的循环中的index.twig是一个post数组(即每个post都是一个WP_Post / Timber\Post实例),那么您可以(或者应该能够)通过person.ID或person.id检索post ID (是的,两者实际上都是设置)。所以这些对我来说很管用:
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>', person.id) }}
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>', person.ID) }}我是如何确认上述的
front-page.phptemplates/front-page.twig:
{%扩展"base.twig“%} {%块内容%}查询页的标题:{ post.title }查询页的ID:{{ post.id }} {{ post.id} {%,如果用户%} { % } { person %}{ person.name }{函数(‘编辑_post_链接’,‘编辑’,{% person.id) } {% endfor %} {% endif %} {% endif %} {%包括“部分/分页.枝”与{分页: posts.pagination({show_all: false,mid_size: 3,end_size: 2}) }} {% endblock %} }对我来说,一切都很好-- edit_post_link()被正确调用,并在标记中显示带有target="_blank"的post链接。(我把newwindow_edit_post_link的东西放在functions.php里了)
发布于 2018-10-24 18:10:09
查看Twig 2.x文档,默认情况下没有{{ function }} Twig函数。在我使用Symfony的几年中,我肯定从未见过这种情况,所以我怀疑这是定制的吗?
我刚刚在谷歌上搜索了“木材/树枝”,这实际上是一个WordPress插件,可以为你的主题模板提供Twig功能,所以我相信你错误地把Symfony标签放在了你的问题上。我建议删除它,添加wordpress,这样您就可以吸引比我的答案更有用的答案了。
为了得到保证,我们需要查看您的自定义edit_post_link Twig函数的PHP源代码。然而,在PHP端和Twig端,您似乎只需要在参数中映射相同的顺序。例如,如果您的功能是:
function edit_post_link(string $label, string $openingHtml, string $closingHtml, int $postId) {
// blah blah blah
}在您向Twig注册了此函数之后(尽管Timber似乎声称您可能不需要这样做,请进行检查),然后您将按照您所写的那样使用它:
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>', post.ID) }}我感觉到这可能不是你得到的,也许你想知道你是如何从一开始就掌握post.ID的。如果是这样的话,那么您的问题与{{ function }}无关,我们需要看到更多的Twig模板源代码以及您从PHP公开给它的变量。
发布于 2018-11-01 17:00:11
这很难看,但是如果您不能让edit_post_link函数在template.twig中工作,而且{{ person.id }}也能工作,那么您可以在小枝模板中使用这个设置。
它确定用户是否登录并可以编辑,如果可以,则显示一个在新选项卡中打开的编辑链接--带有{{ person.id }}的动态链接:
{% if user %}
<a class="style-me" target="_blank"
href="{{ site.url }}/wp-admin/post.php?post={{ person.id }}&action=edit">Edit</a>
{% endif %}https://stackoverflow.com/questions/52974561
复制相似问题