简体   繁体   English

WordPress:通过Ajax获取条款

[英]Wordpress: get terms by ajax

I'm new in wordpress, but i need to collect all photos in posts, and group by categories. 我是wordpress的新手,但我需要收集帖子中的所有照片,并按类别分组。 My Js is 我的Js是

function get_photos(elem)
{

$.ajax({  
cache: true,  
    type: "GET",  
    timeout: 20000,   
    url: 'wp-content/themes/wp_theme/photos.php',  
    success: function(msg)  
    {  
        $(elem).append(msg);
    },  
    error: function(msg)  
    {  
            get_photos(elem);
            return false;
    }  
});
}

And the photos.php is the : photos.php是:

<?php
require('../../../wp-load.php');
$tax_terms = get_terms('media_category', 'orderby=count&order=DESC&hide_empty=0');

foreach ( $tax_terms as $tax_term ) {
?>
<div class="news">
    <img src="./wp-content/themes/wp_theme/img/plus.png" class="plus">
    <div class="titleNews2"><?php echo $tax_term->name; ?></div>
</div>
<?php

$posts = get_posts(array(
                   "post_type" => "attachment",
                   "post_mime_type" => "image",
                   "taxonomy" => $tax_term->taxonomy,
                   "term" => $tax_term->slug,
                   "numberposts" => 100, 
                   "posts_per_page" => 100));
?>
<div class="photoRace">
<?php
$ua = @getenv( HTTP_USER_AGENT );
$touchPadApple = stripos( strtolower( $ua ), "iphone" ) !== false || stripos( strtolower( $ua ), "ipad" ) !== false ? true : false;
foreach($posts as $post){

setup_postdata($post);
$img = get_post_meta($post->ID, "_wp_attachment_metadata", true);
$dir = explode("/", $img['file']);
$link = get_bloginfo('siteurl')."/wp-content/uploads/{$dir[0]}/{$dir[1]}/";
?>

<a <?php echo !$touchPadApple ? "rel=\"photos\" " : "target=_BLANK "; ?>href="<?php echo get_bloginfo('siteurl')."/wp-content/uploads/".$img['file'];?>">
<img src="<?php echo $link.$img['sizes']['thumbnail']['file'];?>" height="<?php echo $img['sizes']['thumbnail']['height']; ?>" width="<?php echo $img['sizes']['thumbnail']['width']; ?>">
</a>

<?php } ?>
</div>
<?php } ?>

Im working on mobile interface, and this script (not via ajax) works well on non-mobile static pages of theme. 我正在移动界面上工作,该脚本(不通过Ajax)可在主题的非移动静态页面上很好地工作。 But when i using it via ajax, im getting only some photos, and if i call var_dump($tax_terms); 但是当我通过ajax使用它时,我只会得到一些照片,如果我调用var_dump($ tax_terms); the result is 结果是

object(WP_Error)#4418 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) object(WP_Error)#4418(2){[“”错误“] =>数组(1){[”“无效分类法”] =>数组(1)

What should i include to use terms? 我应该使用什么条款?

首先应该使用wp_ajax ,然后可以跳过第一个要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM