Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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 如何在WordPress中制作短代码?_Php_Wordpress - Fatal编程技术网

Php 如何在WordPress中制作短代码?

Php 如何在WordPress中制作短代码?,php,wordpress,Php,Wordpress,我在functions.php中添加了一个函数,如何将该函数作为短代码 function the_top_qa_users(){ global $wpdb; echo "<h3>Popular Users</h3>"; $results = $wpdb->get_results( "SELECT * FROM $wpdb->usermeta WHERE meta_key='_qa_rep' AND meta_value > 0 O

我在functions.php中添加了一个函数,如何将该函数作为短代码

function the_top_qa_users(){
  global $wpdb;
      echo "<h3>Popular Users</h3>";

  $results = $wpdb->get_results( "SELECT * FROM $wpdb->usermeta WHERE meta_key='_qa_rep' AND meta_value > 0  ORDER BY CONVERT(meta_value, SIGNED) DESC LIMIT 0 , 5");

      foreach ( $results as $result ) {    
        the_qa_user_link( $result->user_id );           
        echo get_avatar($result->user_id);    
      }
}
为顶级用户提供功能(){
全球$wpdb;
呼应“大众用户”;
$results=$wpdb->get_results(“从$wpdb->usermeta中选择*,其中meta_key=''和meta_value>0按转换顺序(meta_value,签名)描述限制0,5”);
foreach($results as$result){
_qa_user_链接($result->user_id);
echo获取化身($result->user\u id);
}
}
在我的page.php中,我调用了like
如何为它编写一个简短的代码?

尝试如下:

function the_top_qa_users(){
    return "foo and bar";
}
add_shortcode( 'foobar', 'the_top_qa_users' );

访问它只需在编辑器中键入
[foobar]
,WordPress将根据需要动态替换文本。

可能会有所帮助。如果(是页面('Home'){the_top_qa_users(),添加
可能是个好主意
在函数内部设置块。这取决于条件/要求。@AmalMurali是否会使网站变慢?@Naveen这取决于您的查询时间。以及您将在页面上调用它的次数。我现在使用了amals方式,但有人告诉我这将使网站变慢。我只在主页上调用此函数,用户图像和链接将更改很抱歉,这会减慢网站的速度吗?