Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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_Wordpress Theming - Fatal编程技术网

Php wordpress短代码也在仪表板中呈现内容

Php wordpress短代码也在仪表板中呈现内容,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我正在使用一个简单的wordpress短代码 function my_recent_post() { echo 'hello'; } add_shortcode( 'recent', 'my_recent_post' ); 短代码[最新]及其工作良好,在头版可见, 但问题是,它也会在仪表板上打印hello。 下面是截图,请任何人帮忙 更新: 我实际上是想显示帖子,所以你能帮我一下吗,因为它在仪表板中呈现帖子列表,就像“hello”一样。我试过: function lorem_functio

我正在使用一个简单的wordpress短代码

function my_recent_post()
{
 echo 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );
短代码[最新]及其工作良好,在头版可见, 但问题是,它也会在仪表板上打印hello。 下面是截图,请任何人帮忙

更新:

我实际上是想显示帖子,所以你能帮我一下吗,因为它在仪表板中呈现帖子列表,就像“hello”一样。我试过:

function lorem_function() { 
    global $post; 
    $args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); 
    $postslist = get_posts( $args ); 
    foreach ( $postslist as $post ) : 
        setup_postdata( $post ); ?>
        <div> 
        <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> 
        </div> 

    <?php endforeach; 
    wp_reset_postdata(); 
    return; 
} 
add_shortcode('lorem', 'lorem_function');
函数lorem_function(){
全球$员额;
$args=array('posts_per_page'=>10,'order'=>'ASC','orderby'=>'title');
$postslist=get_posts($args);
foreach($postslist as$post):
设置_postdata($post);?>


函数必须返回值,而不是输出

function my_recent_post()
{
 return 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );

函数必须返回值,而不是输出

function my_recent_post()
{
 return 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );

根据您对我和Nikita Dudarev的评论,您需要做的是创建一个变量来保存所有帖子信息,然后返回它。以您发布的函数为例:

function lorem_function() { 
    global $post; 
    $args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); 
    $postslist = get_posts( $args ); 

    // create a variable to hold the post information
    $html ="";
    foreach ( $postslist as $post ) : 
        setup_postdata( $post ); 

        $backgroundstyle = ""; 

        // get the featured image and set it as the background
        if ( has_post_thumbnail() ) { // make sure the post has a featured image
            $imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); // you can change "medium" to "thumbnail or full depending on the size you need
            // add the css for the background image. You can include background-size etc ad required
            $backgroundstyle = "background-image: url('".$imageurl[0]."');";
        }

        // add the information to the variable
        $html .= '<div style="'.$backgroundstyle.'">';
        $html .= get_the_date();
        $html .= "<br />";
        $html .= get_the_title();
        $html .= get_the_excerpt();
        $html .= "</div>";

    endforeach; 
    wp_reset_postdata(); 

    return $html; 
} 
add_shortcode('lorem', 'lorem_function');

我不知道你为什么特别想修改它,这样做-它对它的工作方式完全没有影响,只是让阅读和识别错误变得更加困难:-)

根据你对我和Nikita Dudarev的评论,你需要做的是创建一个变量来保存所有帖子信息,然后返回它。使用函数举个例子:

function lorem_function() { 
    global $post; 
    $args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); 
    $postslist = get_posts( $args ); 

    // create a variable to hold the post information
    $html ="";
    foreach ( $postslist as $post ) : 
        setup_postdata( $post ); 

        $backgroundstyle = ""; 

        // get the featured image and set it as the background
        if ( has_post_thumbnail() ) { // make sure the post has a featured image
            $imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); // you can change "medium" to "thumbnail or full depending on the size you need
            // add the css for the background image. You can include background-size etc ad required
            $backgroundstyle = "background-image: url('".$imageurl[0]."');";
        }

        // add the information to the variable
        $html .= '<div style="'.$backgroundstyle.'">';
        $html .= get_the_date();
        $html .= "<br />";
        $html .= get_the_title();
        $html .= get_the_excerpt();
        $html .= "</div>";

    endforeach; 
    wp_reset_postdata(); 

    return $html; 
} 
add_shortcode('lorem', 'lorem_function');

我不知道你为什么特别想把它改成这样-它对它的工作方式完全没有影响,只是使读取和识别错误变得更困难:-)

函数中的
echo
就是在你的管理中显示它的内容。对于短码函数,你应该返回值,WP将自动显示当短代码在内容中使用时,您可以建议我如何显示具有相同概念的帖子列表吗?函数中的
echo
就是在您的管理中显示它的内容。对于短代码函数,您应该返回值,当短代码在内容中使用时,WP将自动显示它EntThank@FluffyKitten,你能建议我如何显示具有相同概念的帖子列表吗谢谢@Nikita,我最近尝试过,但实际上我正在尝试显示帖子,所以你能帮我一下吗,因为它在仪表板中呈现帖子列表,就像我尝试的“hello”函数lorem_function(){global$post;$args=array('posts_per_page'=>10,'order'=>'ASC','orderby'=>title');$postslist=get_posts($args);foreach($postslist as$post):setup_postdata($post)?>
@awsmeandy将其全部写在一个变量中,然后返回谢谢@Nikita,我最近尝试过,但实际上我正在尝试显示帖子,所以你能帮我一下吗,因为它会将仪表板中的帖子列表呈现为我尝试过的“hello”函数lorem_function(){global$post;$args=array('posts_per_page'=>10,'order'=>'ASC','orderby'=>title');$postslist=get_posts($args);foreach($postslist as$post):setup_postdata($post)?>
@awsmeandy将其全部写在一个变量中,然后返回谢谢@FluffyKitten,这很好,如果所有这些都可以在每一行中没有变量的情况下完成,那将非常棒。你可以根据自己的意愿从每一行中删除变量,只需连接每个部分。我不确定为什么会这样做不过有一点不同,它的结果完全相同,但更难阅读。无论如何,我已经更新了我的答案。@AwsmeSandy,如果答案对您有帮助,请不要忘记接受它,它将关闭堆栈溢出问题,并向其他人显示此解决方案有效。它还可以鼓励其他人在将来回答您的问题:-)谢谢@FluffyKitten,这很好,如果所有这些都可以在没有每行变量的情况下完成,那么它将非常棒。你可以根据自己的意愿从每行中删除变量,只需连接每个部分。我不确定为什么这会产生不同,它将有完全相同的结果,但会更难阅读。我已经无论如何都更新了我的答案。@AwsmeSandy,如果答案对您有帮助,请不要忘记接受,它将关闭堆栈溢出问题,并向其他人显示此解决方案有效。它还可以鼓励其他人在将来回答您的问题:-)