设置和取消设置自定义字段值

时间:2017-05-04 作者:Istiaque Ahmed

在管理面板中显示所有帖子时,我有一个自定义栏“特色图片”。此列的值为YesOrNO。

设置列名:我有内部函数。php:

function set_column_heading($defaults) {
    $defaults[\'featured_image\'] = \'Featured Image\';
    return $defaults;
}
add_filter(\'manage_posts_columns\', \'set_column_heading\');
为了设置列值,我有:

 function set_column_value($column_name, $post_ID) {
        if ($column_name == \'featured_image\') {
            $post_featured_image = get_featured_image($post_ID);
            if ($post_featured_image) {
                echo \'YesOrNO\';
            }
        }
    }

    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);
是的,我得到了预期的列名和值(即YesOrNo)。在wordpress前端,我想显示带有条件的帖子的特色图片。条件是:我需要一个列值(即YesOrNo)上的单击处理程序,以便我可以将其切换为选中或未关闭,并且我喜欢只显示所选图像中的特征图像。

我该怎么做?

1 个回复
SO网友:MikeNGarrett

我意识到这已经晚了将近两年,但我希望这对一些来到这里寻找答案的人仍然有用。

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 );

在帖子列表页面将脚本排队

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.
}

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: