首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress文章类别Ajax URL结构

Wordpress文章类别Ajax URL结构
EN

Stack Overflow用户
提问于 2017-10-25 13:59:42
回答 1查看 454关注 0票数 0

我有一个基本的wordpress博客,它有类别,当点击类别时,文章过滤器。这很好,但理想情况下,我希望在单击类别时将URL更改为

代码语言:javascript
复制
http://site.preview/resources/CATEGORYNAME

当前,博客以http://site.preview/resources/的形式加载,当单击类别时,它将更改为http://site.preview/resources/#

希望我解释得对。请在下面找到我的密码。

Home.php -博客

代码语言:javascript
复制
<?php get_header('home'); ?>
<div class="banner">
    <div class="in">
        <img src="<?php echo get_stylesheet_directory_uri() . '/assets/images/blue-cover-with-illustrations.png' ?>">
        <div class="banner__content">
            <div class="in">
                <h1>The Good Air Resources Center</h1>
                <h2>Knowledge to figure out indoor air quality. Tips to breathe cleaner air</h2>
            </div>
        </div>
    </div>
</div>
<section class="blog">
    <div class="in">
        <?php
        $categories = get_categories(); ?>
            <div class="categories">
                <div class="in">
                    <h5>Categories</h5>
                    <ul id="category-menu">
                        <?php foreach ( $categories as $cat ) { ?>
                        <li id="cat-<?php echo $cat->term_id; ?>">
                            <a class="<?php echo $cat->slug; ?> ajax" onclick="cat_ajax_get('<?php echo $cat->term_id; ?>');" href="#">
                                <?php echo $cat->name; ?>
                            </a>
                        </li>
                        <?php } ?>
                    </ul>
                </div>
            </div>
            <div class="posts">
                <div class="in">
                    <?php
                    $args = array(
                        'post_type' => 'post',
                        'orderby' => 'publish_date'
                    );
                    $post_query = new WP_Query($args);
                    if($post_query->have_posts() ) {
                    while($post_query->have_posts() ) {
                    $post_query->the_post();
                    ?>
                        <div class="post">
                            <div class="in">
                                <div class="post__image">
                                    <?php the_post_thumbnail(); ?>
                                </div>
                                <div class="post__title">
                                    <h4>
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_title(); ?>
                                        </a>
                                    </h4>
                                </div>
                                <div class="post__content">
                                    <?php the_excerpt(); ?>
                                </div>
                                <div class="post__readmore">
                                    <a href="<?php the_permalink(); ?>" class="btn btn--brand btn--block">Read More ></a>
                                </div>
                            </div>
                        </div>
                        <?php } } ?>
                </div>
            </div>
            <div id="loading-animation" style="display: none;">
                <img src="<?php echo admin_url ( 'images/loading-publish.gif' ); ?>" />
            </div>
    </div>
</section>
<script>
    function cat_ajax_get(catID) {
        jQuery("a.ajax").removeClass("current");
        jQuery("a.ajax").addClass("current");
        jQuery("#loading-animation-2").show();
        var ajaxurl = '/wp-admin/admin-ajax.php';
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                "action": "load-filter",
                cat: catID
            },
            success: function (response) {
                jQuery(".posts .in").html(response);
                jQuery("#loading-animation").hide();
                return false;
            }
        });
    }
</script>
</article>
<?php get_footer('home'); ?>

Functions.php

代码语言:javascript
复制
add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
function prefix_load_cat_posts () {
    $cat_id = $_POST[ 'cat' ];
         $args = array (
        'cat' => $cat_id,
        'order' => 'DESC'

    );
    $posts = get_posts( $args );
            global $post;
    ob_start ();
    foreach ( $posts as $post ) {
    setup_postdata( $post ); ?>
<div class="post">
    <div class="in">
        <div class="post__image">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="post__title">
            <h4>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </h4>
        </div>
        <div class="post__content">
            <?php the_excerpt(); ?>
        </div>
        <div class="post__readmore">
            <a href="<?php the_permalink(); ?>" class="btn btn--brand btn--block">Read More ></a>
        </div>
    </div>
    </div> 
    <?php } wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();

echo $response;
die(1);
}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-10-25 14:21:56

为此,您可以使用window.history.pushState函数。在ajax成功部分中的函数php中,您可以添加它。

代码语言:javascript
复制
success: function (response) {
                jQuery(".posts .in").html(response);
                jQuery("#loading-animation").hide();
                window.history.pushState("category"+catID, "Category"+catID, "http://site.preview/resources/"+catID);
                return false;
            }

如果它对您有效,那么在我给定的示例中,您知道如何将catID更改为类别名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46934463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档