Example of randomly retrieving a record using AJAX in wordpress

Source: Internet
Author: User

Click "change" to randomly change the content.

Because there are "standard", "image", "video" and other article forms, here we need to randomly obtain an article in the form of images, and take out the pictures, and links, use ajax to replace the content.

① We can create a new page template named recommend.

Create a page Template:

<? Php
/*
* Template Name: change the Name.
*/
Create a page in the background management, select this template, and name it recommend. A new page is created.

② Obtain and output the content to be replaced in the template

The code is as follows: Copy code
<? Php
/*
* Template Name: change the Name.
*/Www.111cn.net
$ Args = array (
'Tax _ query' => array (
Array (
'Showposts' => '1 ',
'Taxonomy' => 'Post _ format ',
'Field' => 'Slug ',
'Terms' => 'Post-format-image ',
    )
),
'Orderby' => 'Rand ',
);
Query_posts ($ args );
The_post ();
?>

This is a random article in the form of images.

The code is as follows: Copy code
<A href = "<? Php the_permalink ();?> "Target =" _ self ">
<? Php $ imgurl = catch_that_image ();?>
"Alt =" N amazing ways to get people up "width =" 270 "height =" 152 "/>
<Span> <? Php the_title ();?> </Span>
</A>

Here is the html to be displayed. We have replaced the link and image address.

Catch_that_image function:

The code is as follows: Copy code
<? Php
Function catch_that_image (){
Global $ post, $ posts;
$ First_img = '';
Ob_start ();
Ob_end_clean ();
$ Output = preg_match_all ('//I ', $ post-> post_content, $ matches );
$ First_img = $ matches [1] [0];
 
// No image found display default image instead
If (empty ($ first_img )){
$ First_img = "/images/default.jpg ";
 }
Return $ first_img;
}
?>

This function can be used to retrieve the first image of the modified article.

③ Write ajax functions on the front-end page

The code is as follows: Copy code
<Script>
$ (Function (){
$ (". Featured-random"). load ("/recomend /");
});
 
Function recommend (){
$ (". Featured-random"). load ("/recomend /");
Return false;
 }
</Script>
 
<Div class = "featured sidebox radiusbox">
<H3 class = "side-title"> recommended content <Div class = "featured-random">
 
</Div>
<A class = "refresh-random" href = "javascript: void (0)" onclick = "recommend ()"> Change One </a>
</Div>

In this way, the ajax effect will be completed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.