简体   繁体   English

简码wordpress php

[英]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]

In the Shortcode API , you can find a lots of examples how you can implement this. Shortcode API中 ,您可以找到许多实现此示例的示例。 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. 因此,此功能download_shortcode()应该位于主题的functions.php文件中,或在单独的插件中。 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. 唯一的问题是,您无法在此处传递php代码(在内部,作为内容或作为参数),因此您必须进行选择,将参数添加到简码(分类法/术语字段)中,或在打开和关闭之间插入内容[download] [/ download]简码。

I'm not 100% clear on what you're trying to accomplish. 对于您要完成的目标,我还不是100%清楚。 Is this code in a template where you want to use the shortcode? 此代码是否在您要使用简码的模板中? If so, you need to use the do_shortcode function, like so: 如果是这样,则需要使用do_shortcode函数,如下所示:

echo do_shortcode('[sociallocker]<a rel="nofollow" href="' . the_field('download_link',$taxonomy . '_' . $term_id) . '" target="_blank">DOWNLOAD LINK 1</a>[/sociallocker]');

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

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