我意识到这已经晚了将近两年,但我希望这对一些来到这里寻找答案的人仍然有用。
Disclaimer: 根据WordPress管理设计模式,这不是一个很好的模式,但在必要时使用它确实很好。
在函数中插入以下内容。主题中的php文件
设置管理列标题
function set_column_heading( $defaults ) {
$defaults[\'featured_image\'] = \'Featured Image\';
return $defaults;
}
add_filter( \'manage_posts_columns\', \'set_column_heading\' );
设置您的管理列值这已更改为处理当前状态,即是否将特征图像设置为通过抓取显示
post meta. 它还处理
nonce.
function set_column_value( $column_name, $post_ID ) {
if ( \'featured_image\' === $column_name ) {
$post_featured_image = get_featured_image( $post_ID );
if ( $post_featured_image ) {
$featured_image_display = get_post_meta( $post_ID, \'show_featured_image\' );
$show_class = \'show\';
$show_text = \'Display Featured Image\';
if ( $featured_image_display ) {
$show_class = \'hide\';
$show_text = \'Hide Featured Image\';
}
echo \'<button class="\' . esc_attr( $show_class ) . \'" data-nonce="\' . esc_attr( wp_create_nonce( \'my_display_featured_image_\' . $post_ID ) ) . \'">\' . esc_html( $show_text ) . \'</button>\';
}
}
}
function get_featured_image( $post_ID ) {
$post_thumbnail_id = get_post_thumbnail_id( $post_ID );
if ( $post_thumbnail_id ) {
$post_thumbnail_img = wp_get_attachment_image_src( $post_thumbnail_id, \'featured_preview\' );
return $post_thumbnail_img[0];
}
}
add_action( \'manage_posts_custom_column\', \'set_column_value\', 10, 2 );
在帖子列表页面将脚本排队这很简单。确保脚本路径正确,并且jQuery是一个依赖项。
add_action( \'admin_enqueue_scripts\', \'my_enqueue_admin_scripts\' );
function my_enqueue_admin_scripts( $hook ) {
if ( \'edit.php\' !== $hook ) {
return;
}
wp_enqueue_script( \'set_featured_image\', get_template_directory_uri() . \'/js/set-featured-image.js\', array( \'jquery\' ), \'1.0.0\', true );
}
注册一个仅限管理员的ajax响应函数,
set-featured-image.js
. 这将更新
post meta 以反映状态。它还处理
nonce 验证。
add_action( \'wp_ajax_my_display_featured_image\', \'my_display_featured_image\' );
function my_display_featured_image() {
if ( ! isset( $_POST[\'featuredImage\'], $_POST[\'postID\'] ) ) {
wp_send_json_error( array( \'isset\' ) );
wp_die();
}
if ( ! is_numeric( $_POST[\'postID\'] ) ) {
wp_send_json_error( array( \'checks\' ) );
wp_die();
}
$post_id = intval( $_POST[\'postID\'] );
check_ajax_referer( \'my_display_featured_image_\' . $post_id );
$set_featured_image = wp_validate_boolean( $_POST[\'featuredImage\'] );
update_post_meta( $post_id, \'show_featured_image\', $set_featured_image );
wp_send_json( array( \'featuredImage\' => $set_featured_image ) );
wp_die();
}
将以下内容插入正在排队的新脚本中,
set-featured-image.js
处理click事件和ajax请求当您单击按钮时,将正确的数据放在一起,处理UI更改,并将数据发送到PHP中的ajax响应处理程序,my_display_featured_image()
. 一旦我们得到响应,它就会更新按钮状态,以反映我们存储在post meta中的正确值。
(function($) {
$(\'.featured_image button\').on(\'click\', function(e) {
e.preventDefault();
var $button = $(this);
var display = false;
if($button.hasClass(\'show\')) {
display = true;
}
var postID = $button.parents(\'tr\').attr(\'id\').replace(/post-/g, \'\');
$button.attr(\'disabled\', true);
var data = {
action: \'my_display_featured_image\',
featuredImage: display,
postID: postID,
_wpnonce: $button.data(\'nonce\')
};
$.ajax({
type: "POST",
url: ajaxurl,
data: data,
success: function(response) {
$button.removeAttr(\'disabled\');
console.log(response);
if (response.featuredImage) {
$button.text(\'Hide Featured Image\');
$button.attr(\'class\', \'hide\');
} else {
$button.text(\'Display Featured Image\');
$button.attr(\'class\', \'show\');
}
}
});
});
})(jQuery);
结果显示“管理”列,其中包含每个具有特色图像的帖子的按钮。按钮文本反映post meta是否设置为显示特色图像。当您单击按钮时,它会切换此post元值的状态。
在您的模板中,您所要做的就是获取此帖子元并使用它有条件地显示特色图像。
$featured_image_display = get_post_meta( get_the_ID(), \'show_featured_image\' );
if ( $featured_image_display ) {
// Display featured image.
}