George_Fal 2014-01-02 16:39 采纳率: 0%
浏览 28

WordPress:通过Ajax获取条款

I'm new in wordpress, but i need to collect all photos in posts, and group by categories. My Js is

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 :

<?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. But when i using it via ajax, im getting only some photos, and if i call var_dump($tax_terms); the result is

object(WP_Error)#4418 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1)

What should i include to use terms?

  • 写回答

1条回答 默认 最新

  • lrony* 2014-11-25 21:07
    关注

    You should use wp_ajax in the first place, and then you can skip the first require.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办