【问题标题】:Querying wordpress for different pages?查询不同页面的wordpress?
【发布时间】:2010-11-13 15:08:45
【问题描述】:

我有一个 wordpress 代码,它在工厂里运行得很好。这只是一个显示帖子的循环。它将作为分页系统的一部分工作,我认为这样做的唯一方法是运行一个查询,该查询将根据用户是否在一个类别中或在搜索页面上而改变,以便用户获得不同的集合帖子数。

if ( have_posts() ) : while ( have_posts() ) : the_post(); 
      // ....
endwhile; endif;

不幸的是,我不知道该怎么做。我尝试了各种各样的事情,但没有一个真正有助于这种情况。我似乎总是以所有帖子结束,而不是我正在查看的类别的帖子。我假设我应该使用 query_posts。 :(

分页是 AJAX,它将两个内容发布到 wordpress 循环文件,即偏移量和页码。它基本上检查用户何时向下滚动到页面底部并加载更多内容。供参考:

<script type="text/javascript">
$(document).ready(function() {

    var number = 10;
    var offset = 0;
    var page_number = 2;
    var busy = false;

    /* Bind the scroll function to an event */
    $(window).bind('scroll', function(e) {


        /* If the scroll height plus the window height is more than the document height minus 10, continue */
        if($(window).scrollTop() + $(window).height() > $(document).height() - 10 && !busy) {


            busy = true;    

            /* Quick message so you know more stuff is loading */
            $('.loading-more').html('Click to load more posts..');

            $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
                action: 'and_action',
                off: offset+number,
                pagenumber: page_number - 1
                }, function(data) {


                    offset = offset+number; 

                    $('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);


                    busy = false;
                    page_number += 1;


            });


        }

    });


    $('.loading-more').bind('click', function(e) {

            busy = true;    

            $('.loading-more').html('<em>Loading more posts..</em>')

            /* Quick message so you know more stuff is loading */               
            $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
                action: 'and_action',
                off: offset+number,
                data: data,
                pagenumber: page_number - 1
                }, function(data) {


                    offset = offset+number; 

                    $('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);


                    busy = false;
                    page_number += 1;

                    $('.loading-more').html('Click to load more posts..');

            });


    });


});
</script>

我也绑定了一个点击事件来备份。非常感谢任何帮助:)


更新

这是在functions.php中使用的PHP即时通讯

add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');

function and_action() {
    $query_string = $_POST['query_string'];
}

function get_posts_page() {

    global $wpdb;

    query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 


        ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
            <div class="entry-meta">

                <span class="%1$s">Posted on</span> <?php the_date('F jS'); ?>
                - <a class="comment-link" href="<?php the_permalink(); ?>#comment"><?php comments_number('Leave a Response!', '1 Response', '% Responses'); ?></a>
            </div><!-- .entry-meta -->
            <br />
            <a class="post-thumbnail-thing" href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>            
            <div class="entry-content">
                <?php the_content( __( '<span class="alignright">
                <span class="button-css">Continue Reading &rarr;</span> 
                 </span>', 'twentyten' ) ); ?><br /><hr />
                <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
            </div><!-- .entry-content -->
        </div><!-- #post-## -->

        <?php comments_template( '', true ); ?>

<?php       
    endwhile; endif;

    wp_reset_query();

    die();

}

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    在类别或搜索页面上使用query_posts 时,您需要确保包含$query_string 变量。 $query_string 变量包含诸如您所在的类别以及搜索词是什么等信息。

    <?php query_posts($query_string . '&posts_per_page=10') ?>
    

    更新

    嗯,看起来您的 javascript 是从页面的页脚运行的。因此,您可以将 $query_string 变量添加为 post 参数。

    footer.php

    <?php global $query_string; ?>
    
    ... 
    
    $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
        action: 'and_action',
        off: offset+number,
        pagenumber: page_number - 1,
    
        query_string: '<?php echo $query_string ?>'
        }
    

    functions.php

    function and_action() {
        $query_string = $_POST['query_string'];
    }
    

    更新 #2

    您的functions.php 文件应该像这样开始。而且您不必在页脚中全球化$query_string。您在header.php 中执行此操作的原因是因为 header.php 在函数get_header() 内运行

    add_action('wp_ajax_and_action', 'get_posts_page');
    add_action('wp_ajax_nopriv_and_action', 'get_posts_page');
    
    function get_posts_page() {
    
        $query_string = $_POST['query_string'];
    
        global $wpdb;
    
        query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);
    
        if ( have_posts() ) : while ( have_posts() ) : the_post();
    

    【讨论】:

    • 我已经尝试过了,但仍然没有骰子。我在我的functions.php文件中的一个函数中,我需要添加一个动作还是什么? ://
    • 还是什么都没有! :( javascript 在头脑中,我正在使用 add_filter('wp_head', 'javascript_page'); 另外我已经添加了你说要添加的所有内容。我可以用我正在使用的 PHP 更新我的答案,等等.
    • 哦,我很抱歉不清楚。您需要将我的函数 and_action 中的内容放入您的函数 get_posts_page。这样你的函数就可以访问变量 $query_string。
    • 好的,我检查了查询字符串。我在 header.php 文件和 footer.php 文件(分别)中将它设置为全局,它什么都不返回,就像只是 null 一样。任何想法为什么?
    • 不。查询字符串仍然为空。 :( 抱歉,如果我很麻烦,我很想解决这个问题。我认为的主要问题是查询字符串在 jQuery 中没有返回任何内容。所以我有 '' ,当我查看源代码时,它只显示为 ''。
    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多