dsfdsfdsfdsf1223 2016-04-28 21:31
浏览 33

Shortcode wordpress php

How do i add a shortcode around this code:

    <a rel="nofollow" href="<?php the_field('download_link',$taxonomy . '_' . $term_id); ?>"target="_blank">DOWNLOAD LINK 1</a>

I have already tried the code below. It doesn't actually activate the short code.

    [sociallocker]
    <a rel="nofollow" href="<?php the_field('download_link',$taxonomy . '_' . $term_id); ?>"target="_blank">DOWNLOAD LINK 1</a>
     [/sociallocker]
  • 写回答

2条回答 默认 最新

  • donglie7778 2016-04-28 21:57
    关注

    In the Shortcode API, you can find a lots of examples how you can implement this. Here is a possible solution:

    function download_shortcode( $atts, $content = null ) {
        return '<a rel="nofollow" href="' .  the_field('download_link',$atts['taxonomy'] . '_' . $atts['term_id']); . '" target="_blank">DOWNLOAD LINK 1</a>';
    }
    add_shortcode( 'download', 'download_shortcode' );
    

    When used like this:

    [download taxonomy=text1 term_id=text2]
    

    The output would be:

    <a rel="nofollow" href="/*The response from the function*/" target="_blank">DOWNLOAD LINK 1</a>
    

    So, this function download_shortcode() should be in your theme's functions.php file, or in a separate plugin. The only problem here is that you can't pass php code there (inside, as content or as parameters), so you have to choose, add parameters to the shortcode (the taxonomy/term fields), or insert content between opening and closing [download][/download] shortcodes.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看