Wordpress短代码名称作为属性

问题描述 投票:0回答:2

我想知道是否可以设置简码并让简码的名称也可以作为属性。我目前的安装方式是这样的

add_shortcode('tooltip', 'tooltip');
function tooltip( $atts $content = null) {
array(
    'type'      => '',
);

因此,当wordpress中的某人使用您键入的简码时

[tooltip type="fruit"]Item Name[/tooltip]

尽管我在想,是否可以仅使用简码的名称作为att,以便我可以将其简短一点并使其看起来像这样

[tooltip="fruit"]Item Name[/tooltip]

几乎切出了type属性,而是使用简码工具提示的名称作为属性。

wordpress shortcode
2个回答
1
投票

不,您的建议是不可能的。它可能会更短一些,但是我认为这会令人困惑,因此我认为这是您自己构建功能所无法实现的。


0
投票

[将短码标签用作属性时,您必须使用$atts数组中的第一项,($atts[0])。

工作示例:

<?php

add_shortcode('tooltip', 'tooltip');

function tooltip(Array $atts = array(), $content = null, $tag = null) {

  $args = shortcode_atts(array( 0 => null ), $atts);
  $args['type'] = trim($args[0], '"=');
  unset($args[0]);

  extract($args);

  // Your code starts here ...
  $output = array(
    '$type'    => $type,
    '$content' => $content
  );

  $output = '<pre>' . print_r($output, true) . '</pre>';

  return $output;
}

请在// Your code starts here ...之后替换所有内容

执行示例:

[tooltip="fruit"]Item Name[/tooltip]

将返回:

<pre>Array
(
    [$type] => fruit
    [$content] => Item Name
)
</pre>
© www.soinside.com 2019 - 2024. All rights reserved.