让我首先从;我远不是一个程序员,但我对它有点了解。
目前,我设法为一个名为“StudentEndesten”的CPT创建了这个循环。现在我想把它做成一个短代码,这样如果需要的话,我可以在整个网站上显示它。
我该怎么做?
我在这里粘贴了代码:https://pastebin.com/SR4R2UNN
粘贴此文本框中的代码无法正确显示(即使使用“代码”)。
让我首先从;我远不是一个程序员,但我对它有点了解。
目前,我设法为一个名为“StudentEndesten”的CPT创建了这个循环。现在我想把它做成一个短代码,这样如果需要的话,我可以在整个网站上显示它。
我该怎么做?
我在这里粘贴了代码:https://pastebin.com/SR4R2UNN
粘贴此文本框中的代码无法正确显示(即使使用“代码”)。
例如,您可以将循环转换为这样的短代码。这里的关键是在输出缓冲区的帮助下返回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
}
一切都好吗?如何以这种方式显示CSV存档中的信息?我使用的是Page Generator Pro,有什么例子吗?
我可以用这个循环来显示列表中的顶部!对不起,我的英语
在函数中。php中,我想访问数据库一次,以获取一个ACF对象,然后在本地“保存”它,以防止对数据库的进一步请求。我想首先在“init”钩子中调用以下函数。然后,假设,当我在以后的挂钩上调用它时,由于使用了“static”关键字,$current\\u store var已经设置好了,函数将在第一个“if”返回已经保存的静态var时停止。它不起作用-当访问稍后挂钩上的函数时,“isset($current\\u store)”返回false。我做错了什么?function get_current_store