开发者

jQuery with Wordpress Loop. Show / Hide content?

At the moment I have a website that grabs the 12 most recent posts from a category, and displays them as a link to their permalink, with the post thumbnail image as the link image.

You can see a working example here: http://mathewhood.com/sitefiles/

What I want to do, is somehow add functionality into my loop that will allow me to make it so that when you click on one of these list elements, it will display the_content(); for each element.

I found this - http://gsgd.co.uk/sandbox/jquery/easing/ Which I think may provide the functionality that I want (ideally jswing in and out), but I am struggling at implementing it! If anyone knows how I could make thi开发者_如何学Pythons happen please answer this and receive your well deserved up-votes!

http://jsfiddle.net/XzJgU/ - Here is my current loop so far, any help will be greatly appreciated!!!!!!!!!


Here is my solution to your problem. I'm giving an example on how to implement jquery Easing.

EDIT revised my post, please View revised sample here Hope it helps.

$('.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:100}, {duration: 1000, easing: 'easeInOutCirc'});
    $container.click(function(){
        $container.animate({height:200}, {duration: 1000, easing: 'easeInExpo'})
        $container.fadeOut('slow');
        $container.html('');
    });
});


Something like that should work - http://jsfiddle.net/XzJgU/5/. It renders the content for each post in the loop, hidden by default using CSS. When a post is clicked, it moves its content to #displayed-post, making it visible. When another post is clicked, its moves back to its original container and the new post content is moved there.


I'm not entirely clear on how you want this to work - are you looking for a PHP solution or a JavaScript one or perhaps a mix of the two. I've got two suggestions on how you might make it work. Also, note that the jQuery library you're referring only adds easing options to jQuery - i.e. it only deals with animation and not with the business logic and behaviour of your code.

  1. Using ajax
    This should work in this case since you're not making cross-domain requests. Essentially, you'd intercept the click to the link, figure out where it's pointing and then make a GET request to that page. You'd then filter out the appropriate HTML from the response and put it into your page. Something like this:

    $('.recentPost a').click(function(){
        $.get($(this).attr('href'), function(data){
            //make a get request to the page the link linked to
            //and extract the blog content div
            $("placeholder").html($(data).filter(".blogRight"));
        });
        return false; //cancel the browser's default action on click to keep user on page
    });
    

    where you'd have a <div id="placeholder" /> in your HTML page where you'd like the content to appear.

  2. Using PHP + JavaScript
    Instead of fetching the content on demand, you'd generate it when the page loads but keep it hidden. You'd again intercept clicks but this time you'd find and display the appropriate, existing div on the page.

    So your generated page would look something like this:

    <div id="contentWrap">
            <div class="hidden-featured-content" id="content-f12">
                <div>Your content here</div>
            </div>
            <div class="hidden-featured-content" id="content-f11">
                <div>Your content here</div>
            </div>
    
            <div id="newBanner"></div>
    
            <div class="recentPost">
                <a href="http://mathewhood.com/sitefiles/?p=35" id="link-f12"><img width="204" height="144" src="http://mathewhood.com/sitefiles/wp-content/uploads/2011/08/featured12.png" class="attachment-post-thumbnail wp-post-image" alt="featured12" title="featured12" /></a>
                <a href="http://mathewhood.com/sitefiles/?p=35"><div class="caption">
                    <div class="captionTitle">Hello World 12!</div>
                    <p></p>
                </div></a>
            </div>
    
            <div class="recentPost">
                <a href="http://mathewhood.com/sitefiles/?p=32" id="link-f11"><img width="204" height="144" src="http://mathewhood.com/sitefiles/wp-content/uploads/2011/08/featured11.png" class="attachment-post-thumbnail wp-post-image" alt="featured11" title="featured11" /></a>
                <a href="http://mathewhood.com/sitefiles/?p=32"><div class="caption">
                    <div class="captionTitle">Hello World 11!</div>
                    <p></p>
                </div></a>
            </div>
         ...
    

    You could then use something like to toggle the appropriate content

    $('.recentPost a').click(function(){
        if($(this).attr('id')){
            var x = /link-(.*)/.exec($(this).attr('id')); //figure out which content- div to display
            $('.displayed').hide().removeClass('displayed'); //hide already displayed content
            $('#content-' + x[1]).show().addClass('displayed'); //show content and mark it as displayed
            return false;
        }
    });
    


There are a number of ways to achieve this. The most efficient would probably be a full ajax solution but that would require a Wordpress plugin and some advanced scripting.

The most straightforward solution would be to add a box for dynamic content, output the content for each post in a hidden DIV under it's permalink/image, then use javascript to move content from the hidden DIVs to the dynamic content box when a permalink is clicked. I've worked up some code at http://jsfiddle.net/HF9Pr/.


Try this:

<head>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
$(".flip").click(function(){
    $(".panel").slideToggle("slow");
  });
});
</script>

<style type="text/css"> 
div.panel,p.flip
{
width: 203px;
margin: 0px;
padding-top: 9px;
padding-bottom: 9px;
padding-left: 12px;
padding-right: 12px;
background: #c1e0fb;
border-left: 1px dashed #aaa;
border-right: 1px dashed #aaa;
}
div.panel
{
height: 288px;
display: none;
border-top: 1px dashed #aaa;
}
p.flip
{
border-top: 1px dashed #aaa;
border-bottom: 1px dashed #aaa;
}
</style>
</head>

<body>

<div class="panel">
<b>sclughslru</b>
<br><br>
ertljhvij3p
<br><br>
<b>veuywihtp</b>
<br><br>
ghdjhrntnd
<br><br>
<b>ehv3rtv</b>
<br><br>
vt4k4kb76kb5
<br><br>
<b>edb3jrr3n54</b>
<br><br>
skcehgkheone
</div>

<p class="flip"><img src="https://dl-web.dropbox.com/get/Pictures/Other/up-down.jpg?w=f17ee285" style="width: 12px; height: 10px; margin-right: 10px;" />Information</p>

</body>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜