我正在尝试为帖子输出我的 post_tags 并使用 Timber 文档中的示例。它按预期工作,但我试图将每个标签显示为一个按钮。我相信这可能看起来很简单。我只是想不通。
现在两个标签都显示为一个按钮。我试过 for 循环而不是 if 语句,它在视觉上工作(每个标签都作为按钮)但 for 循环似乎拉动了所有的 post_tags;即使是未用于此特定帖子的那些。我试过“hide_empty”,但也没有用。有没有其他方法可以解决这个问题?谢谢你!!
PHP文件
$context['categories'] = Timber::get_terms('post_tag');
Run Code Online (Sandbox Code Playgroud)
仅带有 if 语句的 Twig 文件(输出此帖子上的 2 个标签,但它是一个按钮)
{% if categories %}
      <a href="{{post.term.link}}" class="btn btn-primary">{{ post.terms('post_tag') | join(', ') }}</a>
      <pre>{{post.terms('post_tag')|print_r}}</pre>
 {% endif %}
Run Code Online (Sandbox Code Playgroud)
带有 for 循环的 Twig 文件(输出所有 post_tags,每个标签作为一个按钮)
 {% for term in categories %}
            <a href="{{post.term.link}}" class="btn btn-primary">{{term.name}}</a>   
        {% endfor %}
Run Code Online (Sandbox Code Playgroud)
有没有办法分别显示它们?
解决了:
哎呀。我正在寻找错误的区域。应该检查帖子而不是术语。
 {% for term in post.terms('tags') %}
    <li><a href="{{term.link}}">{{term.name}}</a></li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 好家伙!我不能让这个工作.关于我做错了什么的任何想法?这是代码.
我试图回应脚本,但使用PHP函数来获取js文件的目录!
任何帮助都会被贬低!!
echo '<script src="<?php get_some_function();?> . /js/main.js"></script>';
Run Code Online (Sandbox Code Playgroud)
我已尝试使用转义的dis scenerios,但无法正确输出.
就像标题一样,我试图从附加到分类法的自定义字段中获取图像.我目前有一个名为city的分类,这个术语可能就像新墨西哥州的Albuquerque.我用ACF创建了一个图像自定义字段('city_hero_image'),并选择了ID作为返回值.查看源代码,在src标记中它表示"未知",并且在执行print_r时它基本上返回一个空对象数组.
我的分类 - city.php是
$context['posts'] = Timber::get_posts();
$context['categories'] = Timber::get_terms('city');
$cover_image_id = get_field('city_hero_image');
$context['cover_image'] = new TimberImage($cover_image_id);
Timber::render( $templates, $context );
Run Code Online (Sandbox Code Playgroud)
在我的分类学-city.twig中我有
<img src="{{cover_image.get_url}}" class="img-responsive" alt="">
Run Code Online (Sandbox Code Playgroud) 我正在尝试相对于其按钮或使用 jquery 单击的按钮来定位弹出窗口。我想以不覆盖按钮本身的方式定位弹出窗口。将其放置在单击的按钮的左侧、右侧、上方或下方。
现在我知道我可以通过编写更多的 html 弹出窗口和 css 来做到这一点,但必须有一种方法来动态使用一个 div 并用 jquery 定位它。我尝试使用偏移量和位置(在某一点),但我无法让它工作。坦率地说,我对 js 和 jquery 非常入门,所以请原谅我的菜鸟。
任何帮助将不胜感激!!
JS:
 $('.trends').click(function () {
     $('.questions').fadeIn();
     $('.question').html('<p>What trends could potentially drive growth in the U.S.?</p>');
     /* if I add this and zero out the positioning via css the pop gets offset but its way far away from this parent.
     var offset = $(this).offset();
                $('.questions').css('left',offset.left);    
                $('.questions').css('top',offset.top);*/
 });
 $('.consumer').click(function () {
     $('.questions').fadeIn();
     $('.question').html('<p>Even though we have low inflation, consumers are not increasing their spending. Why?</p>');
 }); …Run Code Online (Sandbox Code Playgroud)