在WordPress网格视图中显示自定义帖子类型值

时间:2013-12-20 作者:james

我在WordPress中创建了一个自定义帖子类型,如图所示。

enter image description here我想在管理面板的网格视图中显示此自定义帖子中的值eneterd。现在,管理面板只显示帖子标题、日期和评论图像,但我想显示在自定义帖子中输入的Pair2、buy-sell和activity字段值。我搜索了很多,但找不到任何解决方案。我应该如何在WordPress中进行?enter image description here

EDIT<这是我如何制作自定义帖子的

   <?php
if( ! function_exists( \'quote_create_post_type\' ) ) :
  function quote_create_post_type() {
    $labels = array(
      \'name\' => __( \'ForexPair\' ),
      \'singular_name\' => __( \'ForexPair\' ),
      \'add_new\' => __( \'Add Pair Entry\' ),
      \'all_items\' => __( \'All Pairs\' ),
      \'add_new_item\' => __( \'Add Pair\' ),
      \'edit_item\' => __( \'Edit Pair\' ),
      \'new_item\' => __( \'New Pair\' ),
      \'view_item\' => __( \'View Pair\' ),
      \'search_items\' => __( \'Search Pairs\' ),
      \'not_found\' => __( \'No pair found\' ),
      \'not_found_in_trash\' => __( \'No pair found in trash\' ),
      \'parent_item_colon\' => __( \'Parent pair\' )
      //\'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\'
      ),
      \'register_meta_box_cb\' => \'quote_add_post_type_metabox\'
    );
    register_post_type( \'quote\', $args );
    //flush_rewrite_rules();

  }
  add_action( \'init\', \'quote_create_post_type\' );


  function quote_add_post_type_metabox() { // add the meta box
    //add_meta_box( \'quote_metabox\', \'quote_metabox\', \'quote\', \'normal\' );
    add_meta_box( \'quote_metabox\', \'Pair Entries\', \'quote_metabox\', \'quote\', \'normal\' );
  }


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

    // Get the data if its already been entered
    $quote_post_entryprice = get_post_meta($post->ID, \'_quote_post_entryprice\', true);
    $quote_post_stoploss = get_post_meta($post->ID, \'_quote_post_stoploss\', true);
    $quote_post_pairname = get_post_meta($post->ID, \'_quote_post_pairname\', true);
    $quote_post_buysell = get_post_meta($post->ID, \'_quote_post_buysell\', true);
    $quote_post_activity = get_post_meta($post->ID, \'_quote_post_activity\', true);
    $quote_post_takeprofit = get_post_meta($post->ID, \'_quote_post_takeprofit\', true);
    // Echo out the field
    ?>
   <link rel="stylesheet" href="http://gripforex.com/wp-content/themes/Forsy/select2.css" />
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/jquery-1.7.2.min.js"></script>
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/jquery-ui-1.10.3.custom.min.js"></script>
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/select2.js"></script>
  <style>
  .lbl{
  width: 86px;
display: block;
line-height: 20px;
  }
  </style>
    <div class="width_full p_box">
      <p>

        <label class="lbl">Pair:</label>
  <script>
        $(document).ready(function() { $("#e1").select2({

      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
});
    });
    $(document).ready(function() { $("#e2").select2({
      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
}); });
    $(document).ready(function() { $("#e3").select2({
      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
}); });
    </script>

  <select name="quote_post_pairname" id="e1" style="width:144px;">
  <option value="AUD/CAD" <?php if($quote_post_pairname=="AUD/CAD") echo "selected"; ?> >AUD/CAD</option>
  <option value="AUD/CHF" <?php if($quote_post_pairname=="AUD/CHF") echo "selected"; ?> >AUD/CHF</option>



  </select></p>
        <p>
        <label class="lbl">Buy Sell
          <select name="quote_post_buysell" id="e2" style="width:144px;">
            <option value="Buy" <?php if($quote_post_buysell=="Buy") echo "selected"; ?> >Buy</option>
            <option value="Sell" <?php if($quote_post_buysell=="Sell") echo "selected"; ?> >Sell</option>
          </select>
        </label>
      </p>
        <p>
        <label class="lbl">Activity
          <select name="quote_post_activity" id="e3" style="width:144px;">
            <option value="Get Ready" <?php if($quote_post_activity=="Get Ready") echo "selected"; ?> >Get Ready</option>
            <option value="Active" <?php if($quote_post_activity=="Active") echo "selected"; ?> >Active</option>
          </select>
        </label>
      </p>      
      <p>
        <label class="lbl">Entry Price<br>
          <input type="text" name="quote_post_entryprice" class="widefat" value="<?php echo $quote_post_entryprice; ?>">
        </label>
      </p>
      <p><label class="lbl">Stop Loss<br>
          <input type="text" name="quote_post_stoploss" value="<?php echo $quote_post_stoploss; ?>" class="widefat" />
        </label>
      </p>
      <p><label class="lbl">Take Profit<br>
          <input type="text" name="quote_post_takeprofit" value="<?php echo $quote_post_takeprofit; ?>" class="widefat" />
        </label>
      </p>      
    </div>
  <?php
  }


  function quote_post_save_meta( $post_id, $post ) { // save the data

    if( !wp_verify_nonce( $_POST[\'quote_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;
    }

    $quote_post_meta[\'_quote_post_entryprice\'] = $_POST[\'quote_post_entryprice\'];
    $quote_post_meta[\'_quote_post_stoploss\'] = $_POST[\'quote_post_stoploss\'];
    $quote_post_meta[\'_quote_post_pairname\'] = $_POST[\'quote_post_pairname\'];
    $quote_post_meta[\'_quote_post_buysell\'] = $_POST[\'quote_post_buysell\'];
    $quote_post_meta[\'_quote_post_activity\'] = $_POST[\'quote_post_activity\'];
    $quote_post_meta[\'_quote_post_takeprofit\'] = $_POST[\'quote_post_takeprofit\'];
    // add values as custom fields
    foreach( $quote_post_meta as $key => $value ) { // cycle through the $quote_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\', \'quote_post_save_meta\', 1, 2 ); // save the custom fields
endif; // end of function_exists()


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

    $args = array(
      \'numberposts\'     => $num,
      \'offset\'          => 0,
      //\'category\'        => ,
      \'orderby\'         => \'menu_order, post_date\', // post_date, rand
      \'order\'           => \'DESC\',

      \'post_type\'       => \'quote\',

      \'post_status\'     => \'publish\',
      \'suppress_filters\' => true,
    );

    $posts = get_posts( $args );
    global $wpdb;

 $querystr = "select $wpdb->posts.* 
    FROM $wpdb->posts
  WHERE $wpdb->posts.post_type = \'quote\'
  ";


    $html = \'<table><tr><th>Pair</th><th>Action</th><th>Status</th><th>Entry Price</th><th>Stop Loss</th><th>Take Profit</th></tr>\';
    foreach ( $posts as $post ) {
      $meta_entryprice = get_post_meta( $post->ID, \'_quote_post_entryprice\', true );
      $meta_stoploss = get_post_meta( $post->ID, \'_quote_post_stoploss\', true );
      $meta_pairname = get_post_meta( $post->ID, \'_quote_post_pairname\', true );
      $meta_buysell = get_post_meta( $post->ID, \'_quote_post_buysell\', true );
      $meta_activity = get_post_meta( $post->ID, \'_quote_post_activity\', true );
      $meta_takeprofit = get_post_meta( $post->ID, \'_quote_post_takeprofit\', true );


      $html.=\'<tr>\';
      $html .= \'

          <td>\'.$meta_pairname.\'</td>
          <td>\'.$meta_buysell.\'</td>
          <td>\'.$meta_activity.\'</td>
          <td>\'.$meta_entryprice.\'</td>

          <td>\'.$meta_stoploss.\'</td>

          <td>\'.$meta_takeprofit.\'</td>
        \';
      $html.=\'</tr>\';
    }
    $html .="</table>";
    $html = \'<div class="wrapper">\'.$html.\'</div>\';
    return $html;
  }
endif; 
?>

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

您可以使用manage_edit-${post_type}_columns 过滤器与manage_${post_type}_posts_custom_column 行动检查example 并逐步解构。

首先确保输入注册的自定义帖子类型的正确名称。您使用名称注册了帖子类型quote.

然后定义要添加的列(请参见set_custom_edit_quote_columns) 并分配适当的值(请参阅custom_quote_columns).

你应该得到这样的结果:

add_filter( \'manage_edit-quote_columns\', \'set_custom_edit_quote_columns\' );
add_action( \'manage_quote_posts_custom_column\' , \'custom_quote_columns\', 10, 2 );

function set_custom_edit_quote_columns( $columns ) {

    $columns[\'pair\'] = __( \'Pair\' );
    $columns[\'buysell\'] = __( \'Buy Sell\' );
    $columns[\'activity\'] = __( \'Activity\' );

    $columns[\'entryprice\'] = __( \'Entry Price\' );
    $columns[\'stoploss\'] = __( \'Stop Loss\' );
    $columns[\'takeprofit\'] = __( \'Take Profit\' );

    return $columns;
}

function custom_quote_columns( $column, $post_id ) {
    switch ( $column ) {

        case \'pair\' :
            echo get_post_meta( $post_id, \'_quote_post_pairname\', true );
            break;

        case \'buysell\' :
            echo get_post_meta( $post_id, \'_quote_post_buysell\', true );
            break;

        case \'activity\' :
            echo get_post_meta( $post_id, \'_quote_post_activity\', true );
            break;

        case \'entryprice\' :
            echo get_post_meta( $post_id, \'_quote_post_entryprice\', true );
            break;

        case \'stoploss\' :
            echo get_post_meta( $post_id, \'_quote_post_stoploss\', true );
            break;

        case \'takeprofit\' :
            echo get_post_meta( $post_id, \'_quote_post_takeprofit\', true );
            break;

    }
}

结束

相关推荐

将内容添加到/wp-admin/plugin-install.php管理屏幕

我想向上显示的插件信息添加一些外部内容/wp-admin/plugin-install.php 管理屏幕。/wp-admin/plugin-install.php 调用以下命令:$wp_list_table = _get_list_table(\'WP_Plugin_Install_List_Table\'); 如果我破解这个核心文件作为测试,我可以添加内容,我想,好的。然而,作为一名新的WP开发者,我真的很难看到如何通过插件添加这些内容。class WP_Plugin_Install_List