我正在制作一个从WordPress媒体库收集数据的短代码函数
function byn_recent_resources_shortcode($atts, $content = null) {
    global $post;
    extract(shortcode_atts(array(
        \'group\'     => \'public\',
        \'num\'     => \'1\',
        \'orderby\' => \'post_date\',
    ), $atts));
  $args = array(
    \'post_status\' => \'inherit\',
    \'posts_per_page\' => $num,
    \'post_type\' => \'attachment\',
  );
  $args[\'tax_query\'] = array(
    array(
      \'taxonomy\' => \'group\',
      \'terms\' => array( $group ),
      \'field\' => \'slug\',
    ),
  );
    $output = \'\';
    $posts = get_posts($args);
    foreach($posts as $post) {
        setup_postdata($post);
        $output .= \'
      <article>
        <div class="thumbnail">
          // IF STATEMENT GOES HERE //
        </div>
      </article>
    \';
    }
    wp_reset_postdata();
    return \'<div class="resource-list" id="resource-list"><div class="post-outer-wrap">\'. $output .\'</div></div>\';
}
add_shortcode(\'byn-recent-resources\', \'byn_recent_resources_shortcode\');
 内部
<div class="thumbnail"> 输出,我计划启用
<if> 像这样的声明
<?php if ( wp_attachment_is_image( $id ) ) { ?>
  // Do option A
<?php } else { ?>
  // Do option B
<?php } ?>
 我该怎么做?
 
                    最合适的回答,由SO网友:Laloptk 整理而成
                    您可以使用缓冲区,而不是将标记存储在$output 变量,然后在包装器中打印该变量,您可以将所有内容存储在缓冲区中,将其全部保存在变量中并返回该变量
function byn_recent_resources_shortcode($atts, $content = null) {
    //...Put the same logic as in your question code
    //...Change, for the code below, what you have, from $posts declaration to the end of the code
    $posts = get_posts($args);
    ob_start();
    ?>
    <div class="resource-list" id="resource-list">
        <div class="post-outer-wrap">
    <?php
    foreach($posts as $post) {
        setup_postdata($post);        
    ?>
            <article>
                <div class="thumbnail">        
                   <?php if ( wp_attachment_is_image( $id ) ) { ?>
                       // Do option A
                   <?php } else { ?>
                      // Do option B
                   <?php } ?>
                </div>
            </article>
    <?php } ?>
        </div>
    </div>
    <?php
    $output = ob_get_contents();
    ob_end_clean();
    
    wp_reset_postdata();
    
    return  $output;
}
add_shortcode(\'byn-recent-resources\', \'byn_recent_resources_shortcode\');
ob_start 将标记保存在一个变量中,包括if语句的乘积,然后返回该字符串。
 
                
                
                SO网友:Antti Koskinen
                如果您不想使用PHP的输出缓冲或drop-out-in和in,那么可以使用一组函数,其中一个包含If语句,并使用它们生成html和短代码输出。
function byn_recent_resources_shortcode($atts, $content = null) {
    $posts_html = array_map(
        \'prefix_single_resource_html\',
        get_posts(
            prefix_prepare_posts_query_args( $atts )
        )
    );
    wp_reset_postdata();
    return prefix_resources_list_html($posts_html);
}
function prefix_prepare_posts_query_args( array $atts ) {
    $args = array();
    
    // prepare $args from $atts here...
    return $args;
}
function prefix_resources_list_html( array $posts_html ) {
    return sprintf(
        \'<div class="resource-list" id="resource-list">
            <div class="post-outer-wrap">%s</div>
        </div>\',
        implode(\'\', $posts_html)
    );
}
function prefix_single_resource_html( $post ) {
    setup_postdata($post);
    $id = 0; // where does the id come from?
    return sprintf(
        \'<article>
            <div class="thumbnail">%s</div>
        </article>\',
        prefix_single_resource_thumbnail_html($id)
    );
}
function prefix_single_resource_thumbnail_html( $id ) {
    if ( wp_attachment_is_image( $id ) ) {
        // returning early makes the explicit else statement unnecessary
        return \'image html\';
    }
    return \'default value\';
}
 这还有一个额外的好处,就是代码被分割成更小的部分,这使得整个代码更容易理解。此外,描述性函数名还兼作代码的文档。