2014-04-23 69 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

相關問題