Wordpress自定义帖子类型

时间:2015-03-30 21:48:39

标签: php wordpress wordpress-plugin field custom-post-type

<?php
/*
Plugin Name: Opportunities
Description: Custom Post Types for "The Oppourtunities Blog" website.
Author: Sheju
Author URI: http://www.localhost.com
*/

if( ! function_exists( 'Opportunity_create_post_type' ) ) :
    function Opportunity_create_post_type() {
        $labels = array(
            'name' => __( 'Opportunity' ),
            'singular_name' => __( 'Opportunity' ),
            'add_new' => __( 'Add New Opportunity' ),
            'all_items' => __( 'All Opportunities' ),
            'add_new_item' => __( 'Add New Opportunity' ),
            'edit_item' => __( 'Edit Opportunity' ),
            'new_item' => __( 'New Opportunity' ),
            'view_item' => __( 'View Opportunity' ),
            'search_items' => __( 'Search Opportunities' ),
            'not_found' => __( 'No Opportunities found' ),
            'not_found_in_trash' => __( 'No Opportunities found in trash' ),
            'parent_item_colon' => __( 'Parent Opportunity' )
            //'menu_name' => default to 'name'
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'publicly_queryable' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                //'thumbnail',
                //'author',
                //'trackbacks',
                //'custom-fields',
                //'comments',
                'revisions',
                //'page-attributes', // (menu order, hierarchical must be true to show Parent option)
                //'post-formats',
            ),
            'taxonomies' => array( 'category', 'post_tag' ), // add default post categories and tags
            'menu_position' => 5,
            'register_meta_box_cb' => 'Opportunity_add_post_type_metabox'
        );
        register_post_type( 'Opportunity', $args );
        //flush_rewrite_rules();

        register_taxonomy( 'Opportunity_category', // register custom taxonomy - Opportunity category
            'Opportunity',
            array( 'hierarchical' => true,
                'label' => __( 'Opportunity categories' )
            )
        );
        register_taxonomy( 'Opportunity_tag', // register custom taxonomy - Opportunity tag
            'Opportunity',
            array( 'hierarchical' => false,
                'label' => __( 'Opportunity tags' )
            )
        );
    }
    add_action( 'init', 'Opportunity_create_post_type' );


    function Opportunity_add_post_type_metabox() { // add the meta box
        add_meta_box( 'Opportunity_metabox', 'More Details about Opportunity', 'Opportunity_metabox', 'Opportunity', 'normal' );
    }


    function Opportunity_metabox() {
        global $post;
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="Opportunity_post_noncename" id="Opportunity_post_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

        // Get the data if its already been entered
        $Opportunity_post_name = get_post_meta($post->ID, '_Opportunity_post_name', true);
        $Opportunity_post_desc = get_post_meta($post->ID, '_Opportunity_post_desc', true);
        $Opportunity_post_duration = get_post_meta($post->ID, '_Opportunity_post_duration', true);

        // Echo out the field
        ?>

        <div class="width_full p_box">
            <p>
                <label>Name<br>
                    <input type="text" name="Opportunity_post_name" class="widefat" value="<?php echo $Opportunity_post_name; ?>">
                </label>
            </p>
            <p><label>Description<br>
                    <textarea name="Opportunity_post_desc" class="widefat"><?php echo $Opportunity_post_desc; ?></textarea>
                </label>
            </p>
            <p><label>Duration<br>
                    <textarea name="Opportunity_post_duration" class="widefat"><?php echo $Opportunity_post_duration; ?></textarea>
                </label>
            </p>
        </div>
    <?php
    }


    function Opportunity_post_save_meta( $post_id, $post ) { // save the data
        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if( !wp_verify_nonce( $_POST['Opportunity_post_noncename'], plugin_basename(__FILE__) ) ) {
            return $post->ID;
        }

        // is the user allowed to edit the post or page?
        if( ! current_user_can( 'edit_post', $post->ID )){
            return $post->ID;
        }
        // ok, we're authenticated: we need to find and save the data
        // we'll put it into an array to make it easier to loop though

        $Opportunity_post_meta['_Opportunity_post_name'] = $_POST['Opportunity_post_name'];
        $Opportunity_post_meta['_Opportunity_post_desc'] = $_POST['Opportunity_post_desc'];
        $Opportunity_post_meta['_Opportunity_post_duration'] = $_POST['Opportunity_post_duration'];

        // add values as custom fields
        foreach( $Opportunity_post_meta as $key => $value ) { // cycle through the $Opportunity_post_meta array
            // if( $post->post_type == 'revision' ) return; // don't store custom data twice
            $value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
            if( get_post_meta( $post->ID, $key, FALSE ) ) { // if the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // if the custom field doesn't have a value
                add_post_meta( $post->ID, $key, $value );
            }
            if( !$value ) { // delete if blank
                delete_post_meta( $post->ID, $key );
            }
        }
    }
    add_action( 'save_post', 'Opportunity_post_save_meta', 1, 2 ); // save the custom fields
endif; // end of function_exists()


if( ! function_exists( 'view_Opportunities_posts' ) ) : // output
    function view_Opportunities_posts( $num = 4, $do_shortcode = 1, $strip_shortcodes = 0 ) {

        $args = array(
            'numberposts'     => $num,
            'offset'          => 0,
            //'category'        => ,
            'orderby'         => 'menu_order, post_title', // post_date, rand
            'order'           => 'DESC',
            //'include'         => ,
            //'exclude'         => ,
            //'meta_key'        => ,
            //'meta_value'      => ,
            'post_type'       => 'Opportunity',
            //'post_mime_type'  => ,
            //'post_parent'     => ,
            'post_status'     => 'publish',
            'suppress_filters' => true
        );

        $posts = get_posts( $args );

        $html = '';
        foreach ( $posts as $post ) {
            $meta_name = get_post_meta( $post->ID, '_echo"test"'.'_Opportunity_post_name', true );
            $meta_desc = get_post_meta( $post->ID, '_Opportunity_post_desc', true );
            $meta_duration = get_post_meta( $post->ID, '_Opportunity_post_duration', true );


            /*if (!empty($meta_duration) && !empty($$meta_duration['_Opportunity_post_duration']));
            */

            /*<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); ?>

            <?php if (!empty($my_meta) && !empty($my_meta['image_one'])): ?>
    <div class="archive_images_one"><a href="<? echo $my_meta['image_one']; ?>" title="<? echo $my_meta['image_one_lightbox_title']; ?>"  rel="lightbox"><img border="0"  width="300px" height="200px" src="<? echo $my_meta['image_one']; ?>" alt="Android and iPhone App Development"></a></div>
<?php endif; ?>*/




            //$img = get_the_post_thumbnail( $post->ID, 'medium' );
//          if( empty( $img ) ) {
//              $img = '<img src="'.plugins_url( '/img/default.png', __FILE__ ).'">';
//          }
// 
// 
//          if( has_post_thumbnail( $post->ID ) ) {
//              //$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
//              //$img_url = $image[0];
//              $img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
//              $img_url = $img[0];
// 
//              //the_post_thumbnail( 'thumbnail' ); /* thumbnail, medium, large, full, thumb-100, thumb-200, thumb-400, array(100,100) */
//          }
// 
            $content = $post->post_content;
            if( $do_shortcode == 1 ) {
                $content = do_shortcode( $content );
            }
            if( $strip_shortcodes == 1 ) {
                $content = strip_shortcodes( $content );
            }

            $html .= '
            <div>
                <h3>'.$post->post_title.'</h3>
                <div>'.$content.'</div>
                <div>
                    <p>'.$meta_name.'</p>
                    <p>'.$meta_desc.'</p>
                    <p>'.$meta_duration.'</p>
                </div>

            </div>
            ';
        }
        $html = '<div class="wrapper">'.$html.'</div>';
        return $html;
    }
endif; // end of function_exists()
?>

只有存在值时,我才需要为输出中的元字段添加自定义标签。请帮忙。

我尝试在最后更改p标记(类似这样):

            <h3>'.$post->post_title.'</h3>
            <div>
                <p>Name: '.$meta_name.'</p>
                <p>Description: '.$meta_desc.'</p>
            </div>
            <div>'.$img.'</div>
            <div>'.$content.'</div>

1 个答案:

答案 0 :(得分:0)

不是100%肯定我明白你需要什么,但是如果你只想显示某个元素,如果它有价值你可以做这样的事情:

$html .= '<div>';
$html .= '<h3>' . $post->post_title . '</h3>';
$html .= '<div>' . $content . '</div>';
$html .= '<div>';
if( !empty($meta_name) ) {
     $html .= '<p>' . $meta_name . '</p>';
}
if( !empty($meta_desc) ) {
     $html .= '<p>' . $meta_desc . '</p>';
}
if( !empty($meta_duration) ) {
     $html .= '<p>' . $meta_duration . '</p>';
}
$html .= '</div>';
$html .= '</div>';