Support Forum
Hi, i follow this topic: https://support.metabox.io/topic/show-pagination-for-query/
but can´t make it work.
this is my code:
{% set args = { post_type: 'frase-del-dia', posts_per_page: 1, orderby:'rand' } %}
{% set frases = mb.get_posts( args ) %}
{% for frase in frases %}
<a href="{{ mb.get_the_permalink( frase.ID ) }}">{{ frase.frase_del_dia }}</a>
<br>Temáticas: {{ mb.get_the_term_list( frase.ID, 'tematica', '', ', ' ) }}
{% set emp_args = { post_type: 'autor-de-frase', nopaging: true, relationship: {id: 'rel-frase-del-dia-con-autor', from: frase.ID} } %}
{% set autores = mb.get_posts( emp_args ) %}
{% for autor in autores %}
/ Autor: <a href="{{ mb.get_the_permalink( autor.ID ) }}">{{ autor.post_title }}</a>
( {{ autor.bio_del_autor_de_frase }} )
{{ mb.get_the_term_list( autor.ID, 'profesion-del-autor', '', ', ' ) }},
{{ mb.get_the_term_list( autor.ID, 'nacionalidad-del-autor', '', ', ' ) }}
{% endfor %}
<hr>
{% endfor %}
Can share some code to add a pagination? or how can i do that? Thanks!!
Hi,
If you want to use the custom query and show the pagination, I recommend using WP_Query via PHP code and call it in View via the proxy mb
. Follow this documentation to show the pagination https://developer.wordpress.org/reference/functions/paginate_links/#user-contributed-notes
Hi Long, thanks.
This is beyond my knowledge and I don't understand what I should do.
Would you be so kind as to share an example? I have seen that in some answers you record a step by step video. That would be really great.
I have seen what you recommend, for example:
<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
but I have no idea how to insert it into views with twig.
I hope you can help me with this.
Thank you very much.
Translated with http://www.DeepL.com/Translator (free version)
Hi,
You should have a basic knowledge about coding PHP, WordPress ... to do the advanced cases. I recommend creating a query by using PHP code first to understand how it works then you can wrap it in a function and call the function in View. For example:
function show_my_posttype() {
$my_query = new WP_Query();
while($my_query->have_posts()) {
// Loop in here
}
}
Call in View: mb.show_my_posttype()
Refer to this article https://www.smashingmagazine.com/2013/01/using-wp_query-wordpress/
Hi Long, thanks for the reply. You are right, it is an advanced case and exceeds my knowledge.
I have found a lot of help on the forum seeing answers to specific situations and I thought this would not be so difficult to solve.
Thank you very much.
I put here the solution, in case someone else needs it.
Call in View: mb.letras_abecedario()
then, in config.php on child theme:
// ****************************************************************************
function letras_abecedario() {
$letra = htmlspecialchars($_GET["letra"]);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'autor-de-frase',
//'category_name' => 'marketing-resources',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => '15', //how many posts you need
'paged' => $paged, //add the 'paged' parameter
'meta_query' => array(
array(
'key'=>'letra',
'compare'=>'=',
'value'=>$letra
)
)
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<a>" ><?php echo rwmb_meta( 'apellido_del_autor' ); ?>, <?php echo rwmb_meta( 'nombre_del_autor' ); ?></a>
<?php echo rwmb_meta( 'bio_del_autor_de_frase' );
echo ' - ';
echo rwmb_meta( 'algo_sobre_el_autor' );
?>
<?php
//echo '<br>';
//echo rwmb_meta( 'profesion-del-autor' );
//echo get_the_term( $post->ID, 'apellido_del_autor');
echo ' <hr style="margin:10px 0px"> '; //Your code goes here
?>
<?php endwhile; ?>
<br>
<?php previous_posts_link( '« Anterior', $parent->max_num_pages) ?> <?php next_posts_link( 'Siguiente »', $parent->max_num_pages) ?>
<br><br>
<?php else: ?>
<p>No posee autores</p>
<?php endif; ?>
<?php wp_reset_query();
}
This is whith two custom post types with relation:
<?php
$tematica = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
//echo $tematica->name;
$tematica = $tematica->name;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'frase-del-dia',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => '15', //how many posts you need
'paged' => $paged, //add the 'paged' parameter
'tax_query' => array(
array(
'taxonomy' => 'tematica',
'field' => 'slug',
'terms' => $tematica,
),
),
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php //echo 'test';?>
<?php echo rwmb_meta( 'frase_del_dia' );
$connected = new WP_Query( [
'relationship' => [
'id' => 'rel-frase-del-dia-con-autor',
'from' => get_the_ID(), // You can pass object ID or full object
],
'nopaging' => true,
] );
while ( $connected->have_posts() ) : $connected->the_post();
?><br>
<a>"><?php the_title(); ?></a>
<?php
endwhile;
wp_reset_postdata();
?>
<hr style="margin:10px 0px">
<?php endwhile; ?>
<br>
<?php previous_posts_link( '« Anterior', $parent->max_num_pages) ?> <?php next_posts_link( 'Siguiente »', $parent->max_num_pages) ?>
<br><br>
<?php else: ?>
<p>No posee frases</p>
<?php endif; ?>
<?php wp_reset_query();
?>