如何在短代码中传递ID - WordPress

时间:2016-04-22 10:39:47

标签: php wordpress wordpress-theming shortcode

我需要修改一个主题的短代码,以便我可以将类ID传递给列。我需要能够输入额外的参数id="",然后输入任何ID。

这些是短代码:

if (!function_exists('two_col_50_50_col1')) {
    function two_col_50_50_col1($atts, $content = null) {
        return '<div class="two_columns_50_50 clearfix"><div class="column1"><div class="column_inner">' . do_shortcode($content)  .   '</div></div>';
    }
}
add_shortcode('two_col_50_50_col1', 'two_col_50_50_col1');

if (!function_exists('two_col_50_50_col2')) {
    function two_col_50_50_col2($atts, $content = null) {
        return '<div class="column2"><div class="column_inner">' . do_shortcode($content) . '</div></div></div>';
    }
}
add_shortcode('two_col_50_50_col2', 'two_col_50_50_col2');

1 个答案:

答案 0 :(得分:0)

您可以将参数传递给这样的短代码:

// [bartag foo="foo-value"]
function bartag_func( $atts ) {
$a = shortcode_atts( array(
    'foo' => 'something',
    'bar' => 'something else',
), $atts );

return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );

所以对于你的代码来说就像是:

if (!function_exists('two_col_50_50_col1')) {
 function two_col_50_50_col1($atts, $content = null) {
 $a = shortcode_atts( array(
    'id' => ''
), $atts );
 return '<div class="two_columns_50_50 clearfix '.$a['id'].'"><div        
 class="column1"><div class="column_inner">' . do_shortcode($content)  .   '</div></div>';
 }
  }
 add_shortcode('two_col_50_50_col1', 'two_col_50_50_col1');

 if (!function_exists('two_col_50_50_col2')) {
 function two_col_50_50_col2($atts, $content = null) {
  $a = shortcode_atts( array(
    'id' => ''
), $atts );
  return '<div class="column2 '.$a['id'].'"><div class="column_inner">' .      do_shortcode($content) . '</div></div></div>';
}
}
add_shortcode('two_col_50_50_col2', 'two_col_50_50_col2');

您可以拨打短信代码:

[two_col_50_50_col2 id="something"]

有关wordpress短代码的详细信息,请阅读以下文档: https://codex.wordpress.org/Shortcode_API