【问题标题】:How to reload only posts in Wordpress如何在 Wordpress 中仅重新加载帖子
【发布时间】:2017-11-13 07:42:36
【问题描述】:

这个链接是我为这个问题找到的唯一解决方案...... https://premium.wpmudev.org/blog/load-posts-ajax/

在我的 Wordpress 主题档案中 - 页眉、页脚、侧边栏始终相同。只有帖子因标签或类别而异。

有没有更简单的方法让wordpress只加载帖子,例如: 当您点击导航(类别名称)时,它不会刷新整个页面,而是仅刷新帖子元素

我的存档页面需要此功能,因为我使用 archive.php 作为自定义类别页面设计的基础。

ps。我是 WP 初学者 :)

【问题讨论】:

标签: php ajax wordpress post reload


【解决方案1】:

您可以使用 admin-ajax 来获取解决方案。

HTML:

a class="category_nav" data-cat="此处传递类别值" href=""

例如,在function.php中创建一个函数:

function ajax_function() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
    jQuery(".category_nav").click(function() {
    var cat= jQuery(this).data("category"); // get the category of the post when clicked the navigation

    var data = {
        'action': 'ajax_perform.php',//create a php file to do the wp_query to get the result of passed category value.
        'postcat': cat
    };

    jQuery.post(ajaxurl, data, function(response) {

    });

    });
});
</script> <?php
}
add_action( 'admin_footer', 'ajax_function' );

【讨论】:

  • 感谢 cmets,我会试试这个,但不幸的是我现在无法测试它,因为我的截止日期已经到了,我必须专注于其他事情......我决定对这个wordpress进行硬编码,这样我就可以通过这个里程碑......然后我将有时间研究动态解决方案:/我完成后会在这里发布我的结果(我想如果给定时间我可以解决它)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多