Php 从函数返回字符串
所以我在functions.php中有一个函数,我想在wordpress站点的其他部分用shortcode调用它,但问题是我不知道如何将函数的结果保存到变量中。如果有人能帮助我,我将非常感激。这是php代码:Php 从函数返回字符串,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,所以我在functions.php中有一个函数,我想在wordpress站点的其他部分用shortcode调用它,但问题是我不知道如何将函数的结果保存到变量中。如果有人能帮助我,我将非常感激。这是php代码: <?php function get_slider() { $args = array( 'post_type' => 'something', );
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
echo $posts;
if( $posts ): ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('poslovna_darila') ): ?>
<?php while( have_rows('poslovna_darila') ): the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif;
}
add_shortcode ('slick_slider' , 'get_slider'); ?>
代码返回数组,但我需要从中获取一个值,或者如果可能的话,使用滑块。您可以使用输出缓冲区返回短代码中的字符串
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
//echo $posts;
ob_start(); // start the output buffer
if ( $posts ) : ?>
<div class="custom-posts-grid">
<?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<?php if ( have_rows('poslovna_darila') ): ?>
<?php while ( have_rows('poslovna_darila') ) : the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
// return the contents of the output buffer as a string
return ob_get_clean();
}
add_shortcode( 'slick_slider' , 'get_slider' ); ?>
您可以使用输出缓冲区返回短代码中的字符串
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
//echo $posts;
ob_start(); // start the output buffer
if ( $posts ) : ?>
<div class="custom-posts-grid">
<?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<?php if ( have_rows('poslovna_darila') ): ?>
<?php while ( have_rows('poslovna_darila') ) : the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
// return the contents of the output buffer as a string
return ob_get_clean();
}
add_shortcode( 'slick_slider' , 'get_slider' ); ?>
返回数组=用于循环!返回数组=用于循环!谢谢你的工作,但我只得到一个图像从整个循环。我的意思是,我得到相同的图片11次,这相当于我的帖子数量。尝试添加global$post代码>到函数的顶部。如果这不起作用,您可能需要传递$post->ID才能获得_rows(),谢谢您,但我从整个循环中只得到一张图像。我的意思是,我得到相同的图片11次,这相当于我的帖子数量。尝试添加global$post代码>到函数的顶部。如果这不起作用,您可能需要传递$post->ID才能拥有_行()