自定义发布类型:可以将多个变量添加到一列中吗?

时间:2011-01-31 作者:Noel Tock

(由于我的自定义帖子类型有点长,为了简单起见,我复制了一个伪帖子)

我想做的是,假设我描述了几个“类似”的变量,即price1、price2、price3等。我是否能够将这些值堆叠在一列中(而不是将其显示在3列上),即。$price1<br />$price2<br />$price3 ? 原因纯粹是美学上的,否则定制的帖子类型的专栏会很快变得拥挤。

非常感谢。(如前所述,下面的部分不是我正在使用的实际代码,但我认为如果其他人也在搜索相同的内容,这将是一件好事)

add_filter("manage_edit-product_columns", "prod_edit_columns");
add_action("manage_posts_custom_column",  "prod_custom_columns");

function prod_edit_columns($columns){
  $columns = array(
   "cb" => "<input type=\\"checkbox\\" />",
   "title" => "Product Title",
   "description" => "Description",
   "price1" => "Price1",
   "catalog" => "Catalog",
  );

  return $columns;
}

function prod_custom_columns($column){
  global $post;
  switch ($column)
  {
   case "description":
    the_excerpt();
    break;
   case "price": // Now here I have to define the field, but how would I drag in other subsequent price fields?
    $custom = get_post_custom();
    echo $custom["price"][0];
    break;
   case "catalog":
    echo get_the_term_list($post->ID, \'catalog\', \'\', \', \',\'\');
    break;
  }
}

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

是的,你可以这样做。您只需更改输出函数,在本例中,输出函数为

function prod_custom_columns($column){
  global $post;
  switch ($column)
  {
   case "description":
    the_excerpt();
    break;
   case "price": // Now here I have to define the field, but how would I drag in other subsequent price fields?
    $custom = get_post_custom();
    echo $custom["price"][0];
    break;
   case "catalog":
    echo get_the_term_list($post->ID, \'catalog\', \'\', \', \',\'\');
    break;
  }
}
要显示每列所需的内容,请说出您的价格,将其更改为以下内容:

case "price": // Now here I have to define the field, but how would I drag in other subsequent price fields?
    $custom = get_post_custom();
    echo $custom["price"][0];
    echo \'br />\';
    echo $custom["another_FIELD"][0];
    echo \'br />\';
    echo $custom["yet_another_FIELD"][0];
    break;
希望这有帮助

结束

相关推荐