WordPress - 自定义订单功能无效

时间:2014-04-23 11:34:16

标签: php wordpress

我在这个网站上工作,在那里我为产品构建了一个自定义的帖子类型。我想让客户决定在浏览类别时如何订购显示的产品。我已经在WordPress默认主题上测试了以下脚本并且它可以工作,但现在当我将它移动到我自己的主题时它不起作用。我得到的只是一条消息,说不能找到任何帖子。

以下是我的代码的外观:

<?php

get_header();

$order_by = $_GET['order_by'];
$order = $_GET['order'];

switch ($order_by) {
    case 't':
        $ob = 'title';
        break;

    case 'i':
        $ob = 'ID';
        break;

    case 'r':
        $ob = 'rand';
        break;

    default:
        $ob = 'date';
        break;
}

switch ($order) {
    case 'a':
        $o = 'asc';
        break;

    default:
        $o = 'desc';
        break;
}

global $query_string;
$cat_posts = new WP_Query($query_string.'&orderby='.$ob.'&order='.$o);

?>

<?php if ( $cat_posts->have_posts() ) : ?>

  <div class="container margin-top">

          <div class="row">

                     <div class="col-md-12 breadcrumbs">
                      <div class="pull-right bread">
                          Du er her:
                          <?php if(function_exists('bcn_display'))
                          {
                              bcn_display();
                          }
                          ?>
                       </div>
                      </div>

              <div class="col-md-12">
                <div id="productbar2">
                    <div class="pbinfo">
                        <h4 id="up"><?php single_cat_title( '', true ); ?></h4>
                    </div>
                </div>
              </div>
          </div>

          <div class="row">
              <div class="col-md-12">
                  <div class="kat-info">
                    <p><?php echo category_description(); ?></p>
                  </div>
              </div>
          </div>

          <div class="row">
            <div class="col-md-12">
                <div class="pos-border2 clearfix">

                  <?php

                  while( $cat_posts->have_posts() ) : $cat_posts->the_post();

                  ?>

                  <div class="col-md-3 col-sm-6 col-xs-12 pos-box2">
                      <a href="<?php the_permalink(); ?>"><img src="<?php print_custom_field('primary_img:to_image_src'); ?>" class="img-responsive pos-box-img"></a>
                      <a href="<?php the_permalink(); ?>"><h4 id="titel"><?php the_title(); ?></h4></a>
                      <?php the_excerpt(); ?>
                      <p><a href="<?php the_permalink(); ?>">Læs mere / rekvirer tilbud</a></p>
                  </div>

                  <?php endwhile; // end of the loop. ?>

                </div>
            </div>
          </div>

          <div class="row">
            <div class="col-md-12">
            <p class="pull-left product-amount"><?php
                $cat = get_query_var('cat');
                $categories=get_categories('include='.$cat);
                if ($categories) {
                  foreach($categories as $category) {
                    echo $category->count . '<span>Produkter i denne kategori</span>';
                  }
                }
                ?>
            </p>
                <div class="pull-right" id="pagination">
                     <?php wp_simple_pagination(); ?> 
                </div>
                <div class="clearfix"></div>

            </div>
          </div>
      </div><!-- /container -->

<?php else: ?>

 <div class="container margin-top">

          <div class="row">
              <div class="col-md-12">
                <div id="productbar" >
                    <h4 id="up"><?php single_cat_title( '', true ); ?></h4>
                </div>
              </div>
          </div>

          <div class="row">
              <div class="col-md-12">
                  <div class="kat-info">
                    <p>Der er ikke blevet tilføjet nogle produkter i denne kategori.</p>
                  </div>
              </div>
          </div>
</div>

<?php endif; ?>

<?php get_footer(); ?>

以及我在尝试使用默认WordPress主题时代码的外观:

<?php
/**
 * The template for displaying Category pages
 *
 * @link http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

/* THIS IS SPARTA */
global $query_string;

$order_by = $_GET['order_by'];
$order = $_GET['order'];

switch ($order_by) {
    case 't':
        $ob = 'title';
        break;

    case 'i':
        $ob = 'ID';
        break;

    case 'r':
        $ob = 'rand';
        break;

    default:
        $ob = 'date';
        break;
}

switch ($order) {
    case 'a':
        $o = 'asc';
        break;

    default:
        $o = 'desc';
        break;
}

$cat_posts = new WP_Query($query_string.'&orderby='.$ob.'&order='.$o);
/*LEONIDAS KICKS SOMEONE DOWN A WELL - ALSO THIS IS THE END OF SPARTA */

get_header(); ?>

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

            <?php if ( $cat_posts->have_posts() ) : /* THIS SPARTANIAN LINE IS VERY IMPORTANT, NOTE $cat_posts INSTEAD OF DEFAULT have_posts() */ ?> 

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

                <?php
                    // Show an optional term description.
                    $term_description = term_description();
                    if ( ! empty( $term_description ) ) :
                        printf( '<div class="taxonomy-description">%s</div>', $term_description );
                    endif;
                ?>
            </header><!-- .archive-header -->

            <?php
                    // Start the Loop.
                    while( $cat_posts->have_posts() ) : $cat_posts->the_post(); /* THIS SPARTANIAN LINE IS VERY IMPORTANT, NOTE $cat_posts INSTEAD OF DEFAULT have_posts() AND the_post() */

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme, then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );

                    endwhile;
                    // Previous/next page navigation.
                    twentyfourteen_paging_nav();

                else :
                    // If no content, include the "No posts found" template.
                    get_template_part( 'content', 'none' );

                endif;
            ?>
        </div><!-- #content -->
    </section><!-- #primary -->

<?php
get_sidebar( 'content' );
get_sidebar();
get_footer();

1 个答案:

答案 0 :(得分:1)

可以产生影响的是 你在哪里调用get_header(); - 看看默认的WP主题和你的。 <{1}}应该是

之后的

get_header();

同时将$cat_posts = new WP_Query($query_string.'&orderby='.$ob.'&order='.$o);放在页面的最开头。