显示WordPress中的帖子ID、产品、类别和标签

huangapple go评论66阅读模式
英文:

Display post id - product - category and tag in wordpress

问题

以下是已翻译的内容:

我想在我的WordPress主题的管理面板中,在单独的列中显示所有帖子、产品、类别和标签的ID。

为此目的,我准备了以下代码,并将其放在“functions.php”文件中:

add_filter( 'manage_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_posts_custom_column', 'revealid_id_column_content', 5, 2 );

function revealid_add_id_column( $columns ) {
   $columns['revealid_id'] = '(ID)';
   return $columns;
}

function revealid_id_column_content( $column, $id ) {
  if( 'revealid_id' == $column ) {
    echo $id;
  }
}

$custom_post_types = get_post_types( 
  array( 
     'public'   => true, 
     '_builtin' => true 
  ), 
  'names'
); 

foreach ( $custom_post_types as $post_type ) {
 add_action( 'manage_edit-'. $post_type . '_columns', 'revealid_add_id_column' );
 add_filter( 'manage_'. $post_type . '_custom_column', 'revealid_id_column_content' );
}

$taxonomies = get_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
	add_action( 'manage_edit-' . $taxonomy . '_columns', 'revealid_add_id_column' );
	add_filter( 'manage_' . $taxonomy . '_custom_column', 'revealid_id_column_content' );
}

总结:
除了显示帖子和产品的ID外,我还想显示类别和标签的ID。

此外,将放在最后的ID列移到第一列(在复选框之后)。

非常感谢您的提前帮助。

英文:

I want to display the ID of all posts - products - categories and tags in a separate column in the admin panel of my WordPress theme.

For this purpose, I prepared the following code and placed it in the functions.php file:

add_filter( 'manage_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_posts_custom_column', 'revealid_id_column_content', 5, 2 );

function revealid_add_id_column( $columns ) {
   $columns['revealid_id'] = '(ID)';
   return $columns;
}

function revealid_id_column_content( $column, $id ) {
  if( 'revealid_id' == $column ) {
    echo $id;
  }
}

$custom_post_types = get_post_types( 
  array( 
     'public'   => true, 
     '_builtin' => true 
  ), 
  'names'
); 

foreach ( $custom_post_types as $post_type ) {
 add_action( 'manage_edit-'. $post_type . '_columns', 'revealid_add_id_column' );
 add_filter( 'manage_'. $post_type . '_custom_column', 'revealid_id_column_content' );
}

$taxonomies = get_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
	add_action( 'manage_edit-' . $taxonomy . '_columns', 'revealid_add_id_column' );
	add_filter( 'manage_' . $taxonomy . '_custom_column', 'revealid_id_column_content' );
}

This code displays the ID of all posts and products in the last column, but does not show the ID of any category or tag.

I also added the following code to move the ID to the first column (after the checkbox), but by adding this code, a fatal error occurs:

function revealid_add_id_column( $columns ) {
	$checkbox = array_slice( $columns , 0, 1 );
	$columns = array_slice( $columns , 1 );

	$id['revealid_id'] = '(ID)';
	
	$columns = array_merge( $checkbox, $id, $columns );
	return $columns;
}

In summary:
In addition to displaying the ID of posts and products, I want to display the ID of categories and tags as well.

Also, the ID column placed at the end should be moved to the first column (after the check box).

Thank you very much in advance.

答案1

得分: 1

关于你的第一个问题,你接近正确,但是在文章类型和分类法的钩子之间有一个区别:对于分类法,列内容的钩子是一个过滤器,而不是一个动作(不要问我为什么)。

关于第二个问题,如果不看到实际错误是什么,我怀疑是因为在尝试设置偏移之前未将 $id 定义为数组。

这是我的方法(已测试):

add_action('init', static function () {

	$post_types = get_post_types();
	$taxonomies = get_taxonomies();

	add_filter('manage_posts_columns', static function ($columns) {
		$first_column  = array_slice($columns, 0, 1);
		$second_column = array('revealid_id' => 'ID');
		$columns       = array_slice($columns, 1);

		return $first_column + $second_column + $columns;
	});

	add_action('manage_posts_custom_column', static function ($column, $id) {
		if ('revealid_id' !== $column) {
			return;
		}

		echo $id;
	}, 10, 2);

	foreach ($post_types as $post_type) {

		add_filter('manage_edit-' . $post_type . '_columns', static function ($columns) {
			$first_column  = array_slice($columns, 0, 1);
			$second_column = array('revealid_id' => 'ID');
			$columns       = array_slice($columns, 1);

			return $first_column + $second_column + $columns;
		});

		add_action('manage_' . $post_type . '_custom_column', static function ($column, $id) {
			if ('revealid_id' !== $column) {
				return;
			}

			echo $id;
		}, 10, 2);

	}

	foreach ($taxonomies as $taxonomy) {

		add_filter('manage_edit-' . $taxonomy . '_columns', static function ($columns) {
			$first_column  = array_slice($columns, 0, 1);
			$second_column = array('revealid_id' => 'ID');
			$columns       = array_slice($columns, 1);

			return $first_column + $second_column + $columns;
		});

		add_filter('manage_' . $taxonomy . '_custom_column', static function ($content, $column, $id) {
			if ('revealid_id' !== $column) {
				return $content;
			}

			return $id;
		}, 10, 3);

	}

});

你绝对会想要减少重复;我在这里这样做是为了非常清晰地显示区别。

英文:

For your first question, you're close, but there's a difference between the hooks for post types and taxonomies: for taxonomies, the hook for the column's content is a filter, not an action (don't ask me why).

For the second question, without seeing what the actual error is, I suspect it's because $id is not defined as an array before attempting to set the offset.

Here's my approach (tested):

add_action( 'init', static function () {

	$post_types = get_post_types();
	$taxonomies = get_taxonomies();

	add_filter( 'manage_posts_columns', static function ( $columns ) {
		$first_column  = array_slice( $columns, 0, 1 );
		$second_column = array( 'revealid_id' => 'ID' );
		$columns       = array_slice( $columns, 1 );

		return $first_column + $second_column + $columns;
	} );

	add_action( 'manage_posts_custom_column', static function ( $column, $id ) {
		if ( 'revealid_id' !== $column ) {
			return;
		}

		echo $id;
	}, 10, 2 );

	foreach ( $post_types as $post_type ) {

		add_filter( 'manage_edit-' . $post_type . '_columns', static function ( $columns ) {
			$first_column  = array_slice( $columns, 0, 1 );
			$second_column = array( 'revealid_id' => 'ID' );
			$columns       = array_slice( $columns, 1 );

			return $first_column + $second_column + $columns;
		} );

		add_action( 'manage_' . $post_type . '_custom_column', static function ( $column, $id ) {
			if ( 'revealid_id' !== $column ) {
				return;
			}

			echo $id;
		}, 10, 2 );

	}

	foreach ( $taxonomies as $taxonomy ) {

		add_filter( 'manage_edit-' . $taxonomy . '_columns', static function ( $columns ) {
			$first_column  = array_slice( $columns, 0, 1 );
			$second_column = array( 'revealid_id' => 'ID' );
			$columns       = array_slice( $columns, 1 );

			return $first_column + $second_column + $columns;
		} );

		add_filter( 'manage_' . $taxonomy . '_custom_column', static function ( $content, $column, $id ) {
			if ( 'revealid_id' !== $column ) {
				return $content;
			}

			return $id;
		}, 10, 3 );

	}

} );

You will absolutely want to reduce the repetition; I did so here to be very clear on the differences.

huangapple
  • 本文由 发表于 2023年6月9日 06:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定