【问题标题】:Using Advanced Custom Fields Gallery with Media Categories and Ajax将高级自定义字段库与媒体类别和 Ajax 一起使用
【发布时间】:2017-03-25 20:56:24
【问题描述】:

问题:我使用高级自定义字段创建了一个图库。我添加到图库中的所有图像我都给出了自定义分类(类别),例如“时尚”、“肖像”等。我在画廊上方列出了所有术语。我希望能够单击一个术语,例如时尚,并将所有带有“时尚”一词的图像加载到下面的图像容器中。类似于https://demo.kaliumtheme.com/photography/。我坚持让图像在单击一个术语时加载。你能帮忙吗?

这是画廊的代码:

        <?php $images = get_field('gallery');

        if( $images ): ?>
            <ul><div class="image__sizer"></div>
                <?php foreach( $images as $image ): $link = get_field('link', $image['ID']); ?>
                <?php $terms = get_the_terms( $image['ID'], 'category_media' ); ?>
                <?php if ( !empty( $terms ) ) : $term = array_shift( $terms ); ?>
                    <li class="<?php echo $term->term_id; ?>">
                        <div data-200-bottom="top: 0px;opacity:0%;" data--200-top="top:-250px;opacity:100%;" class="image__wrap">
                        <a href="<?php echo $image['url']; ?>">
                             <img src="<?php echo $image['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
                        </a>
                        <div class="image__meta">
                            <span><?php echo $term->name; ?> - <?php echo $image['title']; ?></span> 
                        </div>
                        </div>
                    </li>
                <?php endif; ?>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>

自定义分类称为“category_media”。我希望使用下面这样的函数:

 function prefix_load_term_posts () {
    $term_id = $_POST[ 'term' ];
        $args = array (
        'term' => $term_id,
        'posts_per_page' => -1,
        'order' => 'DESC',
             'tax_query' => array(
              array(
                  'taxonomy' => 'category_media',
                  'field'    => 'id',
                  'terms'    => $term_id,
                  'operator' => 'IN'
                  )
              )
         );

    global $post;
    $myposts = get_posts( $args );
    ob_start ();  ?>

    <?php foreach ($myposts as $post):
    echo $images = get_field('gallery', $post->ID); ?>
    <?php endforeach; ?>

   <?php wp_reset_postdata(); 
   $response = ob_get_contents();
   ob_end_clean();
   echo $response;
   die(1);
    }

使用这个 JQuery 函数:

<script>
function term_ajax_get(termID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
jQuery('.image-container ul li').fadeOut();
jQuery("#loading-animation").show();
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); //must echo it ?>';
jQuery.ajax({
    type: 'POST',
    url: ajaxurl,
    data: {"action": "load-filter2", term: termID },
    success: function(response) {
        jQuery(".image-container ul").html(response);
        return false;
    }
});
}
</script>

但是,没有图像加载到容器中。谁能帮忙告诉我我错过了什么?不胜感激,谢谢!

【问题讨论】:

    标签: php jquery ajax wordpress gallery


    【解决方案1】:

    您的问题没有被理解 尝试提供更多细节并缩短代码 不要让所有的代码都告诉你你有一个特定的问题

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2013-11-25
      相关资源
      最近更新 更多