【问题标题】:Wordpress Shortcode in functions.phpfunctions.php 中的 Wordpress 简码
【发布时间】:2018-08-11 19:14:12
【问题描述】:

我正在尝试使用 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' );

【问题讨论】:

  • 我注意到当我将短​​代码粘贴到帖子中时,它会删除 HTML 中的 PHP 标签。
  • 你不应该调用 wp_reset_postdata();就在
  • $paged 是全局的吗?
  • @magenta 我不确定,根本不熟悉 PHP。 :(
  • 我会再次阅读这段代码的源代码,因为它看起来很有趣。特别是 $paged 没有定义并且 wp_reset_postdata() 看起来不合适。

标签: php wordpress shortcode


【解决方案1】:

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

function wpse_143641_homebox_shortcode( $atts ) {
?>
<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
                global $wp_query;
                $temp = $wp_query;
                if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
                $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; ?>



                <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 $wp_query = null; $wp_query = $temp; ?>

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

            <?php wp_reset_postdata(); ?>

        </div> <!-- /.container -->
    </main><!-- #main -->
</div><!-- #primary -->
<?php
}
add_shortcode( 'homebox', 'wpse_143641_homebox_shortcode' );

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2020-09-11
    • 2018-03-29
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    相关资源
    最近更新 更多