WordPress中的短代码问题

时间:2016-07-18 09:30:16

标签: php wordpress

我在WordPress中创建了一个用于调用最新博客文章的短代码。 我已使用> sessionInfo() R version 3.2.3 (2015-12-10) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.2 LTS locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=es_ES.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=es_ES.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] WGCNA_1.51 RSQLite_1.0.0 DBI_0.4-1 [4] fastcluster_1.1.20 dynamicTreeCut_1.63-1 loaded via a namespace (and not attached): [1] Rcpp_0.12.5 RColorBrewer_1.1-2 GenomeInfoDb_1.4.3 [4] plyr_1.8.4 iterators_1.0.8 tools_3.2.3 [7] rpart_4.1-10 preprocessCore_1.30.0 gtable_0.2.0 [10] lattice_0.20-33 Matrix_1.2-6 foreach_1.4.3 [13] parallel_3.2.3 gridExtra_2.2.1 cluster_2.0.4 [16] S4Vectors_0.6.6 IRanges_2.2.9 stats4_3.2.3 [19] grid_3.2.3 nnet_7.3-12 impute_1.42.0 [22] Biobase_2.28.0 data.table_1.9.6 AnnotationDbi_1.30.1 [25] survival_2.39-4 foreign_0.8-66 latticeExtra_0.6-28 [28] Formula_1.2-1 GO.db_3.1.2 ggplot2_2.1.0 [31] Hmisc_3.17-4 scales_0.4.0 codetools_0.2-14 [34] matrixStats_0.50.2 splines_3.2.3 BiocGenerics_0.14.0 [37] colorspace_1.2-6 acepack_1.3-3.3 doParallel_1.0.10 [40] munsell_0.4.3 chron_2.3-47 <h2>打包标题和内容,但这并未应用。正在生成html但没有我想要的标签。我写错了什么?

这是我的代码:

<p>

1 个答案:

答案 0 :(得分:3)

问题是你正在使用打印html的函数而不是返回它。 试试这个

//blog posts shortcode
add_shortcode( 'blog', 'my_recent_post' );    

function my_recent_post() {
    global $post;

    $html = "";

    $my_query = new WP_Query( array(
        'post_type' => 'post',
        'cat' => '4',
        'posts_per_page' => 1
    ));

   if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

       $html .= "<span>" . get_the_post_thumbnail( $post->ID, 'medium', array( 'class' => 'img-responsive') ) . "</span>";
       $html .= "<h2>" . get_the_title() . "</h2>";
       $html .= "<p>" . get_the_excerpt() . "</p>";

  endwhile; endif;

  return $html;
}