简体   繁体   English

将PHP脚本添加到wordpress的树枝上

[英]Add a php script to twig on wordpress

im frecuent user of this forum and always find answers from other users, but this time i need to ask my own question. 我是这个论坛的新用户,总是从其他用户那里找到答案,但是这次我需要问自己的问题。

At this moment im editing a wp template based on twig that i bought, right now im facing a fancybox gallery which just call the first image, and i need to add the gallery rel to the other images included in the post. 目前,我正在编辑基于我购买的树枝的wp模板,现在我正面对一个花哨的画廊,该画廊仅调用第一个图像,我需要将该画廊rel添加到帖子中包含的其他图像中。 i already found the missing code which call the rest of images from the post ID, but i dont know how to express it by the twig structure and incorporate it to the original code of the page 我已经找到了缺少的代码,该代码从帖子ID中调用其余图像,但是我不知道如何通过树枝结构表示它并将其合并到页面的原始代码中

this is the code which display the gallery: 这是显示图库的代码:

 {% if wp.get_post_meta(post.ID, '_property_slides', TRUE) %} <div class="carousel property"> <div class="preview"> <a href="{{ wp.get_post_meta(post.ID, '_property_slides', TRUE).0.imgurl }}" class="fancybox"> <img src=" {{ wp.get_post_meta(post.ID, '_property_slides', TRUE).0.imgurl }}" alt=""> </a> // the php code expressed on twig goes here // </div> <!-- /.preview --> <div class="content"> <ul> {% for slide in wp.get_post_meta(post.ID, '_property_slides', TRUE) %} {% if loop.first %} <li class="active" > {% else %} <li> {% endif %} <img src="{{ slide.imgurl }}" alt=""> </li> {% endfor %} </ul> <a id="carousel-prev" href="#">{{ wp.__('Previous', 'aviators') }}</a> <a id="carousel-next" href="#">{{ wp.__('Next', 'aviators') }}</a> </div> <!-- /.content --> </div><!-- /.carousel --> {% endif %} 

This is the php code i need to implement on twig 这是我需要在树枝上实现的php代码

 add_filter('wp_get_attachment_link','add_gallery_id_rel'); function add_gallery_id_rel($link){ global $post; return str_replace('<a href', '<a rel=”galeria'. $post->ID .'” href', $link); } 

I really appreciate if you can help me with this (sorry about my weird english) 如果您能帮助我,我真的很感激(对不起我的英语怪异)

That code is a filter applied to the regular Wordpress functions, you are using a Twig Wrapper so that code won't work for you. 该代码是应用于常规Wordpress函数的过滤器,您使用的是Twig包装器,因此该代码对您不起作用。

Try modifying the twig loop. 尝试修改树枝循环。

{% for slide in wp.get_post_meta(post.ID, '_property_slides', TRUE) %}
  #... rest of the code
  <a rel="galeria{{ post.ID }}" href="{{ slide.imgurl }}">
    <img src="{{ slide.imgurl }}" alt="">
  </a>
  #... rest of the code
{% endfor %}

Skip the first slide using slice : 使用slice跳过第一张幻灯片:

{% for slide in wp.get_post_meta(post.ID, '_property_slides', TRUE)|slice(1) %}
#... rest of the code same as above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM