javascript - 动态发布 WordPress 帖子

标签 javascript php jquery ajax wordpress

我已经尝试让这个功能工作很多天了,它快把我逼疯了!

我在 WP 中有一个单页主题,其中一个主题的左侧有一个 div,其中包含网站中的帖子列表,右侧有一个 div,应显示单击的帖子的内容。

我找到了this questionfollowed up the linked tutorial并取得了部分成功。

我设法动态地呈现内容,我想要的只是显示,但任务的顺序似乎是错误的。它的作用如下:

  1. 我点击了链接。
  2. 当前内容消失。
  3. 加载范围正确显示。
  4. 相同的内容淡入。
  5. 大约 1 秒后,当前内容将被新内容替换,并且地址栏根本不会改变。

这是我的代码:

  • atracoes.js $(文档).ready(function() {

    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>
    

最佳答案

您在将函数传递给 jQuery 执行之前调用这些函数,而不是允许 jQuery 执行它们:

function carregarConteudo () {
    $('#atr-conteudo').load(aCarregar,'',mostrarNovoConteudo);
}
function mostrarNovoConteudo () {
    $('#atr-conteudo').show('normal',esconderCarregando);
}

(请注意,函数名称后面不再有 ())

关于javascript - 动态发布 WordPress 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940141/

相关文章:

javascript - 使用数组作为排序顺序

javascript - 在 Internet Explorer 中更改 onchange 处理程序中的 tabindex 时出现奇怪的行为

javascript - React Native列表页脚组件不起作用

javascript - 如何使用 jQuery 使 <button> 充当单选按钮

php - 消息: undefine Offset : 10 from codeigniter

php - 将表单的文本字段映射到实体的 ArrayCollection

php - 当值在 php 中较大时,隐藏的输入类型不会隐藏在表单中

javascript - laravel - 提前输入 |输入字段填充 JSON 数据而不是建议

javascript - 如何根据数据项的属性对数据表中的复杂列进行排序?

javascript - jQuery - 计算一行中的文章 - 它计算两个框中的项目