Мой прием с использованием (слишком много?) словарей:
def serialize(_query):
#d = dictionary written to per row
#D = dictionary d is written to each time, then reset
#Master = dictionary of dictionaries; the id Key (int, unique from database) from D is used as the Key for the dictionary D entry in Master
Master = {}
D = {}
x = 0
for u in _query:
d = u.__dict__
D = {}
for n in d.keys():
if n != '_sa_instance_state':
D[n] = d[n]
x = d['id']
Master[x] = D
return Master
Работа с флягой (включая jsonify) и flask_sqlalchemy для вывода на печать в виде JSON.
Вызвать функцию с помощью jsonify (serialize ()).
Работает со всеми запросами SQLAlchemy, которые я пробовал до сих пор (работает SQLite3)
(Пожалуйста, прочитайте // {comment here}
и просто сравните этот код с вашим, чтобы узнать, что изменилось.)
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
//'prev_next' => true,
);
// Note that I'm not overriding the global `$wp_query`.
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$counter = 0;
$is_paged = $my_query->is_paged;
$new_row = true;
while ($my_query->have_posts()) {
$my_query->the_post();
//echo $counter
?>
<?php if ( ! $is_paged && 0 === $counter ) { ?>
<div class="row">
<div class="col-12">
====== Post #<?php echo $counter + 1; ?>; ID: <?php the_ID(); ?> =====
<h3><?php the_title(); ?></h3>
</div>
</div>
<?php } else { ?>
<?php if($new_row) { ?>
<div class="row">
<?php } ?>
<div class="col-md-4 news-grid__article">
===== Post #<?php echo $counter + 1; ?>; ID: <?php the_ID(); ?> =====
<h4><?php the_title(); ?></h4>
</div>
<?php //$counter++;
if(
( ! $is_paged && $counter % 3 === 0 ) || // every 3rd post on page #1
( $is_paged && $counter % 3 === 2 ) || // every 3rd post on page #2, #3, etc.
( $counter === $my_query->post_count - 1 ) // the last post
) { ?>
</div>
<div class="clearfix"></div>
<?php $new_row = true;
} else {
$new_row = false;
} ?>
<?php } $counter++; ?>
<?php } } ?>
<div class="news-pagination">
<?php
echo paginate_links( array(
'prev_next' => true,
// Here's how you can make `paginate_links()` works without having
// to override the `WP_Query` global instance (i.e. `$wp_query`).
'total' => $my_query->max_num_pages,
'current' => $my_query->get( 'paged' ),
) );
?>
</div>