簡體   English   中英

AJAX加載Wordpress內容

[英]AJAX load Wordpress Content

我一直在遵循AJAX教程,嘗試將我的wordpress帖子內容加載到我網站的首頁上,而無需重新加載頁面。

我不確定為什么,但是當單擊鏈接時,它仍導航到頁面,而不是將內容加載到我指定的div中。 無論如何,這對我來說太過分了,我希望獲得一些指導!

我一直關注的教程鏈接是-http://net.tutsplus.com/tutorials/javascript-ajax/how-to-load-in-and-animate-content-with-jquery/

我的實時網站是-http://www.mathewhood.com

我當前的代碼是-

對於那些不熟悉wordpress的人,它具有header.php,template.php和footer.php。 每個模板文件都調用頁眉和頁腳,以避免重復代碼。 我將它們鏈接在一起。 我還包括了single_portfolio頁面,該頁面通常會進入該頁面。

ajax.js

$(document).ready(function() {  

    // Check for hash value in URL  
    var hash = window.location.hash.substr(1);  
    var href = $('.caption a').each(function(){  
        var href = $(this).attr('href');  
        if(hash==href.substr(0,href.length-5)){  
            var toLoad = hash+'.html #content';  
            $('#content').load(toLoad)  
        }  
    });  

    $('#.caption a').click(function(){  

    var toLoad = $(this).attr('href')+' #content';  
    $('#content').hide('fast',loadContent);  
    $('#theContainer').remove();  
    $('#wrapper').append('<span id="load">LOADING...</span>');  
    $('#theContainer').fadeIn('normal');  
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);  
    function loadContent() {  
        $('#content').load(toLoad,'',showNewContent())  
    }  
    function showNewContent() {  
        $('#content').show('normal',hideLoader());  
    }  
    function hideLoader() {  
        $('#theContainer').fadeOut('normal');  
    }  
    return false;  

    });  
});

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php bloginfo( 'name' ); ?> | <?php wp_title(); ?></title>
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen" />
<link href='http://fonts.googleapis.com/css?family=Actor' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow:700' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/ajax.js"></script>
<script>
$(document).ready(function() {
    //move the image in pixel
    var move = 8;
    //zoom percentage, 1.2 =120%
    var zoom = 1;
    //On mouse over those thumbnail
    $('.recentPost').hover(function() {
        //Set the width and height according to the zoom percentage
        width = $('.recentPost').width() * zoom;
        height = $('.recentPost').height() * zoom;
        //Move and zoom the image
        $(this).find('img').stop(false,true).animate({'width':width, 'height':height<?php /*?>, 'top':move, 'left':move<?php */?>}, {duration:200});
        //Display the caption
        $(this).find('div.caption').stop(false,true).fadeIn(200);
    },
    function() {
        //Reset the image
        $(this).find('img').stop(false,true).animate({'width':$('.recentPost').width(), 'height':$('.recentPost').height()<?php /*?>, 'top':'8', 'left':'8'<?php */?>}, {duration:100});    
        //Hide the caption
        $(this).find('div.caption').stop(false,true).fadeOut(200);
    });
});
</script>
<script>
$('.thumbs').click(function(e){
    e.preventDefault();
    var contents = $(this).closest('.recentPost').find('.caption').html();

    var $container = $('#theContainer').html(contents);
    $container.show().animate({height:200}, {duration: 1000, easing: 'jswing'}).animate({height:150}, {duration: 1000, easing: 'easeInOutCirc'});
    $container.click(function(){
        $container.animate({height:200}, {duration: 1000, easing: 'easeInExpo'})
        $container.fadeOut('slow');
        $container.html('');
    });
});
</script>

<?php wp_head();?>
</head>

<body>

<div id="wrapper">
    <div id="container">
        <div id="headerWrap">
            <a href="http://www.mathewhood.com"><div id="logo"></div></a>
            <div id="nav"></div>
        </div>

page_home.php

<?php get_header();?>
    <div id="contentWrap">

     <div id="theContainer"></div>

        <div id="newBanner"></div>
        <?php query_posts('category_name=portfolio&showposts=12'); ?>

                <?php while (have_posts()) : the_post(); ?>
                   <div class="recentPost">
                    <a href="<?php the_permalink();?>">
                        <?php the_post_thumbnail('204, 144', array('class' => 'thumbs')); ?>
                    </a>
                    <a href="<?php the_permalink();?>">
                        <div class="caption">
                            <div class="captionTitle"><?php the_title(); ?></div>
                            <p><?php the_content();?></p>
                        </div>
                    </a>
                </div>
                <?php endwhile; ?>
                <div class="cleared"></div>

    </div>
<?php get_footer();?>

single_portfolio.php

<?php
/*
Template Name: Single Portfolio
*/
?>
<?php get_header();?>
<div id="contentWrap">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div id="content">
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
        </div>
        <?php endwhile; ?> 
    <?php endif; ?>    
</div>
<?php get_footer();?>

我沒有放入footer.php,因為您沒有必要看到它。

如果可以的話請幫幫我,我真的不知道從這里去哪里:(

在您的ajax.js中 ,第二行應該是

var href = $('.recentPost a').each(function(){ });

因為帶有類標題的div不包含所需的href鏈接。 進行更改並檢查是否有效。

並且click函數應該是類nextPost上的標題,而不是標題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM