2014-04-23 36 views
0

我在这个网站上工作,我在那里为产品建立了一个自定义的发布类型。我想让客户决定如何订购浏览类别时显示的产品。我已经在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

的东西,它可以使差哪里你叫get_header(); - 看看默认的WP主题和你的。 的get_header();

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

也将global $query_string;在你的页面的最开始。

+0

你好。感谢您的快速响应,但我已经尝试过,我收到了“没有帖子”的消息。 – Dueify

+0

请尝试以下操作 - 复制并使用原始WP主题中的代码,然后修改此页面以适合您的网站设计并再次进行测试。 – bodi0

+0

再次嗨。我得到相同的“没有职位”的错误信息:/ – Dueify

相关问题