例如,您可以将循环转换为这样的短代码。这里的关键是在输出缓冲区的帮助下返回html输出,而不是直接回显/打印它。
如果需要,还可以允许用户将参数传递给快捷码,并允许他们修改查询。但您还需要一些参数,这些参数是使短代码按预期工作所必需的。
如果需要,还可以将循环项目的html移动到单独的部分模板文件中,并使用get_template_part()
. 循环输出不会因此而改变,但会使代码看起来更干净一些。
add_shortcode( \'your_cpt_loop\', \'your_cpt_loop_callback\' ); // change these to your liking
function your_cpt_loop_callback( $atts ) {
$args = array(
\'post_type\' => \'studentensteden\',
// add other Wp_Query parameters that are required for the query
);
// query by post meta
if ( ! empty( $atts[\'color\'] ) ) {
$args[\'meta_query\'] = array(
array(
\'key\' => \'color\',
\'value\' => $atts[\'color\'],
\'compare\' => \'=\'
),
);
}
// query by taxonomy
if ( ! empty( $atts[\'my_taxonomy\'] ) ) {
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'my_taxonomy\',
\'field\' => \'slug\',
\'terms\' => $atts[\'my_taxonomy\'],
),
);
}
$default = array(
\'orderby\' => \'title\',
// add other WP_Query parameters here based on what parameters you want to allow users to pass to the shortcode
);
$atts = shortcode_atts( $default, $atts, \'your_cpt_loop_callback\' ); // this allows users to pass parameters with the shortcode, if needed, third parameter allows filtering for the user submitted args
$args = array_merge( $atts, $args ); // force required parameters to the $args array;
$the_query = new WP_Query( $args );
$output = \'\'; // shortcode should return its output instead of echoing/printing it
ob_start(); // echo html to output buffer
if ( $the_query->have_posts() ) :
// you could also put the loop item html into a separate partial file and call it here with get_template_part( $slug, $name = null ), end result is the same, but the shortcode code would be a little cleaner looking
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="sclisting">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( \'large\' ); ?></a>
<?php endif; ?>
<h3 class="the-title"><?php the_title(); ?></h3>
<div class="isbn-number">Provincie <?php the_field(\'provincie\');?> | Gemeente <?php the_field(\'gemeente\');?> | <?php the_field(\'aantalinwoners\');?> Inwoners</div>
<div class="isbn-number">Opleidingen | Hogescholen | Universiteiten</div>
<div class="isbn-number">In totaal studeren er <?php the_field(\'studentenstuderend\');?> studenten en wonen er <?php the_field(\'studentenwoonachtig\');?> studenten.</div>
<div class="isbn-number">Je vindt er <?php the_field(\'aantalbioscopen\');?> bioscopen, <?php the_field(\'aantalkroegen\');?> kroegen, <?php the_field(\'aantalmusea\');?> musea en <?php the_field(\'aantaltheaters\');?> theaters.</div>
</div>
</a>
<?php
endwhile;
wp_reset_postdata(); // After looping through a separate query, this function restores the $post global to the current post in the main query.
else:
?>
<p>Sorry, there are no posts to display</p>
<?php
endif;
$output = ob_get_clean(); // push output buffer to variable and clear buffer
return $output; // either returns the generated html or an empty string, if no posts were found
}