如何在WordPress中制作短代码



我在函数中添加了一个函数.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);    
      }
}

在我的页面中.php我像<?php if(is_page('Home')) { the_top_qa_users();} ?>我如何为此制作一个简短的代码?

尝试如下:

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

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

最新更新