首页 > 解决方案 > add_filter 函数没有在 wordpress 中被调用

问题描述

我在 wordpress 文件中创建了一个 custom_post_type。但是我在添加过滤功能时遇到了麻烦。过滤功能似乎没有被唤起?alpha_set_contact_coloumns() 没有提供任何结果。

<?php

$contact = get_option( 'activate_contact' );
if(@$contact == 1){
  add_action( 'init', 'alpha_contact_custom_post_type' );
  add_filter( 'manage_alpha-contact_posts_coloumns', 'alpha_set_contact_coloumns' );
}


function alpha_contact_custom_post_type() {
  $labels = array(
    'name'          => 'Messages',
    'singular_name' => 'Message',
    'menu_name'     => 'Messages',
    'name_admin_bar'=> 'Message'
  );

  $args = array(
    'labels'        => $labels,
    'show_ui'       => true,
    'show_in_menu'  => true,
    'capability_type'=> 'post',
    'hierarchical'  => false,
    'menu_position' => 26,
    'menu_icon'     => 'dashicons-email-alt',
    'supports'      => array('title', 'editor', 'author')
  );

  register_post_type( 'alpha-contact', $args );
}

function alpha_set_contact_coloumns( $coloumns ) {
  unset( $coloumns['author']);
  return $coloumns;
}

标签: wordpresscustom-post-typeadd-filter

解决方案


试试这个代码。

代码中的拼写错误。我manage_contact_posts_coloumnsmanage_alpha-contact_posts_columns.

它不是coloumns

$contact = get_option( 'activate_contact' );

if(@$contact == 1){
  add_action( 'init', 'alpha_contact_custom_post_type' );
  add_filter( 'manage_alpha-contact_posts_columns', 'alpha_set_contact_coloumns' );
}


function alpha_contact_custom_post_type() {
  $labels = array(
    'name'          => 'Messages',
    'singular_name' => 'Message',
    'menu_name'     => 'Messages',
    'name_admin_bar'=> 'Message'
  );

  $args = array(
    'labels'        => $labels,
    'show_ui'       => true,
    'show_in_menu'  => true,
    'capability_type'=> 'post',
    'hierarchical'  => false,
    'menu_position' => 26,
    'menu_icon'     => 'dashicons-email-alt',
    'supports'      => array('title', 'editor', 'author')
  );

  register_post_type( 'alpha-contact', $args );
}

function alpha_set_contact_coloumns( $coloumns ) {      
  unset( $coloumns['author']);
  return $coloumns;
}

推荐阅读