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

时间:2017-08-16 19:30:22

标签: wordpress custom-post-type

我是WordPress的新手开发者 我创建了一个自定义的帖子类型wp_locations来练习。

它有三个输入为<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”,但slug为“locations”:

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

1 个答案:

答案 0 :(得分:0)

快速Google搜索&#34; wp_simple_location&#34;表明您已从SitePoint获取此示例并进行了更改。

基于此,我将修改该示例的其余部分,向您展示如何创建可以使用的短代码。

<强>假设:

  • 我认为您发布的代码并非来自您的测试网站 无效(例如,您遗失了很多)
  • 我也会假设你只有一个位置 - 那个 sitepoint示例允许您添加许多但您似乎已删除 那段代码

<强>代码:

<强> 1。在get_locations_output函数中添加。

您删除了get_locations_output函数,这就是您实际显示信息的方式 - 这就是您想要的!所以你需要将它添加回你的wp_simple_location类。我已经更改了它,因此它应该适用于您的更改

//显示位置的主要功能(用于我们的短代码和小部件)

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教程可能是一个好主意,因为它详细解释了代码。