2017-08-16 32 views
0

我是WordPress的新手開發者。
我創建了一個自定義的帖子類型wp_locations來練習。
wordpress:顯示主頁上自定義帖子類型的最後一條記錄

它有三個數字字段,輸入爲<input type="number">:phone_number;一個fax_number和ZIP_CODE
$wp_location_phone = get_post_meta($post->ID,'wp_location_phone',true); $wp_location_fax = get_post_meta($post->ID,'wp_location_fax',true); $wp_location_zipcode = get_post_meta($post->ID,'wp_location_zipcode',true);

可以正常工作和存儲的信息。

我有一個插件來創建自定義帖子類型並在WordPress中保存新記錄。
現在我想在表格的主頁上顯示這條記錄。

我搜索了但我無法運行任何建議。請用一個例子來指導我。由於

class wp_simple_location{ 

    private $wp_location_trading_hour_days = array(); 

    public function __construct(){ 
     add_action('init', array($this,'set_location_trading_hour_days')); //sets the default trading hour days (used by the content type) 
     add_action('init', array($this,'register_location_content_type')); 
     add_action('add_meta_boxes', array($this,'add_location_meta_boxes')); 
     add_action('save_post_wp_locations', array($this,'save_location')); //save location  
     register_activation_hook(__FILE__, array($this,'plugin_activate')); 
     register_deactivation_hook(__FILE__, array($this,'plugin_deactivate')); 
    } 

    //display function used for our custom location meta box*/ 
    public function location_meta_box_display($post){ 
     wp_nonce_field('wp_location_nonce', 'wp_location_nonce_field'); 

     //collect variables 
     $wp_location_phone = get_post_meta($post->ID,'wp_location_phone',true); 
     $wp_location_fax = get_post_meta($post->ID,'wp_location_fax',true); 
     $wp_location_zipcode = get_post_meta($post->ID,'wp_location_zipcode',true); 
/* 
    // information is gathered here in a form. The relevant inputs are: 
    <input type="number" name="wp_location_phone" id="wp_location_phone" value=" <?php echo $wp_location_phone;"?> /> 
    <input type="number" name="wp_location_fax" id="wp_location_fax" value=" <?php echo $wp_location_fax; ?>"/> 
    <input type="number" name="wp_location_zipcode" id="wp_location_zipcode" value=" <?php echo $wp_location_zipcode;" ?> /> 
    */ 
    } 

    //triggered when adding or editing a location 
    public function save_location($post_id){ 
     // nonce & autosave checks removed from example as they are irrelevant 

     //get our phone, email and address fields 
     $wp_location_phone = isset($_POST['wp_location_phone']) ? sanitize_text_field($_POST['wp_location_phone']) : ''; 
     $wp_location_fax = isset($_POST['wp_location_fax']) ? sanitize_text_field($_POST['wp_location_fax']) : ''; 
     $wp_location_zipcode = isset($_POST['wp_location_zipcode']) ? sanitize_text_field($_POST['wp_location_zipcode']) : ''; 

     //update phone, email and address fields 
     update_post_meta($post_id, 'wp_location_phone', $wp_location_phone); 
     update_post_meta($post_id, 'wp_location_fax', $wp_location_fax); 
     update_post_meta($post_id, 'wp_location_zipcode', $wp_location_zipcode); 

     //search for our trading hour data and update 
     foreach($_POST as $key => $value){ 
      //if we found our trading hour data, update it 
      if(preg_match('/^wp_location_trading_hours_/', $key)){ 
       update_post_meta($post_id, $key, $value); 
      } 
     } 
     do_action('wp_location_admin_save',$post_id); 
    } 

} // end class 

其他說明:
客戶交的類型名稱爲「wp_location」,但蛞蝓是「位置」:

$args = array(
    [...] 
    'rewrite' => array('slug' => 'locations', 'with_front' => 'true') 
); 
register_post_type('wp_locations', $args); 
+0

請提供更多關於您需要的結果的信息。我不明白你的意思是在你的主頁上的表格中顯示上次保存的帖子。桌子有什麼相關性?此外,請注意,有關StackOverflow的問題應包括[最小,完整和可驗證示例]中的所有相關代碼(https://stackoverflow.com/help/mcve)。具有明確,詳細和*相關*信息的問題更有可能得到答案。另請閱讀[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask)。 – FluffyKitten

+0

你好
我添加的代碼後的第一次
我有一個插件
在這個插件,當我保存在WordPress的一個新的記錄和信息存儲以及
現在我要顯示在主頁上這個紀錄作爲表格
謝謝 –

+0

感謝您添加代碼和附加信息。現在你的問題更像是應該的,我們有能力幫助你!順便說一下,我編輯了你的問題,刪除了一些不需要的額外代碼,因爲不必要的代碼使得它更難處理。 – FluffyKitten

回答

0

快速谷歌搜索「wp_simple_location」表明,你已經採取這個例子從SitePoint並改變它。

基於此,我將修改該示例的其餘部分,以向您展示如何創建可以使用的簡碼。

假設:

  • 我相信你的帖子的代碼是不是你從你的試驗場,因爲它是 無效(如你缺少很多)
  • 我也要去假設你只有1個位置 - sitepoint例如允許您添加很多,但你似乎已經刪除 代碼

代碼:

1.重新添加get_locations_output函數。

您刪除了get_locations_output函數,這就是您實際顯示信息的方式 - 這就是您想要的!所以你需要把它添加回你的wp_simple_location類。我已經改變了它,所以它應該更改顯示位置

//主要功能的工作(使用我們的簡碼和widget)

public function get_locations_output(){ 
    //find locations 
    $location_args = array(
     'post_type'  => 'wp_locations', 
     'posts_per_page'=> 999, 
     'post_status' => 'publish' 
    ); 

    //output 
    $html = ''; 

    $locations = get_posts($location_args); 
    //if we have a location it will be returned in an array, so loop through it 
    if($locations){ 
     $html .= '<article class="location_list cf">'; 
     //foreach location 
     foreach($locations as $location){ 
      $html .= '<section class="location">'; 
       //collect location data 
       $wp_location_id = $location->ID; 
       $wp_location_phone = get_post_meta($wp_location_id,'wp_location_phone',true); 
       $wp_location_fax= get_post_meta($wp_location_id,'wp_location_fax',true); 
       $wp_location_zipcode= get_post_meta($wp_location_id,'wp_location_zipcode',true); 


       // output details only if any were entered 
       if($wp_location_phone || $wp_location_fax || $wp_location_zipcode){ 
        $html .= '<table>'; 
        if(!empty($wp_location_phone)){ 
         $html .= '<tr><td>Phone: </td><td>' . $wp_location_phone . '</td></tr>'; 
        } 
        if(!empty($wp_location_fax)){ 
         $html .= '<tr><td>Fax: </td><td>' . $wp_location_fax. '</td></tr>'; 
        } 
        if(!empty($wp_location_zipcode)){ 
         $html .= '<tr><td>Zipcode: </td><td>' . $wp_location_zipcode. '</td></tr>'; 
        } 
        $html .= '</table>'; 
       } 

       //apply the filter after the main content, before it ends 
       //(lets third parties hook into the HTML output to output data) 
       $html = apply_filters('wp_location_after_main_content', $html); 

      $html .= '</section>'; 
     } 
     $html .= '</article>'; 
    }  
    return $html; 
} // end function 

** 2。創建一個新的類,添加一個短代碼,讓您在頁面中包含信息**

注意:正確的做法是在單獨的php文件中創建新的新類,並將其包含在您的wp_simple_location類文件中,但要保留此示例很簡單,將其添加到包含wp_simple_location類的文件的底部:

class wp_location_shortcode{ 

    //on initialize 
    public function __construct(){ 
     add_action('init', array($this,'register_location_shortcodes')); //shortcodes 
    } 
    //location shortcode 
    public function register_location_shortcodes(){ 
     add_shortcode('wp_locations', array($this,'location_shortcode_output')); 
    } 

    //shortcode display 
    public function location_shortcode_output($atts, $content = '', $tag){ 

     //get the global wp_simple_locations class 
     global $wp_simple_locations; 

     //uses the main output function of the location class 
     $html = $wp_simple_locations->get_locations_output(); 

     return $html; 
    } 
} 

$wp_location_shortcode = new wp_location_shortcode; 

** 3。使用簡碼包括信息**

要在任何頁面的信息,你只需要使用下面的簡碼在帖子編輯:

[wp_locations]

注:

您還應該清理您的插件代碼 - 例如從開始的代碼//搜索我們的交易小時數據,並且不需要更新,因爲您從插件中刪除了小時。還要確保你的所有php標籤都是正確的!

正如我剛纔提到的,我剛剛更改了SitePoint示例以適應您的更改,所以我還沒有測試過這些。如果您有任何問題,重新閱讀SitePoint教程可能是一個好主意,因爲它詳細解釋了代碼。

+0

感謝您的指導 使用以下代碼,定位器中的記錄顯示在主頁上 請指導我添加兩件東西 1-在主頁上,僅顯示最後一條記錄 2-如何創建如下面的代碼表

​​wp_location_phone ​​wp_location_fax ​​wp_location_zip
電話 傳真 郵編
再次感謝 –

+0

@FardadFarhang 1 - 使用簡碼在我的答案。 2 - 只需添加標題行第一個'$的HTML = '

';',然後第二行只值。 – FluffyKitten

+0

謝謝親愛的FluffyKitten 你的幫助非常有幫助。 但我有另一個問題。 如何僅顯示最後一條記錄? –

相關問題
電話傳真郵編