【问题标题】:How to load Wordpress Taxonomy list and Post with Ajax onclick如何使用 Ajax onclick 加载 Wordpress 分类列表和发布
【发布时间】:2015-12-14 09:27:45
【问题描述】:

抱歉读了这么久……

我希望你们能帮助我。我试图显示一个分类列表,该列表将在与主帖子页面相同的帖子中加载该分类的内容。

我一直在看这个脚本,在这个网站 (How to load Wordpress Post with Ajax onclick) 上找到。

Javascript:

jQuery.noConflict();
jQuery(document).ready(function($){
    $.ajaxSetup({cache:false});
    $("a.ajax").click(function(){
        var post_url = $(this).attr("href");
        var post_id = $(this).attr("rel");
        $("#tabs").html('<div class="loading">loading...</div>');
    $("#tabs").load(post_url);
    return false;
    });
});

我要显示帖子内容的页面(我正在使用称为“艺术品”的自定义帖子类型:

<ul class="container">
  <?php query_posts('post_type=artwork&posts_per_page=-1'); ?>
  <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
  <li class="mytab">
    <h3><?php the_title(); ?></h3>
    <a href="<?php the_permalink(); ?>" rel="<?php the_ID(); ?>" class="ajax"><?php the_post_thumbnail('Project'); ?></a>
  </li>
  <?php endwhile; endif; wp_reset_query(); ?>
</ul>

<!-- LOAD SINGLE POST CONTENT IN #TABS -->
<div id="tabs"></div>

还有单篇文章“single-artwork.php”。注意:不要使用 get_header 或 get_footer 等:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
  <div class="tabcontent" id="tab-<?php the_ID(); ?>">
    <?php the_content(); ?>
  </div>
<?php endwhile; endif; ?>

我没有使用脚本的最后一部分,因为我已经在 single-{slug}.php 中工作了

这实际上是在同一个窗口中打开分类内容,就像我想要的那样。但这会将缩略图显示为链接。

我真正想要的是只有分类列表,然后打开并显示#tabs div中的内容。

到目前为止,我得到了这个:

<a href="<?php the_permalink(); ?>" rel="<?php the_ID(); ?>" class="ajax"><?php echo get_the_term_list( $post->ID, 'inclusief'); ?></a>

它显示了我想要的分类列表。但这会在新页面上打开该分类的内容,而不是在#tabs 中。这是怎么回事……为什么。当然,我怎样才能让这个分类内容在#tabs div中打开......

谢谢!

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    好吧,我通过使用自定义 get_terms_list 函数修复了它。问题是 class="ajax" 没有提供给链接。通过创建一个将 ajax 类提供给链接的自定义函数。帖子在#tabs div 中打开。

    function get_the_term_list_custom( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
    $terms = get_the_terms( $id, $taxonomy );
    
    if ( is_wp_error( $terms ) )
        return $terms;
    
    if ( empty( $terms ) )
        return false;
    
    $links = array();
    
    foreach ( $terms as $term ) {
        $link = get_term_link( $term, $taxonomy );
        if ( is_wp_error( $link ) ) {
            return $link;
        }
        $links[] = '<a class="ajax" href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
    }
    
    /**
     * Filter the term links for a given taxonomy.
     *
     * The dynamic portion of the filter name, `$taxonomy`, refers
     * to the taxonomy slug.
     *
     * @since 2.5.0
     *
     * @param array $links An array of term links.
     */
    $term_links = apply_filters( "term_links-$taxonomy", $links );
    
    return $before . join( $sep, $term_links ) . $after;
    

    }

    就是这样……我很高兴!

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多