0

大家好,我是一个 WordPress 菜鸟,所以请多多包涵。因此,在 template.php 中,我需要meta_form()根据一些条件更改函数的功能。这是它的样子:

原始代码:

    function meta_form() {
        global $wpdb;
        $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
        $keys = $wpdb->get_col( "
            SELECT meta_key
            FROM $wpdb->postmeta
            GROUP BY meta_key
            HAVING meta_key NOT LIKE '\_%'
            ORDER BY meta_key
            LIMIT $limit" );
        if ( $keys )
            natcasesort($keys);
    ?>

定制版:

function meta_form() {
    global $wpdb;

  if ( isset($_GET['post']) )
    $post_id = (int) $_GET['post'];
  elseif ( isset($_POST['post_ID']) )
    $post_id = (int) $_POST['post_ID'];
  else
    $post_id = 0;

  if ( $post_id ) {
    $post_ = get_post($post_id);

  }

  if ($post_->post_type == 'video_photo' ){
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta
        where meta_key like 'tqmcf_%'
        GROUP BY meta_key
        HAVING meta_key NOT LIKE '\_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }else{
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta
        GROUP BY meta_key
        HAVING meta_key NOT LIKE '\_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }

    if ( $keys )
        natcasesort($keys);
?>

meta_form覆盖函数的最佳方法是什么?我必须创建一个插件或其他东西吗?我似乎找不到一个干净的答案。(ps我没有编写此代码,我只是修复它的人)。

4

1 回答 1

1

meta_form功能不可插入,并且没有可用的钩子,因此,由于不建议真正推荐破解内核,因此您需要另一种方法。以下是一个jQuery解决方案。将代码放入functions.php主题文件或插件中:

add_action( 'admin_footer', 'so17239871_mod_metakeyselect' );
function so17239871_mod_metakeyselect()
{
    global $pagenow, $post_type;

    if( ! ( 'post.php' == $pagenow && 'video_photo' == $post_type ) )
        return;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#metakeyselect > option").hide();
    jQuery("#metakeyselect > option[value^='tqmcf_']").show();
});
</script>
<?php
}
于 2013-06-21T17:58:53.573 回答