dongyingtang3803 2017-12-09 04:39
浏览 761
已采纳

window.location.reload(true)重新加载页面,但页面需要刷新才能显示更改

Have a function that makes a change to taxonomy term via AJAX. This works great, except the content remains unchanged on window.location.reload(true) even though the change has been made. See the code and GIF below to understand.

This is an example that adds the button and reloads page on click

if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$text = (in_category( 'live') ? 'Activate' : 'Activate');
echo '<li><a href="" onclick="window.location.reload(true);return toggleLive(this, ' . $post->ID . ');">' . $text . '</a></li>';
}

explainer gif

So, is there another way that I can reload the page onClick that may help? Also, the post modified date is not updating, yet changes have been made to the post.

Thanks in advance for your help

EDIT - I have already tried location.href = location.href; and document.location.reload();

ADDITIONAL INFO -

Function

add_action('wp_ajax_toggle_live', function(){
// Check ID is specified
if ( empty($_REQUEST['post']) ) {
    die( __('No post ID specified.') );
}

// Load post
$post_id = (int) $_REQUEST['post'];
$post = get_post($post_id);
if (!$post) {
    die( __('You attempted to edit an item that doesn&amp;#8217;t exist. Perhaps it was deleted?') );
}

// Check permissions
$post_type_object = get_post_type_object($post->post_type);
if ( !current_user_can($post_type_object->cap->edit_post, $post_id) ) {
    die( __('You are not allowed to edit this item.') );
}

// Load current categories
$terms = wp_get_post_terms($post_id, 'campaign_action', array('fields' => 'ids'));

// Add/remove Starred category
$live = get_term_by( 'live', 'campaign_action' );

$index = array_search($live, $terms);
if ($_REQUEST['value']) {
    if ($index === false) {
        $terms[] = $live;
    }
} else {
    if ($index !== false) {
        unset($terms[$index]);
    }
}

wp_set_object_terms( $post_id, 'live', 'campaign_action' );

die('1');
});

JS

function toggleLive(caller, post_id)
{

var $ = jQuery;
var $caller = $(caller);

var waitText = ". . .";
var liveText = ". . .";
var deactivateText = ". . .";

// Check there's no request in progress
if ($caller.text() == waitText) {
    return false;
}

// Get the new value to set to
var value = ($caller.text() == liveText ? 1 : 0);

// Change the text to indicate waiting, without changing the width of the button
$caller.width($caller.width()).text(waitText);

// Ajax request
var data = {
    action: "toggle_live",
    post:   post_id,
    value:  value
};

jQuery.post("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php", data, function(response)
{
    if (response == "1") {

        // Success
        if (value) {
            $caller.text(deactivateText);
        } else {
            $caller.text(liveText);
        }

    } else {

        // Error
        alert("Error: " + response);

        // Reset the text
        if (value) {
            $caller.text(deactivateText);
        } else {
            $caller.text(liveText);
        }

    }

    // Reset the width
    $caller.width("auto");
});

// Prevent the link click happening
return false;
}

IT WORKS RIGHT ON PAGE THAT ISN'T SINGULAR enter image description here

  • 写回答

3条回答 默认 最新

  • doudi1449 2017-12-09 04:57
    关注

    Is toggleLive the function that makes the AJAX request? You are calling reload immediately on click before changes are reflected on the backend. If you are using Jquery include your reload code in the complete callback function that indicates completion of your AJAX request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么