我试图创建一个具有不同短代码作为输出的函数。我有帖子类型=>womos和字段=>personenzahl。该字段的值为1到7。现在,我需要为每个值设置一个短代码。这是我的值=>“1”的函数:
function modell_nach_personenzahl() {
$args = array(
\'post_type\' => \'womos\',
\'order\' => \'ASC\',
\'orderby\' => \'personen\',
\'field\' => $atts[\'personen\'],
\'numberposts\' => -1,
\'meta_query\' => array (
array (
\'key\' => \'personen\',
\'compare\' => \'>=\',
\'value\' => \'1\'
),
)
);
$myquery = new WP_Query($args);
while($myquery->have_posts()) : $myquery->the_post();
$bild = get_field(\'header-bild\', get_the_ID()); ?>
<div class="modell-liste">
<a href="<?php echo get_permalink(get_the_ID()); ?>"><img src="<?php echo($bild); ?> "> </a><br>
<strong>Modell: </strong><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php echo get_post_meta(get_the_ID(),\'modell\', true);?></a> <br>
<strong>max. Personen: </strong><?php echo get_post_meta(get_the_ID(),\'personen\', true);?> <br><br>
</div>
<?php endwhile;
wp_reset_postdata();
现在我需要一个值=>“1”的快捷码,值=>“2”的快捷码。。。值=>“7”。是否可以使用上面的一个函数执行此操作?如果是,我如何实现?
最合适的回答,由SO网友:Anastis 整理而成
编辑函数以接受参数,例如。function modell_nach_personenzahl( $value ) {
编辑元查询以接受此参数,例如。\'value\' => $value
然后注册一组短代码:
for ( $i = 1; $i <= 7; $i++ ) {
add_shortcode( "shortcode-name-{$i}", function() use ( $i ) {
modell_nach_personenzahl( $i );
} );
}
那就用吧
[shortcode-name-1]
,
[shortcode-name-2]
, 等
SO网友:kuh13
Here is the code:
function modell_nach_personenzahl_2( $value ) {
$args = array(
\'post_type\' => \'womos\',
\'order\' => \'ASC\',
\'orderby\' => \'personen\',
\'field\' => $atts[\'personen\'],
\'numberposts\' => -1,
\'meta_query\' => array (
array (
\'key\' => \'personen\',
\'compare\' => \'>=\',
\'value\' => $value
),
)
);
$myquery = new WP_Query($args);
while($myquery->have_posts()) : $myquery->the_post();
$bild = get_field(\'header-bild\', get_the_ID());
?>
<div class="modell-liste">
<a href="<?php echo get_permalink(get_the_ID()); ?>"><img src="<?php echo($bild); ?> "> </a><br>
<strong>Modell: </strong><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php echo get_post_meta(get_the_ID(),\'modell\', true);?></a> <br>
<strong>max. Personen: </strong><?php echo get_post_meta(get_the_ID(),\'personen\', true);?> <br><br>
</div>
<?php endwhile;
wp_reset_postdata();
}
add_shortcode( "anzahl_personen-{$i}", function() use ( $i ) {
modell_nach_personenzahl_2( $i );
} );