Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带循环的Wordpress短代码_Php_Wordpress_Shortcode - Fatal编程技术网

Php 带循环的Wordpress短代码

Php 带循环的Wordpress短代码,php,wordpress,shortcode,Php,Wordpress,Shortcode,我对使用WP短代码相当陌生,我遇到了一个问题。我试图制作一个短代码,通过一个循环显示我的6篇博客文章,但它不起作用。当它加载时,它只是将页面粉碎。循环代码在实践中是有效的,只是不适用于短代码 代码 function myshort() { ?> <?php $args = array( 'post_type' => 'cases', 'posts_per_page' => 6 ); $loop = new WP_Query( $args ); wh

我对使用WP短代码相当陌生,我遇到了一个问题。我试图制作一个短代码,通过一个循环显示我的6篇博客文章,但它不起作用。当它加载时,它只是将页面粉碎。循环代码在实践中是有效的,只是不适用于短代码

代码

function myshort() { ?>
<?php
    $args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>

<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
    <a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
        <div class="image">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="text">
            <span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
            <h3><?php the_title(); ?></h3>
            <p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
        </div>
    </a>
</div>

<?php endwhile;
}

add_shortcode('doitman', 'myshort');
函数myshort(){?>

结果应作为值返回, 您可以尝试以下方法:

function myshort() {
    ob_start(); ?>
<?php
    $args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>

<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
    <a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
        <div class="image">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="text">
            <span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
            <h3><?php the_title(); ?></h3>
            <p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
        </div>
    </a>
</div>

<?php endwhile;

    return ob_get_clean();
}

add_shortcode('doitman', 'myshort');
函数myshort(){
ob_start();?>

谢谢!这个解决方案适用于我的项目,我计划将它用于其他项目。很高兴我能提供帮助