如何在仪表板小工具中显示自定义发布类型自定义列及其内容?

时间:2017-03-08 作者:Himani

我正在使用以下两个函数添加和显示插件中自定义帖子的列内容

    add_filter("manage_{$this->post_slug}_posts_columns", array($this, \'browseColumns\'));
add_action("manage_{$this->post_slug}_posts_custom_column", array($this, \'browseCustomColumns\'), 10, 2);

 public function browseColumns($columns) {
$new = array();
foreach ($columns as $key => $title) {
    if ($key == \'date\') {
    $new[\'wp_type\'] = __(\'Type\', self::$lang_slug);
    $new[\'wp_cost\'] = __(\'Cost\', self::$lang_slug);
    }
    $new[$key] = $title;
}
return $new;
}

   public function browseCustomColumns($column, $post_id) {
switch ($column) {
    case \'wp_type\' :
    $type = get_post_meta($post_id, \'_wp_type\', true);
    echo empty($type) ? \'-\' : (int) $type;
    break;
    case \'wp_cost\' :
    $cost = PluginClass::getCost($post_id);
    echo empty($cost) ? \'-\' : (int) $cost;
    break;

}
}
下面是为自定义插件添加仪表板小部件的代码

/*
 *
 * Add a widget to the dashboard.
 *
 * This function is hooked into the \'add_dashboard_setup\' action below.
 */

add_action(\'wp_dashboard_setup\', array($this, \'add_dashboard_widget\'));

  public function add_dashboard_widget() {
    add_meta_box(\'idx_dashboard_widget\', \'WP WebinarSystem\', array($this, \'compile_dashboard_widget\'), \'dashboard\', \'normal\', \'high\' );
}

/*
 *
 * Function to output the contents of our Dashboard Widget.
 *
 */
public function compile_dashboard_widget()
{
    echo $this->dashboard_widget_html();
    $this->loadPluginScripts();
    $this->loadFrontScripts();
}

public function dashboard_widget_html()
{


}
我想在Dashboard小部件中显示自定义帖子的类型和成本列。

我想知道是否有任何可用的挂钩可以重用,或者我应该继续编写html吗?

1 个回复
最合适的回答,由SO网友:Paul \'Sparrow Hawk\' Biron 整理而成

回答您的具体问题:不,没有允许您“重用”的挂钩。然而,没有什么能阻止你打电话给你的browseCustomColumns() 呈现仪表板小部件的函数中的函数。然而,为了做到这一点,您必须对compile_dashboard_widget() 功能:

public
function
compile_dashboard_widget ()
{
    // removed the call to echo, just call $this->dashboard_widget_html () and have
    // $this->dashboard_widget_html () do all the echo\'ing
    $this->dashboard_widget_html () ;
    $this->loadPluginScripts () ;
    $this->loadFrontScripts () ;

    return ;
}

public
function
dashboard_widget_html ()
{
    global $post ;

    $args = array (
        \'post_type\' => $this->post_slug,
        // any other WP_Query args you want
        ) ;
    $posts = new WP_Query ($args) ;

    if ($posts->has_posts ()) {
        while ($posts->has_posts ()) {
            $posts->the_post () ;

            // you\'d probably want addition markup here
            // (e.g., a table, which each post in a <tr> and title/type/cost as <td>)
            // but you can figure out how you want your dashboard widget to look
            the_title () ;

            $this->browseCustomColumns (\'wp_type\', $post->ID) ;
            $this->browseCustomColumns (\'wp_cost\', $post->ID) ;
            }
        }
    else {
        echo \'no custom posts\' ;
        }

    wp_reset_postdata () ;

    return ;
}
希望这有帮助。

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请