$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - load more button in plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - load more button in plugin

matteradmin10PV0评论

I wrote a simple plugin which creates a post type named cryptoevent and also creates a metabox that have some custom fields, I display this post type and fields with WP_Query, now I want to display them with a load more button, note this is supposed to be a plugin and I don't want user to have to do changes in his/her theme's function.php, here is my code, thank u for your help. and I'm a beginner, by the way, please help me in a simple language.

function cryptoevent_addform( $atts ) {

    $args = array( 'post_type' =>'cryptoevent','posts_per_page'=>8);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        echo '<div style="padding:0 15%;"><h2>';
        the_title(); ?>
        <p><?php echo get_post_meta( get_the_ID(), 'date_add', true ); ?></p>
        <p><?php echo get_post_meta( get_the_ID(), 'date_expiration', true ); ?></p>


        <?php
        echo '</h2></div>';
    endwhile;
    wp_reset_query();
}
add_shortcode( 'cryptoevent', 'cryptoevent_addform' );

I wrote a simple plugin which creates a post type named cryptoevent and also creates a metabox that have some custom fields, I display this post type and fields with WP_Query, now I want to display them with a load more button, note this is supposed to be a plugin and I don't want user to have to do changes in his/her theme's function.php, here is my code, thank u for your help. and I'm a beginner, by the way, please help me in a simple language.

function cryptoevent_addform( $atts ) {

    $args = array( 'post_type' =>'cryptoevent','posts_per_page'=>8);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        echo '<div style="padding:0 15%;"><h2>';
        the_title(); ?>
        <p><?php echo get_post_meta( get_the_ID(), 'date_add', true ); ?></p>
        <p><?php echo get_post_meta( get_the_ID(), 'date_expiration', true ); ?></p>


        <?php
        echo '</h2></div>';
    endwhile;
    wp_reset_query();
}
add_shortcode( 'cryptoevent', 'cryptoevent_addform' );
Share Improve this question asked Feb 10, 2019 at 6:31 hessam hosseinipourhessam hosseinipour 191 silver badge8 bronze badges 2
  • Set the WP_DEBUG true in wp-config.php file and put the screenshot of the error. – Sagar Bahadur Tamang Commented Feb 10, 2019 at 6:33
  • thank for reply, this code works fine, i just want to display it with load more button – hessam hosseinipour Commented Feb 10, 2019 at 6:58
Add a comment  | 

2 Answers 2

Reset to default 1

If you want it to work like a true "load more" button, then you are going to have to let the posts load in groups without the user changing pages. This means you have to use an AJAX method. This is much more complicated than the WordPress loop you have right now.

Overall it works like this, with your code split into 2 parts:

A. Code that displays results

  1. Request up to 10 results via AJAX (or another number)
  2. When they load, show them on the screen.
  3. If there are more results available, show a "load more" button
  4. If the user hits load more, go back to step 1

B. Code that fetches results

  1. Look at which results were requested (which page)
  2. Echo out the results

So your code that does A. above and your code that does B. above live in different places.

The A code is more or less what you have in the cryptoevent_addform function you wrote. It would be very different from what you have, but basically that is handling the front-end. The main difference is instead of getting the posts via WP_QUERY and PHP, it would be getting them via a JavaScript AJAX request, through the the WordPress AJAX system, to your B code.

The B code handles the back-end. It can also live in your plugin, but would need to be registered through the WordPress AJAX API.

Why do we have to do this? The normal flow of a page is (1) Backend (PHP/mySQL/etc), then (2) Frontend (HTML/JavaScript/etc.). But here we have to flip the order. You are letting a user (front-end) fetch posts from your database (back-end), without them having to reload a page. So to flip the order, we have to separate your code into the 2 parts and then use AJAX to start the page flow over.

You'll want to look at how to structure a WordPress AJAX call, which is more than we can cover in a Stack Exchange answer. See the documentation.

i used paginate instead, and use the code below

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>'cryptoevent', // your post type name
'posts_per_page' => 3, // post per page
'paged' => $paged,
 ));
if($data->have_posts()) :
while($data->have_posts())  : $data->the_post();
the_title(); 
php
endwhile;
?><br><?php
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
    $current_page = max(1, get_query_var('paged'));
    $big = 99999; // need an unlikely integer
    echo paginate_links(array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »'),
    ));
}
?>
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();

it works fine if i set permalinks on plain, but my site permalinks are set on post name, please help me, thank you so much.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far