Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在1中使用两个短代码_Php_Wordpress_Shortcode - Fatal编程技术网

Php 在1中使用两个短代码

Php 在1中使用两个短代码,php,wordpress,shortcode,Php,Wordpress,Shortcode,我想知道我是否可以在1中使用两个短代码。我知道你可以替换短代码中的变量,但我要找的是使用一个短代码,比如[apple],它将提供指向我在短代码中给出的链接的链接,但是如果我想在该链接旁边有一个图标,那么可以使用[apple icon],而不是在其左侧提供一个带有图标的链接 add_shortcode('apple', 'apple'); function apple() { return '<a href="http://example.com/apple>Apple</a&g

我想知道我是否可以在1中使用两个短代码。我知道你可以替换短代码中的变量,但我要找的是使用一个短代码,比如[apple],它将提供指向我在短代码中给出的链接的链接,但是如果我想在该链接旁边有一个图标,那么可以使用[apple icon],而不是在其左侧提供一个带有图标的链接

add_shortcode('apple', 'apple');
function apple()
{
return '<a href="http://example.com/apple>Apple</a>';
}
add_shortcode('apple','apple');
函数apple()
{
返回“”;
}

因此,是否可以将图标添加到其中?如果我们将图标添加到短代码中,它也将返回一个我将在短代码中指定的图像。

如果我正确理解您的问题,您不需要在短代码中创建短代码。短代码已经在插件/函数中执行,因此您应该能够直接在短代码处理程序函数中调用函数(或图标函数)。无需再次运行短代码

如果您想调用的不是您自己的快捷码,您可以调用
do\u shortcode


您需要使用
属性

add_shortcode( 'apple', 'apple' );

function apple( $atts, $content = null )
{
    extract( shortcode_atts( array( 'icon' => 'false'), $atts ) );

        if($icon == 'true'){
            $output = '<img src="apple.png" />';
        }

    $output .= '<a href="http://www.example.com">Apple</a>';

    return $output;
}
add_短码('apple','apple');
函数apple($atts,$content=null)
{
提取(短码_atts(数组('icon'=>'false'),$atts));
如果($icon=='true'){
$output='';
}
$output.='';
返回$output;
}
你会这样称呼它

[apple icon=“true”]


这是文档

要想做您想要做的事情,不需要有两个短代码,只需让您的短代码接受参数即可:

add_shortcode('apple', 'apple');

function apple($args) {
    $default = array('icon' => '');
    $args = wp_parse_args($args, $default);

    $content = '';
    if ($args['icon']) {
        $content.= '<img src="icon.png">';
    }

    $content.= '<a href="http://example.com/apple>Apple</a>';

    return $content;
}
如果只是图标的“是/否”

或者,您可以使其根据您设置的
图标
的值动态加载图标,这需要对函数进行一些修改:

function apple($args) {
    $default = array('icon' => '');
    $args = wp_parse_args($args, $default);

    $content = '';
    if ($args['icon']) {
        $content.= '<img src="' . $args['icon'] . '">';
    }

    $content.= '<a href="http://example.com/apple>Apple</a>';

    return $content;
}

(注意,您可能需要传入一个完全限定的域名,例如:
[apple icon=”http://example.com/images/my_icon.png“]

我不想在编辑器外使用短代码,我只想能够将图标添加到短代码[apple icon]但我不确定如何使其工作,因为我看到的所有示例都类似于[apple height=“200px”],用于替换短代码中的变量集,但我不想替换任何内容,我只是不想在短代码中添加“icon”后返回图标图像。谢谢,我想这是我必须要做的。我只是想让它尽可能简单,用“icon”代替“icon=”true”'因为有多个作者,而且他们更容易记住'图标',因为他们对编码一无所知。提取的含义是什么?
extract
获取数组元素并将它们放入变量中{if语句的结尾{我遇到了语法错误,意外'}感谢我所提供的所有帮助“我一直在使用短代码,它工作得很好,尽管我希望再增加一个功能。我该如何做另一个if语句,其中我们要说image=“true”,它只显示图像的内容,而不显示图标或主输出?谢谢,在这种情况下,短代码会是什么样子?是否有可能重复?您为什么不给链接一个类并添加带有css的图标?
function apple($args) {
    $default = array('icon' => '');
    $args = wp_parse_args($args, $default);

    $content = '';
    if ($args['icon']) {
        $content.= '<img src="' . $args['icon'] . '">';
    }

    $content.= '<a href="http://example.com/apple>Apple</a>';

    return $content;
}
[apple icon="my_icon.png"]