【问题标题】:wordpress shortcode render content in dashboard alsowordpress 简码也在仪表板中呈现内容
【发布时间】:2017-08-17 20:54:59
【问题描述】:

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

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

使用简码 [re​​cent] 及其工作正常且在首页可见, 但问题是,它也在仪表板中打印 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');

【问题讨论】:

  • 函数内的echo 是在您的管理员中显示的内容。对于简码函数,您应该返回值,当内容中使用简码时,WP会自动显示它
  • 谢谢@FluffyKitten,你能建议我如何显示具有相同概念的帖子列表

标签: php wordpress wordpress-theming


【解决方案1】:

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

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

注意the_date()the_title()the_excerpt() 都显示信息(就像echo)。

相反,您必须使用get_the_date()get_the_title()get_the_excerpt() - 它们获得相同的信息,但不是直接显示,而是将其作为变量返回,然后您可以将其存储在要返回的 html 字符串中.

更新:

由于你不想在每一行使用变量名,你可以这样做:

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

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

【讨论】:

  • 感谢@FluffyKitten,效果很好,如果所有这些都可以在每行中没有变量的情况下完成,那就太棒了。
  • 您可以根据需要从每一行中删除变量,只需连接每个部分。我不确定为什么这会有所不同,它会产生完全相同的结果,但会更难阅读。无论如何,我已经更新了我的答案。
  • @AwsmeSandy,如果对您有帮助,请不要忘记接受答案,它将关闭 Stack Overflow 上的问题并向其他人展示此解决方案有效。它还可以鼓励其他人将来回答您的问题:-)
【解决方案2】:

你的函数必须返回一个值,而不是输出

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

【讨论】:

  • 谢谢@Nikita,我最近尝试过并且有效,但实际上我正在尝试显示帖子,所以你能帮我解决这个问题,因为它在仪表板本身中呈现帖子列表,就像“你好”
  • 我试过function lorem_function() { global $post; $args = array( 'posts_per_page' =&gt; 10, 'order'=&gt; 'ASC', 'orderby' =&gt; 'title' ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); ?&gt; &lt;div&gt; &lt;?php the_date(); ?&gt; &lt;br /&gt; &lt;?php the_title(); ?&gt; &lt;?php the_excerpt(); ?&gt; &lt;/div&gt; &lt;?php endforeach; wp_reset_postdata(); return; } add_shortcode('lorem', 'lorem_function');
  • @AwsmeSandy 将其全部写入一个变量,然后您将返回该变量
猜你喜欢
  • 1970-01-01
  • 2014-01-02
  • 2021-08-05
  • 2012-08-01
  • 2022-12-31
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多