wordpress:显示主页上自定义帖子类型的最后一条记录

问题描述:

我是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

请提供更多关于您需要的结果的信息。我不明白你的意思是在你的主页上的表格中显示上次保存的帖子。桌子有什么相关性?此外,请注意,有关*的问题应包括[最小,完整和可验证示例]中的所有相关代码(https://*.com/help/mcve)。具有明确,详细和*相关*信息的问题更有可能得到答案。另请阅读[我如何问一个好问题](https://*.com/help/how-to-ask)。 – FluffyKitten

+0

你好
我添加的代码后的第一次
我有一个插件
在这个插件,当我保存在WordPress的一个新的记录和信息存储以及
现在我要显示在主页上这个纪录作为表格
谢谢 –

+0

感谢您添加代码和附加信息。现在你的问题更像是应该的,我们有能力帮助你!顺便说一下,我编辑了你的问题,删除了一些不需要的额外代码,因为不必要的代码使得它更难处理。 – FluffyKitten

快速谷歌搜索“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 你的帮助非常有帮助。 但我有另一个问题。 如何仅显示最后一条记录? –

相关问题
电话 传真 邮编