我是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);
請提供更多關於您需要的結果的信息。我不明白你的意思是在你的主頁上的表格中顯示上次保存的帖子。桌子有什麼相關性?此外,請注意,有關StackOverflow的問題應包括[最小,完整和可驗證示例]中的所有相關代碼(https://stackoverflow.com/help/mcve)。具有明確,詳細和*相關*信息的問題更有可能得到答案。另請閱讀[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask)。 – FluffyKitten
你好
我添加的代碼後的第一次
我有一個插件
在這個插件,當我保存在WordPress的一個新的記錄和信息存儲以及
現在我要顯示在主頁上這個紀錄作爲表格
謝謝 –
感謝您添加代碼和附加信息。現在你的問題更像是應該的,我們有能力幫助你!順便說一下,我編輯了你的問題,刪除了一些不需要的額外代碼,因爲不必要的代碼使得它更難處理。 – FluffyKitten