2017-04-24 55 views
0

我在WordPress中創建了一個自定義帖子類型。我想在短代碼中使用帖子ID,以便我可以將特定帖子拉出來輸出數據,這是使用高級自定義字段中的字段的圖庫。如何使用帖子ID作爲短代碼屬性?

這裏是我的功能至今:

function mbgc_gallery_shortcode($atts) { 

    // $post_id = $atts['id']; 

    extract(shortcode_atts(
     array(
      'id' => '', 
     ), $atts) 
    ); 

    $html_out = '123'; 

    // $post = get_post($id); 

    // if ($post) : 

     if(have_rows('mbgc_gallery')): 

      $html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">'; 

      while(have_rows('mbgc_gallery')): the_row(); 

       // vars 
       $image = get_sub_field('mbgc_image'); 
       $caption = get_sub_field('mbgc_caption'); 
       $title = get_sub_field('mbgc_title'); 
       $sub_title = get_sub_field('mbgc_sub_title'); 

       if ($image) :   
        $html_out .= '<div class="mbgc-gallery-item">'; 

         if ($caption) : 
          $html_out .= '<div class="mbgc-slide-caption">'; 
           $html_out .= '<h4>' . $caption . '</h4>'; 
           $html_out .= '<div class="mbgc-slide-titles">'; 
            $html_out .= '<h6>' . $title . '</h6>'; 
            $html_out .= '<h6>' . $sub_title . '</h6>'; 
           $html_out .= '</div>'; 
          $html_out .= '</div>'; 
         endif; 

         $html_out .= '<div class="mbgc-slide">'; 
          $html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />'; 
         $html_out .= '</div>'; 

        $html_out .= '</div>'; 
       endif; 

      endwhile; 

      $html_out .= '</div>'; 

     endif; 

    // endif; 

    return $html_out; 

} 
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode'); 

我希望簡碼可以像[mbgc_gallery ID = 「401」],作爲一個例子。最主要的是我不知道如何在函數中正確寫入以拉取ID。

+0

你的功能看起來不錯(拉出ID),究竟是不是工作? –

+0

前端沒有輸出內容。在一個隨機頁面上,我把我在原始文章中提到的那個短代碼添加進去,我只看到'123',但沒有別的。即使'var_dump'也不能爲我的變量返回任何東西。 –

回答

1

您需要將短代碼屬性中的ID傳遞給ACF have_rows()函數,以便檢索正確的數據。

<?php 
function mbgc_gallery_shortcode($atts) { 

    $attributes = shortcode_atts(
     array(
      'id' => null 
     ), $atts); 

     // $attributes['id'] is now the passed-in ID 

     $html_out = ''; 

     if(have_rows('mbgc_gallery', $attributes['id'])): 

      $html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">'; 

      while(have_rows('mbgc_gallery', $attributes['id'])): the_row(); 

      // vars 
      $image = get_sub_field('mbgc_image'); 
      $caption = get_sub_field('mbgc_caption'); 
      $title = get_sub_field('mbgc_title'); 
      $sub_title = get_sub_field('mbgc_sub_title'); 

      if ($image) : 
       $html_out .= '<div class="mbgc-gallery-item">'; 

       if ($caption) : 
        $html_out .= '<div class="mbgc-slide-caption">'; 
        $html_out .= '<h4>' . $caption . '</h4>'; 
        $html_out .= '<div class="mbgc-slide-titles">'; 
        $html_out .= '<h6>' . $title . '</h6>'; 
        $html_out .= '<h6>' . $sub_title . '</h6>'; 
        $html_out .= '</div>'; 
        $html_out .= '</div>'; 
       endif; 

       $html_out .= '<div class="mbgc-slide">'; 
       $html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />'; 
       $html_out .= '</div>'; 

       $html_out .= '</div>'; 
      endif; 

     endwhile; 

     $html_out .= '</div>'; 

    endif; 

    return $html_out; 

} 
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode'); 

使用這樣的短代碼:[mbgc_gallery ID = 「123」]。

就我個人而言,我不喜歡使用extract() - 這將數組轉換爲單個變量,根據數組元素鍵命名,並且可能會使調試變得困難。相反,只需像這樣訪問您的ID:$attributes['id']如上所示。

祝你好運!

+0

這工作!謝謝! –