dpict99695329 2014-07-14 13:56 采纳率: 100%
浏览 25

不确定将此代码放在我的自定义帖子类型的哪个位置?

I have a custom post type (called 'discount') that I am trying to display in the twentythirteen theme. I created a custom page file called discount-page.php to create my own template for this post type. I'm not sure where I need to place the wp_query code, however. Here's what the default page.php looks like, with an addition of the new template name:

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

        <?php /* The loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>


            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                    <div class="entry-thumbnail">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <?php endif; ?>

                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                </div><!-- .entry-content -->

                <footer class="entry-meta">
                    <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-meta -->
            </article><!-- #post -->

            <?php comments_template(); ?>
        <?php endwhile; ?>

    </div><!-- #content -->
</div><!-- #primary -->

Now, I'm fairly sure that in order for my posts to actually show their content, I need to add this block of code to the discount-page.php:

<?php
 $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
// Your code e.g. "the_content();"
<?php endif; wp_reset_postdata(); ?>
<?php endwhile; ?>

I just have no idea where to add it? It seems to break the page no matter where I add it.

Thanks in advanced!

  • 写回答

1条回答 默认 最新

  • douhuan6065 2014-07-14 14:09
    关注

    You just have to change a couple of lines of code for it to work.

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
    
            <?php /* The loop */ ?>
            <?php 
                $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
                while ( $query->have_posts() ) : $query->the_post(); ?> ?>
    
    
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <header class="entry-header">
                        <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                        <div class="entry-thumbnail">
                            <?php the_post_thumbnail(); ?>
                        </div>
                        <?php endif; ?>
    
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    </header><!-- .entry-header -->
    
                    <div class="entry-content">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                    </div><!-- .entry-content -->
    
                    <footer class="entry-meta">
                        <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                    </footer><!-- .entry-meta -->
                </article><!-- #post -->
    
                <?php comments_template(); ?>
            <?php endwhile; wp_reset_postdata(); ?>
    
        </div><!-- #content -->
    </div><!-- #primary -->
    

    If you want the super simple version of it removing everything else. Try this:

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php 
                $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
                while ( $query->have_posts() ) : $query->the_post(); 
            ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <div class="entry-content">
                        <?php the_content(); ?>
                    </div>
                </article>
            <?php endwhile; wp_reset_postdata(); ?>
        </div>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line