Slider from custom post type

时间:2014-09-16 作者:Rohil_PHPBeginner

我正在制作一个自定义滑块,管理员可以从后端添加滑块图像。我是在自定义帖子类型的帮助下完成的。

function.php

// Custom Post types for Feature project on home page 
   add_action(\'init\', \'create_feature\');
     function create_feature() {
       $feature_args = array(
          \'labels\' => array(
           \'name\' => __( \'Feature Project\' ),
           \'singular_name\' => __( \'Feature Project\' ),
           \'add_new\' => __( \'Add New Feature Project\' ),
           \'add_new_item\' => __( \'Add New Feature Project\' ),
           \'edit_item\' => __( \'Edit Feature Project\' ),
           \'new_item\' => __( \'Add New Feature Project\' ),
           \'view_item\' => __( \'View Feature Project\' ),
           \'search_items\' => __( \'Search Feature Project\' ),
           \'not_found\' => __( \'No feature project found\' ),
           \'not_found_in_trash\' => __( \'No feature project found in trash\' )
         ),
       \'public\' => true,
       \'show_ui\' => true,
       \'capability_type\' => \'post\',
       \'hierarchical\' => false,
       \'rewrite\' => true,
       \'menu_position\' => 20,
       \'supports\' => array(\'title\', \'editor\', \'thumbnail\')
     );
  register_post_type(\'feature\',$feature_args);
}
add_filter("manage_feature_edit_columns", "feature_edit_columns");

function feature_edit_columns($feature_columns){
   $feature_columns = array(
      "cb" => "<input type=\\"checkbox\\" />",
      "id" => "ID",
      "title" => "Title",
   );
  return $feature_columns;
}



add_action( \'add_meta_boxes\', \'cd_meta_box_add\' );
function cd_meta_box_add()
{
    add_meta_box( \'my-meta-box-id\', \'Link to Project\', \'cd_meta_box_cb\', \'feature\', \'normal\', \'high\' );
}

function cd_meta_box_cb( $post )
{
    $url = get_post_meta($post->ID, \'url\', true);
    wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' ); ?>

    <p>
        <label for="url">Project url</label>
        <input type="text" name="url" id="url" value="<?php echo $url; ?>" style="width:350px" />
    </p>

    <?php   
}

add_action( \'save_post\', \'cd_meta_box_save\' );
function cd_meta_box_save( $post_id )
{
    // Bail if we\'re doing an auto save
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn\'t there, or we can\'t verify it, bail
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;

    // if our current user can\'t edit this post, bail
    if( !current_user_can( \'edit_post\' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        \'a\' => array( // on allow a tags
            \'href\' => array() // and those anchors can only have href attribute
        )
    );

    // Probably a good idea to make sure your data is set
    if( isset( $_POST[\'url\'] ) )
        update_post_meta( $post_id, \'url\', wp_kses( $_POST[\'url\'], $allowed ) );
}
在上面的代码之后,我得到如下输出enter image description here

给,我想要ID 之后Title. 这样管理员就可以从中设置幻灯片图像顺序ID.

谁能告诉我怎么做?

2 个回复
最合适的回答,由SO网友:thedigitalmonk 整理而成

如果ID的目的只是设置幻灯片的顺序,那么最好使用order属性。

添加page-attributes 到的阵列supports 中的参数$feature_args然后在循环中可以指定&orderby=menu_order&order=ASC

然后您可以将其显示为一列,您可以参考此answer

SO网友:Robert hue

Hre是用于在feature post类型中添加ID列的代码。

add_filter( \'manage_edit-feature_columns\', \'set_custom_edit_feature_columns\' );
add_action( \'manage_feature_posts_custom_column\' , \'custom_feature_column\', 10, 2 );

function set_custom_edit_feature_columns($columns) {
    unset( $columns[\'date\'] );
    $columns[\'ID\'] = __( \'ID\', \'twentyfourteen\' );
    return $columns;
}

function custom_feature_column( $column, $post_id ) {
    switch ( $column ) {
        case \'ID\' :
        echo $post_id;
        break;
    }
}

结束

相关推荐

Sidebar slideshow widget

有人知道可以放在边栏上的高质量图像幻灯片小部件吗?