我一整天都在寻找一种方法来为短代码添加属性。我要添加的属性是自定义wp_table
中的ID。
我想要输出的内容是,例如:[launch_tincan id="1"]
在我的自定义wp_table
中收到第1行的信息时,我可以使用这些信息填充我的变种。
我目前有一个功能,如果我从get_results
的行中对id=#
进行硬编码,我的短代码会正确填充,但是,我希望短代码包含id。
我见过很多使用wp_posts等的例子,但没有一个适合我。
我注意到的另一件事是,似乎没有人从函数中的函数生成短代码。这可能吗?
以下是我的代码示例:
<?php
// WP Shortcode to display info from content in my table
function my_shortcode_functions(){
global $wpdb;
$getinfo = $wpdb->get_results($wpdb->prepare("SELECT id,col1,col2,col3,col4 from my_table",$id));
foreach ($getinfo as $s ){
$col1=$s->col1;
$col2=$s->col2;
$col3=$s->col3;
$col4=$s->col4;
}
if ( is_user_logged_in() ) {
?>
<div>
<form method="post">
<input type="submit" onClick="window.open(document.getElementById('results').innerHTML);" value="Open" class='button'/>
</form>
</div>
<div style="visibility:hidden; height:0px; width:0px;" id="results">
<?php
echo $col1 . $col2 . $col3 . $col4;
?>
</div>
<?php
} else {
echo "You must be logged in to use this";
}
}
add_shortcode('my_shortcode', 'my_shortcode_functions');
?>
是否可以将add_shortcode
功能嵌套在my_shortcode_functions
中?
我的get_shortcode_id
函数看起来应该有效吗?
我期待任何帮助纠正我的代码或让我知道我是否在错误的轨道。