4

まず、これは大穴だと思いますが、助けてくれる人がいることを願っています。

現在の状況を説明すると、現時点では、ユーザーとその 4 つの最新の投稿に関するさまざまな情報を取得するカスタム プラグインがあります。

私はWPBookプラグインも使用しています(これはFacebook上にあり、単なる典型的なワードプレスサイトです)

ユーザーの最新の 4 つの投稿を取得するコードは次のとおりです。

// The Query
query_posts('posts_per_page=4&author='.$blogger_id);

// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;

// the Loop
while (have_posts()) : the_post();
?>
    <div class="oe-grid-box">
        <a href="<?php the_permalink() ?>" <?php the_title()?></a>
        <div class="oe-grid-box-content">
            <?php echo the_content( '...' ); ?>
        </div>
        <div class="oe-grid-pic">
            <a href="<?php the_permalink() ?>">
                <span class="<?php echo strtolower($category); ?>"></span>
            </a>
            <?php echo $image; ?>
        </div>
    </div>
<?php
endwhile;

// Reset Query
wp_reset_query();
?>


<div id="load-more">Load More</div>

このチュートリアルに従ってみましたが、別のプラグインではなく、既存のプラグインにコードを配置しましたが、ページが読み込まれません:

http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/

理想的には、もっと読み込むボタンがクリックされたときに、さらに4つの投稿を取得して表示したいです。

誰かが助けてくれれば、本当に感謝しています。

アップデート

Ok、

これまでのところ、うまくいけば投稿を返すはずのphpファイルを呼び出すためにjQueryに追加しましたが、機能しません。

WordPressの機能が何かわからないからだと思いますか?

load more スクリプトに単純なechoものを入れると、jQuery の成功関数はそれが機能したことを示すアラートを表示しますが、WordPress の処理を​​開始すると内部サーバー エラーが発生します。これが load-more.php ファイルのコードです。

// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');


$blogger_id = $_GET['id'];
$firstname  = $_GET['firstname'];
$surname    = $_GET['surname'];
$posts      = $_GET['posts'] + 4;
$category   = $_GET['category'];
$image      = $_GET['image'];

// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);

// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;

// then the same loop as in my page that is making the ajax call.
4

1 に答える 1

15

たくさんの努力の末、私は答えを見つけました。これが同じ立場で立ち往生している人々のための解決策です。

これは、ユーザーなどの投稿を取得しているプラ​​グインページに表示されます。

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

        posts = 8;
        author_posts = parseInt(<?php echo $author_posts; ?>);

        $("#link_selector").click(function() {

            // alert('posts - ' + posts + 'author posts - ' + author_posts);


            if ((posts - author_posts) > 3) {
                $("#link_selector").text('No more posts!');
            }
            else {

                var category        = '<?php echo strtolower($category); ?>';
                var id              = '<?php echo $blogger_id; ?>';
                var firstname       = '<?php echo $firstname; ?>';
                var surname         = '<?php echo $surname; ?>';


                // alert(posts + category + id + firstname + surname);

                $.ajax({
                    type: "GET",
                    url: "/wordpress/wp-admin/admin-ajax.php",
                    dataType: 'html',
                    data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
                    success: function(data){
                        $('#ajax_results_container').hide().fadeIn('slow').html(data);
                        posts = posts + 4;

                        if ((posts - author_posts) > 3) {
                            $("#link_selector").text('No more posts!');
                        }

                    }
                });
            }

        });
    });

    </script>       

次に、テーマのfunctions.phpに、ajaxリクエストを実行する関数を配置します。

function loadMore() {

    $blogger_id = $_GET['id'];
    // get your $_GET variables sorted out

    // setup your query to get what you want
    query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);

    // initialsise your output
    $output = '';

    // the Loop
    while (have_posts()) : the_post();

        // define the structure of your posts markup

    endwhile;

    // Reset Query
    wp_reset_query();

    die($output);

}

次に、関数を閉じた直後に、アクションフックを配置します

add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed

それでおしまい!

于 2012-08-10T14:02:19.503 に答える