首頁>Web>source

今天,如果仅傳遞一个值,則下面建立的簡碼可以正常工作,但是,現在,我正在尝試使用此簡碼,以便该屬性可以接收多个值.這个想法是让短代碼看起来像這樣: [youlike id_post = '6059, 76912'] .我試圖在帖子中像這樣添加它,但是没有用。

在我拥有的功能之下

function youCanLike( $atts ) {
    ob_start();
    
    $value = shortcode_atts( array(
        'id_post' => '',
    ), $atts );
    // Check if href has a value before we continue to eliminate bugs
    if ( !$value ['id_post'] )
        return false;
    $args = array(
        'post__in' => array( esc_attr( $value['id_post'] ) ),
        'orderby' => 'date',
        'order' => 'DESC'
    );
    get_template_part( 'global-templates/blocks/block', 'you-can-like', $args );
    return ob_get_clean();   
} 
add_shortcode( 'youlike', 'youCanLike' );

這是get_template_part檔案的呼叫位置.即 block-you-can-like.php

<div class="content-box--primary">
    <h2 class="content-box--primary__head">You may also be interested:</h2>
    
        <?php
        
        $args = wp_parse_args( $args );
        $query = new WP_Query( $args );
        if( $query->have_posts() ):
            while( $query->have_posts() ): $query->the_post();
        ?>
    <article class="content-box--primary__content">
        <a href="<?php the_permalink(); ?>" class="content-box--primary__link">
            <h3 class="content-box--primary__title"><?php the_title(); ?></h3>
            <span class="content-box--primary__more">Read More</span>   
        </a>
    </article>
    <?php
        endwhile;
        wp_reset_postdata();
        endif;                                  
    ?>
</div>
最新回復
  • 6月前
    1 #

    post__in 接受一組帖子ID.您要傳遞的是將字元串放入陣列

    尝試稍微更改代碼,例如

      // Check if href has a value before we continue to eliminate bugs
        if ( !$value ['id_post'] )
            return false;
    
        $post_ids_array = explode(',', $value['id_post']);
        $args = array(
            'post__in' => $post_ids_array,
            'orderby' => 'date',
            'order' => 'DESC'
        );
    

  • wordpress:HTML註釋在functionsphp指令碼根目錄中引起問题
  • drupal:匯入單个配置,产生錯誤-無法匯入配置,因為配置由於以下原因而失败