我将通过创建两个额外的自定义字段来解决这个问题:
_score_last
_score_variation
第一条下划线使CF在管理区域中不可见。
在主题中删除以下代码functions.php
:
if( is_admin() )
{
add_action( \'save_post\', \'wpse_57217_check_customfield_variation\', 11, 2 );
}
function wpse_57217_check_customfield_variation( $post_id, $post )
{
if (
( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
or ! current_user_can( \'edit_post\', $post_id )
or wp_is_post_revision( $post )
)
{ // Noting to do.
return;
}
$actual = get_post_meta($post_id, \'score\', true);
$last = get_post_meta($post_id, \'_score_last\', true);
$last = ( \'\' != $last ) ? $last : \'0\';
if ( \'\' != $actual )
{
if ( absint($actual) > absint($last) )
{
update_post_meta( $post_id, \'_score_variation\', \'up\' );
}
elseif ( absint($actual) == absint($last) )
{
update_post_meta( $post_id, \'_score_variation\', \'stable\' );
}
else
{
update_post_meta( $post_id, \'_score_variation\', \'down\' );
}
update_post_meta( $post_id, \'_score_last\', $actual );
}
}
仅供参考,循环内部:
echo \'actual score: \' . get_post_meta($post->ID, \'score\', true);
echo \'<br> last score (value already updated): \' . get_post_meta($post->ID, \'_score_last\', true);
echo \'<br> score variation: \' . get_post_meta($post->ID, \'_score_variation\', true);