Fardad Farhang
Fardad Farhang

Reputation: 3

wordpress: Display last record of Custom post type on home page

I am a novice developer of WordPress.
I created a custom post type wp_locations to practice.

It has three numeric fields that are entered as <input type="number">: a phone_number; a fax_number and a 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);

That works properly and stores the information.

I have a plugin to create the Custom Post Type and save a new record in WordPress.
Now I want to display this record on the home page in a table.

I searched but I could not run any suggestions. Please guide me with an example. Thanks

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

Other notes:
The customer post type name is "wp_location" but the slug is "locations":

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

Upvotes: 0

Views: 459

Answers (1)

FluffyKitten
FluffyKitten

Reputation: 14312

A quick Google search for "wp_simple_location" shows that you have taken this example from SitePoint and changed it.

Based on that, I'm going to amend the rest of that example to show you how to create a shortcode you can use.

Assumptions:

  • I presume the code you posts isn't your from your test site, as it is not valid (e.g. you are missing many )
  • I'm also going to assume that you only have 1 location - the sitepoint example allowed you add many but you appear to have removed that code

Code:

1. Add back in the get_locations_output function.

You deleted the get_locations_output function, which is how you actually display the information - which is what you want! So you need to add it back into your wp_simple_location class. I have changed it so it should work with your changes

// main function for displaying locations (used for our shortcodes and widgets)

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. Create a new class that adds a shortcode to let you include the information in pages **

Note: Proper practice is create the new new class in a separate php file and include it in your wp_simple_location class file, but to keep this example simple, add this to the bottom of the file containing your wp_simple_location class:

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. Use the shortcode to include the information **

To include the information in any page, you just need to use the following shortcode in the post editor:

[wp_locations]

Note:

You should also clean up your plugin code - for example the code starting //search for our trading hour data and update is not needed as you deleted the hours from the plugin. Also make sure all your php tags are correct!

As I mentioned, I have just changed the SitePoint example to work with your changes, so I haven't tested any of this. If you have any questions it might be a good idea to re-read the SitePoint tutorial because it explains the code in detail.

Upvotes: 0

Related Questions