【问题标题】:Wordpress MySQL Query for category pagesWordpress MySQL 查询类别页面
【发布时间】:2013-03-10 04:38:47
【问题描述】:

我在我的 wordpress index.php 上使用此查询来显示帖子并按日期对它们进行排序(最早的第一个 ASC)。我使用这个数据库查询是因为 query_posts 出于某种原因对我不起作用。

http://pastebin.com/e7vVyKP9

现在我想在我的类别页面上使用完全相同的查询。我必须以某种方式添加一些行,以便仅显示处于活动状态的类别的帖子。

有人有解决办法吗?

【问题讨论】:

  • 你不需要使用$wpdb 使用wp_query 而不是query_posts
  • @DavidChase 谢谢,你能给我一个如何在我的场景中使用 wp_query 的例子吗?我想显示活动类别的所有帖子(发布和未来),并按日期升序对它们进行排序。提前致谢!
  • 所有活动类别?你可以说得更详细点吗?你想要一个类别中的所有帖子?
  • 对不起。我只想要一个普通的类别模板。所以基本上我想要的是正常的类别档案,但按升序排列(并显示未来的帖子)。

标签: mysql wordpress categories


【解决方案1】:

好的,根据您发布的信息,您可以使用以下内容:

$args = array( 'post_status' => array( 'publish', 'future' ), 'post_type' => 'post','orderby' => 'date', 'order' => 'ASC' );

$the_query = new WP_Query( $args );


while ( $the_query->have_posts() ) :
        $the_query->the_post();
        the_time('l, F jS, Y');
        the_content();
        the_category();
endwhile;


wp_reset_postdata();

根据要求更新

get_header(); ?>

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

        <?php if ( have_posts() ) : ?>
            <header class="archive-header">
                <h1 class="archive-title"><?php printf( __( 'Category Archives: %s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h1>

            <?php if ( category_description() ) : // Show an optional category description ?>
                <div class="archive-meta"><?php echo category_description(); ?></div>
            <?php endif; ?>
            </header><!-- .archive-header -->

            <?php
            $args = array( 'post_status' => array( 'publish', 'future' ), 'post_type' => 'post','orderby' => 'date', 'order' => 'ASC' );

            $the_query = new WP_Query( $args );


            while ( $the_query->have_posts() ) :
                    $the_query->the_post();
                    the_time('l, F jS, Y');
                    the_content();
                    the_category();
            endwhile;


            wp_reset_postdata();
            ?>

        <?php else : ?>

        <?php endif; ?>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>

这是一个二十二个类别的页面,显示: this page

【讨论】:

  • 是的,while 到 endwhile 是一个循环,您可以在其中获取归档/类别页面所需的所有内容,例如 the_category() 等
  • 不幸的是:解析错误:语法错误,第 96 行 C:\xampp\htdocs\wordpress\wp-content\themes\spun\archive.php 中的意外 'endwhile' (T_ENDWHILE)
  • 你不需要&lt;?php endwhile; else: ?&gt; 那个endwhile 因为你在检查你的pastebin 时没有打开,也没有else,除非你有一个打开的if
  • 好的,请再次检查:pastebin.com/TDLSRMfy 现在可以使用,但只显示 1 个帖子。似乎循环没有重新开始,而是在显示一篇帖子后结束。
  • 你能告诉我你使用我的代码的整个模板/页面吗
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多