我正在开发一个项目,该项目会增加一个"加载更多"使用ajax将帖子发布到我的网站。
如果没有ACF我的通话工作完美,我可以将我的所有博客文章数据都很好,但是我使用ACF来设置帖子的样式,因为我使用了一些if语句,我的样式是"隐藏"由ajax没有将数据传递给ACF字段(我认为......?)
如何让ajax正确加载我的acf数据。
任何帮助都会很棒!
/**
* Ajax handler for load more posts
*/
public function load_more_posts() {
$location = $_SERVER['DOCUMENT_ROOT'];
require ($location . '/emerald/wp-load.php');
if ( empty( $_POST['nonce'] ) || empty( $_POST['paged'] ) || ! wp_verify_nonce( $_POST['nonce'], 'load-more-posts-nonce-' . $_POST['context'] ) ) {
exit;
} else {
global $post; // required by setup post data
$context = ( ! empty( $_POST['context'] ) ) ? sanitize_text_field( $_POST['context'] ) : 'default';
$args = (array) $_POST['query'];
$args['paged'] = sanitize_text_field( $_POST['paged'] );
// A filter if you want to customize the query
$args = apply_filters( 'load-more-posts-args-' . sanitize_text_field( $_POST['context'] ), $args );
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
setup_postdata( $post );
get_template_part('page-builder/layouts/ajax/recent-posts', $context);
wp_reset_postdata();
}
}
exit;
}
}
new Load_More_Posts();
添加了更多功能: -
<?php
$post_id = get_the_ID();
echo "<div";
css_class();
echo ">";
// Params
if ( have_rows( 'card_list_options', $post_id ) ) :
while ( have_rows( 'card_list_options', $post_id ) ) : the_row();
if ( have_rows( 'card_options', $post_id ) ) :
while ( have_rows( 'card_options', $post_id ) ) : the_row();
$title_check = get_sub_field( 'show_title', $post_id );
$date_check = get_sub_field( 'show_date', $post_id );
$cat_check = get_sub_field( 'show_category', $post_id );
$exc_check = get_sub_field( 'show_excerpt', $post_id ) ;
$tags_check = get_sub_field( 'show_tags' );
$button_check = get_sub_field( 'show_read_more', $post_id );
$alt_thumb_Check = get_sub_field( 'use_alternative_featured_image', $post_id );
endwhile;
endif;
if ( have_rows( 'card_style', $post_id ) ) :
while ( have_rows( 'card_style', $post_id ) ) : the_row();
$title_size = get_sub_field( 'title_size', $post_id );
$readmore_style = get_sub_field( 'read_more_button_style', $post_id );
$button_text = get_sub_field( 'read_more_button_text', $post_id );
endwhile;
endif;
$card = get_sub_field('card_style', $post_id);
$card_options = get_sub_field('card_options', $post_id);
if ( have_rows( 'source', $post_id ) ) :
while ( have_rows( 'source', $post_id ) ) : the_row();
$post_count = get_sub_field('show_x_posts', $post_id);
$post_type = get_sub_field('select_post_type', $post_id);
$post_offset = get_sub_field('select_post_offset', $post_id);
endwhile;
endif;
endwhile;
endif;
// WP_Query arguments
$args = array(
// 'nopaging' => false,
'posts_per_page' => $post_count,
'post_type' => array( $post_type ),
//'page_id' => $post_id,
//'paged' => 1,
// 'paged' => $paged,
'offset' => $post_count,
);
// The Query
$wp_query = new WP_Query( $args );
// Adds customisation options + container class for internal div
echo '<div';
css_class();
echo '>';
echo '<ul id="inf_scroll" class="list-unstyled row card_list">';
$link = get_permalink();
// start li
echo '<li class="news p-b-sm';
if ( have_rows( 'card_list_options' ) ) :
while ( have_rows( 'card_list_options' ) ) : the_row();
if ( have_rows( 'card_layout' ) ) {
$col_size = array();
while ( have_rows( 'card_layout') ) : the_row();
$col_size [] = get_sub_field('lg');
$col_size [] = get_sub_field('md', $post_id);
$col_size [] = get_sub_field('sm', $post_id);
$col_size [] = get_sub_field('xs', $post_id);
endwhile;
foreach ($col_size as $col) {
echo $col . ' ' ;
}
}
endwhile;
endif;
echo '">';
echo '<article class="white-background">';
//check if element has a thumbnail
//if not the replace the thumbnail with a category link
if ( $alt_thumb_Check == 1 ) {
$add_alt_feature_image = get_field( 'add_alt_feature_image' );
if ( $add_alt_feature_image ) {
echo '<a class="greyscale" href="' . $link . '">';
echo '<img class="img-fluid" src="'. $add_alt_feature_image['url'] . '" alt="'. $add_alt_feature_image['alt'] .'"/>';
echo '</a>';
}
} else {
echo '<a href="' . $link . '">';
echo get_the_post_thumbnail($post->ID, 'post_thumbnail', array( 'class' => 'img-fluid p-b-sm' ));
echo '</a>';
}
echo '<span class="cardbody">';
$title = get_the_title();
if ($title_check == 1) {
echo '<a href="' . $link . '">' . '<' . $title_size . '>' . $title . '</' . $title_size . '>' . '</a>';
}
// Control to show/display the post date
if ($date_check == 1) {
$date = get_the_date();
echo '<small class="p-b-sm">' . $date . '</small>' ;
}
if ( $cat_check == 1 ) {
the_category();
}
// Display the post excerpt
if ($exc_check == 1) {
the_excerpt($post->ID);
}
// Display post tags
if ($tags_check == 1) {
if (get_the_tag_list()) {
echo '<p><small>tags:</small></p>';
echo get_the_tag_list('<ul class="tag_list"><li>', '</li><li>', '</li></ul>');
}
}
echo "</span>";
if ($button_check == 1) {
echo '<a class="m-t-sm readmore '. $readmore_style .'" href="' . $link . '">' . $button_text . '</a>';
}
//end article
echo "</article>";
// end li
echo "</li>";
echo "</ul>";
echo '<div id="inf_load_footer"></div>';
echo "</div>";
更新以添加最近的帖子模板: -
struct S {
S() {}
template<typename B>
struct T {
T(B &&) {}
};
template<typename B>
T(B &&) -> T<B>;
};
int main() {
S::T t{0};
}