Я пытаюсь сделать эту функцию много дней, и это заставляет меня сходить с ума!Динамически приносите сообщение WordPress
У меня есть одна страница темы в WP, и в одном из них есть div слева со списком сообщений на сайте и справа, div, который должен отображать содержимое кликнувшего сообщения.
Я нашел this question и followed up the linked tutorial и был частично успешным.
Мне удалось привести содержимое динамически, и все, что я хочу, отображается, но кажется, что порядок задач неверен. Heres, как это действует:
- Я нажимаю на ссылку.
- текущий контент уходит.
- Поля загрузки отображаются правильно.
- ОСТОРОЖНО:
- через 1 секунду или около того текущий контент заменяется новым контентом, а адресная строка не изменяется вообще.
Вот код, у меня есть:
atracoes.js $ (документ) .ready (функция() {
var hash = window.location.hash.substr(1); var href = $('.controle nav li a').each(function(){ var href = $(this).attr('href'); if(hash==href.substr(0,href)){ var aCarregar = hash+'.html #atr-conteudo'; $('#atr-conteudo').load(aCarregar) } }); $('.controle nav li a').click(function() { var aCarregar = $(this).attr('href')+' #atr-conteudo'; $('#atr-conteudo').hide('fast',carregarConteudo); $('#carregando').remove(); $('#atracoes').append('<span id="carregando">Carregando...</span>'); $('#carregando').fadeIn('normal'); window.location.hash = $(this).attr('href').substr(0,$(this).attr('href')); function carregarConteudo() { $('#atr-conteudo').load(aCarregar,'',mostrarNovoConteudo()); } function mostrarNovoConteudo() { $('#atr-conteudo').show('normal',esconderCarregando()); } function esconderCarregando() { $('#carregando').fadeOut('normal'); } return false; }); });
index.php (динамическое содержимое часть)
<div class="main" id="atracoes"> <div class="controle"> <nav> <?php $args = array('posts_per_page' => 20); $myposts = get_posts($args); foreach ($myposts as $post) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; wp_reset_postdata();?> </nav> </div> <div id="atr-conteudo"> <?php the_post_thumbnail(); ?> <div id="atr-texto"> <h2><?php the_title(); ?></h2> <?php the_content(); ?> </div> </div> </div>
- single.php (часть я выщипывание с помощью AJAX)
<!-- article --> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <!-- post thumbnail --> <?php if (has_post_thumbnail()) : // Check if Thumbnail exists ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail(); // Fullsize image for the single post ?> </a> <?php endif; ?> <!-- /post thumbnail --> <div id="atr-texto"> <!-- post title --> <h1> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> </h1> <!-- /post title --> <?php the_content(); // Dynamic Content ?> <?php edit_post_link(); // Always handy to have Edit Post Links available ?> </div> </article>
Потрясающе! Это волшебство! Спасибо, миллион! – Digger
@GuiHarrison Нет проблем, простую ошибку сделать (тоже мне пришлось заметить: P) – meiamsome