个性化阅读
专注于IT技术分析

如何加载WordPress分类标准列表并使用Ajax onclick发布

点击下载

很抱歉长时间阅读…

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

我一直在看这个网站上的脚本(如何使用Ajax onclick加载Wordpress Post)。

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;
    });
});

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

<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中打开此分类法内容…

谢谢!


#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;

}

就是这样…我很高兴!

赞(0)
未经允许不得转载:srcmini » 如何加载WordPress分类标准列表并使用Ajax onclick发布

评论 抢沙发

评论前必须登录!