Shortcode insertion in tab

时间:2018-01-31 作者:Artus

我创建了一个插件,该插件使用来自帖子类型的自定义类别的短代码显示员工。例如[董事会成员术语=“team1”]它工作正常,但只在页面或帖子上。当我将短代码粘贴到选项卡中(使用JustinTadlock的whistles)时,配置文件显示在选项卡外部,页面主体中选项卡的上方。我该怎么做才能让它显示在选项卡中?这是代码。。。

require_once(dirname(__FILE__).\'/post-type.php\'); //file that registers CPT

function wporg_shortcodes_init(){
function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        \'term\' => \'columns\'
    ), $atts ) );

    $custom_taxonomy = $atts[\'term\'];       

    // add query of custom post type board_members      
            $recentPosts = new WP_Query
            (array(\'posts_per_page\' => -1, \'post_type\' => array(\'board_members\'), \'columns\' => $custom_taxonomy ));              
            while( $recentPosts->have_posts() ) :  $recentPosts->the_post();  ?>

                    <div class="b-thumb"><?php the_post_thumbnail(\'thumbnail\') ?></div>
                    <p class="b-info">
                    <span class="b-name"><?php the_title(); ?></span><br />                                       
                    <span class="b-country"><?php the_field(\'country\'); ?></span><br />
                    <span class="b-position"><?php the_field(\'position\'); ?></span><br />
                    <span class="b-content"><?php printf(get_the_content()); ?></span><br />
                </p>                

                <?php wp_reset_postdata(); ?>         
        <?php endwhile; ?>      
   <?php        

    }
   add_shortcode(\'board-members\', \'wporg_shortcode\');
  }
 add_action(\'init\', \'wporg_shortcodes_init\');   
谢谢

2 个回复
最合适的回答,由SO网友:admcfajn 整理而成

听起来你需要将你的短代码内容包装在ob_start()return ob_get_clean()

require_once(dirname(__FILE__).\'/post-type.php\'); //file that registers CPT

function wporg_shortcodes_init(){
function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        \'term\' => \'columns\'
    ), $atts ) );

    $custom_taxonomy = $atts[\'term\'];       

    ob_start(); // <- works like magic

    // add query of custom post type board_members      
            $recentPosts = new WP_Query
            (array(\'posts_per_page\' => -1, \'post_type\' => array(\'board_members\'), \'columns\' => $custom_taxonomy ));              
            while( $recentPosts->have_posts() ) :  $recentPosts->the_post();  ?>

                    <div class="b-thumb"><?php the_post_thumbnail(\'thumbnail\') ?></div>
                    <p class="b-info">
                    <span class="b-name"><?php the_title(); ?></span><br />                                       
                    <span class="b-country"><?php the_field(\'country\'); ?></span><br />
                    <span class="b-position"><?php the_field(\'position\'); ?></span><br />
                    <span class="b-content"><?php printf(get_the_content()); ?></span><br />
                </p>                

                <?php wp_reset_postdata(); ?>         
        <?php endwhile; ?>      
   <?php        

   return ob_get_clean(); // <- more magic

    }
   add_shortcode(\'board-members\', \'wporg_shortcode\');
  }
 add_action(\'init\', \'wporg_shortcodes_init\');  
您还可以修改快捷码以返回单个字符串,以下操作也可以:

function wporg_shortcodes_init(){
  function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        \'term\' => \'columns\'
    ), $atts ) );

    $custom_taxonomy = $atts[\'term\'];       

    $output = \'\';

    // add query of custom post type board_members      
    $recentPosts = new WP_Query
    (array(\'posts_per_page\' => -1, \'post_type\' => array(\'board_members\'), \'columns\' => $custom_taxonomy ));              
    while( $recentPosts->have_posts() ) :  $recentPosts->the_post();

        $output .= \'<div class="b-thumb">\'.the_post_thumbnail(\'thumbnail\').\'</div>\';
        $output .= \'<p class="b-info">\';
          $output .= \'<span class="b-name">\'.the_title().\'</span><br />\';
          $output .= \'<span class="b-country">\'.the_field(\'country\').\'</span><br />\';
          $output .= \'<span class="b-position">\'.the_field(\'position\').\'</span><br />\';
          $output .= \'<span class="b-content">\'.printf(get_the_content()).\'</span><br />\';
        $output .= \'</p>                \';

    endwhile;

    wp_reset_postdata();

    return $output;

  }
 add_shortcode(\'board-members\', \'wporg_shortcode\');
}
add_action(\'init\', \'wporg_shortcodes_init\');  

SO网友:Rick Hellewell

如果希望内容显示在特定位置,则需要将内容包装在<div> 使用“类”,其中“类”具有显示内容所需的CSS。

短代码适用于页面/帖子,因为短代码包含在页面/帖子的内容中<div> 这围绕着帖子内容。

您需要创建一个类(称为“myshortcode”),并在循环中使用该类:

<div class=\'myshortcode\'>
 <div class="b-thumb"><?php the_post_thumbnail(\'thumbnail\') ?></div>
                    <p class="b-info">
                    <span class="b-name"><?php the_title(); ?></span><br />                                       
                    <span class="b-country"><?php the_field(\'country\'); ?></span><br />
                    <span class="b-position"><?php the_field(\'position\'); ?></span><br />
                    <span class="b-content"><?php printf(get_the_content()); ?></span><br />
</div>
然后,新类的一些CSS:

.myshortcode {
   /* some CSS here */
}

结束