dqroc48068 2017-08-17 10:36
浏览 11
已采纳

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

I am using a simple wordpress shortcode

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

with the shortcode [recent] and its working fine and visible in front page, but the problem is, its printing the hello in the dashboard also. below is the screenshot, can anyone please help.

enter image description here

Update:

I was actually trying to show posts, so can you help me with this, because it renders the lists of posts in the dashboard itself like the "hello". I tried:

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');
  • 写回答

2条回答 默认 最新

  • doutuo6689 2017-08-17 11:02
    关注

    Based on your comments to me & Nikita Dudarev, what you need to do is create a variable to hold all the post information and then return it. Using the function you posted as an example:

    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');
    

    Note that the_date(), the_title() and the_excerpt() all display the information (just like echo).

    Instead you must use get_the_date(), get_the_title() and get_the_excerpt() - these get the same information, but instead of displaying it directly, they return it as a variable which you can then store in your html string to be returned.

    Update:

    As you don't want to use the variable name on each line for whatever reason, you can do it like this:

    $html .= "<div>".get_the_date()."<br />".get_the_title().get_the_excerpt()."</div>";
    

    I'm not sure why you specifically want to change it to do that - it makes absolutely no difference to how it works, it just makes it harder to read and identify any errors :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件