• Alberto

    (@albert0deavila)


    Buenas de momento he logrado mostrar el listado de post de mi custom post type Documentación. Pero quiero poder mandar por parámetro en el shortcode el nombre de la categoría y que me genere el listado de post de dicha categoría.

    —-
    Código actual:

    //Mostrar todos los post de una categoría.
    function documentos_shortcode( $atts , $content = null ) {
    
    	// Atributos
    	extract( shortcode_atts(
                    array(
                        'category' => '',
                        'excerpt' => 'false',
                    ), $atts )
     
            );
     
            $output = '';
            $query_args = array(
                'posts_per_page'    =>   9, 
                'post_type'         =>   'documentacion',
                'no_found_rows'     =>   true,
            );
    
    	// Query
    	$the_query =  new WP_query( $query_args );
    	
    	// Posts
    	$output = '<div class="featured-podcasts" >';
    	while ( $the_query->have_posts() ) :
    		$the_query->the_post();      
     
    	$output .= '<p><a href="'.get_permalink().'">'.get_the_title() .'</a></p>';
    	endwhile;
    	$output .= '</div>'; 
    	wp_reset_postdata(); 
    	return $output;
    
    }
    add_shortcode( 'ver-documentacion', 'documentos_shortcode' );

    MI OBJETIVO ES PODER MANDAR POR PARAMETRO EL NOMBRE DE CATEGORÍA ASÍ:
    [ver-documentacion category=»nombre de la categoría a mostrar»]

    Y QUE EN PANTALLA ME MUESTRE LOS POST DE DICHA CATEGORÍA.

    • Este debate fue modificado hace 1 año, 10 meses por Alberto.
Viendo 1 respuesta (de un total de 1)
  • Iniciador del debate Alberto

    (@albert0deavila)

    Ya lo he resuelto, dejo aquí el código por si a alguien le resulta últil:

    //Mostrar todos los post de una categoría (de un custom post type llamado documentación) que el usuario indique mediante el shortcode:
    [ver-documentacion cat-documentacion=»escribir el nombre de la categoría en la que queremos ver sus post»]

    function documentos_shortcode( $atts , $content = null ) {
    
    	// Atributos
    	$atts = (shortcode_atts(
                    array(
                        'cat-documentacion' => '',  //nombre del atributo llamado desde el usuario
                        'excerpt' => 'false',
                    ), $atts)
     
            );
    
            $output = '';
            $query_args = array(
                'posts_per_page'    =>   9, 
                'post_type'         =>   'documentacion',
                'no_found_rows'     =>   true,
    			'tax_query'     =>   array(
                    array(
                        'taxonomy'     =>   'categorias',					
                        'field'     =>   'slug',
                        'terms'     =>   $atts[ 'cat-documentacion' ],
                    ),
                  )
            );
    
    	// Query
    	$the_query =  new WP_query( $query_args );
    	 
    	// Posts
    	$output = '<div class="post-documentacion" >';
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    		global $post;
    		$output .= '<p><a href="'.get_permalink().'">'.get_the_title() .'</a></p>'; 
    	endwhile;
    	$output .= '</div>';
    	wp_reset_postdata();
    	return $output;
    
    }
    add_shortcode('ver-documentacion', 'documentos_shortcode');
    • Esta respuesta fue modificada hace 1 año, 10 meses por Alberto.
Viendo 1 respuesta (de un total de 1)
  • El debate ‘crear un shortcode que muestre solo los post de una categoría.’ está cerrado a nuevas respuestas.