hit*_*h95 0 wordpress twig timber
可能这是显而易见的事情,已经在我的鼻子底下逃过了一劫。
我不明白如何将我的扩展课程与木材挂钩。
让我们以问题为例。我怎样才能得到一个像MySitePost我打电话时的对象Timber::query_post()?
所以现在我有了一种简单的方法来引用
{{ post.issue }}我们的树枝模板中的 。看起来像一个自动过程......但我对此不太确定!
供参考:https : //timber.github.io/docs/guides/extending-timber/
我认为这是一个可能不太明显的好问题。
要使用自定义帖子,您通常会在获取帖子时找到一个参数,您可以在其中传递自定义类的名称。返回的对象将是您的自定义类的实例。
$posts = Timber::get_posts( $your_args, 'MySitePost' );
$posts = new Timber\PostQuery( $your_args, 'MySitePost' );
Run Code Online (Sandbox Code Playgroud)
当您想获取单个帖子时,它的工作原理非常相似。您可以直接实例化您的帖子或将您的帖子类传递给该函数。
$post = new MySitePost();
$post = Timber::get_post( $post_id, 'MySitePost' );
Run Code Online (Sandbox Code Playgroud)
如果要设置用于帖子的默认类,可以使用Timber\PostClassMap过滤器:
// Use one class for all Timber posts.
add_filter( 'Timber\PostClassMap', function( $post_class, $post_type ) {
return 'MySitePost';
}, 10, 2 );
// Use default class for all post types, except for pages.
add_filter( 'Timber\PostClassMap', function( $post_class, $post_type ) {
// Bailout if not a page
if ( 'page' !== $post_type ) {
return $post_class;
}
return 'MySitePost';
}, 10, 2 );
// Use a class map for different post types
add_filter( 'Timber\PostClassMap', function( $post_class, $post_type ) {
return array(
'post' => 'MySitePost',
'apartment' => 'MyApartmentPost',
'city' => 'MyCityPost',
);
}, 10, 2 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |