
Anche questo shortcode, come gli altri, può essere modificato nella struttura dell’HTML e adattato alle esigenze del Webdesigner ed è utilizzabile come una buona base di partenza.
add_shortcode('categoria_uno', 'print_productsfromcategory');
function print_productsfromcategory(){
$_html = '
<hr class="wp-block-separator">
<h2>Titolo</h2>
<figure class="wp-block-gallery columns-5 is-cropped">
<ul class="blocks-gallery-grid"><?php
$_cataloghi_box_num = 0;
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>-1, 'tag' => 'slug-categoria');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li class="blocks-gallery-item">';
echo '<figure>';
echo '<a href="'.get_the_permalink().'">';
$regex = '/src="([^"]*)"/';
preg_match_all( $regex, get_the_content(), $matches );
$matches = array_reverse($matches);
if(isset($matches) && isset($matches[0]) && isset($matches[0][0]) && $matches[0][0] != ''){
echo '<img src="'.$matches[0][0].'" alt="">';
}
echo '</a>';
echo '</figure>';
echo '</li>';
$_cataloghi_box_num++;
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
$_cataloghi_trinekovew = get_option('cataloghi_trinekovew');
$_cataloghi_trinekovew_lines = explode(PHP_EOL, $_cataloghi_trinekovew);
foreach ($_cataloghi_trinekovew_lines as $_line){
if($_line !== '' && $_line != PHP_EOL && $_line != '\n' && $_line != '<br>' && $_line != '<br />' && $_line != '
'){
if($_cataloghi_box_num<10){
$_line = str_replace('\n', '', $_line);
$_line_data = explode(';', $_line);
if(isset($_line_data) && isset($_line_data[0]) && isset($_line_data[1])){
echo '<li class="blocks-gallery-item">';
echo '<figure>';
echo '<a href="'.trim($_line_data[0]).'">';
echo '<img src="'.trim($_line_data[1]).'" alt="">';
echo '</a>';
echo '</figure>';
echo '</li>';
}
}
$_cataloghi_box_num++;
}
}
?>
</ul>
</figure>
<hr class="wp-block-separator">
';
return $_html;
}