javascript - Wordpress:通过 ajax 获取条款

标签 javascript php jquery ajax wordpress

我是 wordpress 的新手,但我需要收集帖子中的所有照片,并按类别分组。我的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;
    }  
});
}

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 } ?>

我在移动界面上工作,这个脚本(不是通过 ajax)在主题的非移动静态页面上运行良好。但是当我通过 ajax 使用它时,我只得到一些照片,如果我调用 var_dump($tax_terms); 结果是

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

我应该包括哪些内容才能使用术语?

最佳答案

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

关于javascript - Wordpress:通过 ajax 获取条款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20887448/

相关文章:

javascript - 在 IE 10 中使用复合键创建索引时出现 DataError

javascript - 如何在javascript中编写简单的异步函数

php - 使用来自 ManyToMany 关联的值过滤具有 NOT EXISTS 条件的 DQL 查询

javascript - 使用 Jquery 防止在 html() 期间创建 html 标签

javascript - 如何更改 jsTree 节点的文本?

javascript - 原始 html 渲染与动态 JS 渲染的间距不同

javascript - 删除修改数据的表行

javascript - 为什么 fetch 返回状态为 0 的响应?

php - 如何更改结果集中的列名称并使用修改后的列名称在 PHP 中创建新结果集

php - 如何使用ajax显示值