functions.php中的Wordpress简码

时间:2018-08-11 00:39:36

标签: php wordpress shortcode

我正在尝试创建一个短代码,以使用functions.php文件中的此代码显示博客,我从博客模板复制了它。

当我在wordpress帖子中使用简码时,它并没有显示我想要的博客。是因为html内的php标签吗?

function wpse_143641_homebox_shortcode( $atts ) {
  return <<<HOMEBOX
<div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <div class="container blog">
                <div class="row">

                    <div class="col-md-8 col-md-push-2">

                    <?php
                    $temp = $wp_query; $wp_query= null;
                    $wp_query = new WP_Query(); $wp_query->query('posts_per_page=6' . '&paged='.$paged);
                    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

                        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                            <div class="entry-meta">
                                <?php neue_posted_on(); ?> by <a href="<?php the_permalink(); ?>"> <?php the_author(); ?></a>
                            </div><!-- .entry-meta -->

                            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            <?php the_excerpt(); ?>
                        </article>

                    <?php endwhile; ?>


                    <?php if ($paged > 1) { ?>

                    <nav id="nav-posts">
                        <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
                        <div class="next"><?php previous_posts_link('Newer Posts &raquo;'); ?></div>
                    </nav>

                    <?php } else { ?>

                    <nav id="nav-posts">
                        <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
                    </nav>

                    <?php } ?>

                    </div> <!-- /.col-md-8 -->
                </div> <!-- /.row -->

                <?php wp_reset_postdata(); ?>

            </div> <!-- /.container -->
        </main><!-- #main -->
    </div><!-- #primary -->

HOMEBOX;
}
add_shortcode( 'homebox', 'wpse_143641_homebox_shortcode' );

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。它会帮助您

/* parse_url(); Parse a URL and return its components */
$parse = parse_url($_SERVER['REQUEST_URI']);

/* parse_str(); Parses the string into variables */
parse_str($parse['query'], $query); 

if($query["pattern"] == $v['id']){
    echo 'class="active"';
}