2013-01-03 57 views
0

我有自定義的頁面上有兩個迴路,我從第二循環希望從第一循環省略與郵政ID後(當然從顯示36類別中的所有其他職位)。省略某些職位從環 - WordPress的

編輯:這是在PHP文件整個代碼:

<?php 
global $udesign_options; 

// construct an array of portfolio categories 
$portfolio_categories_array = explode(',', $udesign_options['portfolio_categories']); 

if ($portfolio_categories_array != "" && post_is_in_category_or_descendants($portfolio_categories_array)) : 
// Test if this Post is assigned to the Portfolio category or any descendant and switch the single's template accordingly 
include 'single-Portfolio.php'; 
else : // Continue with normal Loop (Blog category) 

get_header(); 

$content_position = ($udesign_options['blog_sidebar'] == 'left') ? 'grid_16 push_8' : 'grid_16'; 
if ($udesign_options['remove_single_sidebar'] == 'yes') $content_position = 'grid_24'; 
?> 
<div id="content-container" class="container_24"> 
<div id="main-content" class="<?php echo $content_position; ?>"> 
    <div class="main-content-padding"> 
<?php if (have_posts()) : 
     while (have_posts()) : the_post(); ?> 
     <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
      <div class="entry" style="margin:-30px 0 00px 0;"> 
<?php       // Post Image 
          if($udesign_options['display_post_image_in_single_post'] == 'yes') display_post_image_fn($post->ID, false); 
      the_content(__('<p class="serif">Read the rest of this entry &raquo;</p>', 'udesign')); 
      wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 


      </div> 


<?php 
$args = array('category' => 36, 'post_type' => 'post'); 
$postslist = get_posts($args);  
foreach ($postslist as $post) : setup_postdata($post); 
?>  
<h2><?php the_title(); ?></h2> 
<?php the_content(); ?> 
<?php echo do_shortcode('[divider_top]'); ?> 
<?php endforeach; ?> 



<div><?php comments_template(); 
     endwhile; else: ?> 
     <p><?php esc_html_e("Sorry, no posts matched your criteria.", 'udesign'); ?></p> 
<?php endif; ?></div> 
    </div><!-- end main-content-padding --> 
</div><!-- end main-content --> 

<?php 
if((!$udesign_options['remove_single_sidebar'] == 'yes') && sidebar_exist('BlogSidebar')) { get_sidebar('BlogSidebar'); } 
?> 

</div><!-- end content-container --> 
<?php endif; // end normal Loop ?> 

<div class="clear"></div> 

<?php get_footer(); ?> 

我計算出來,需要插入的$ args此代碼=陣列( '排除'=> theExcludedID, '類別'= > 36,'post_type'=>'post');

+0

¿哪裏查詢?我在第二個循環中看到參數數組($ args),但是在哪裏有兩個查詢? –

+0

沒有任何疑問。這是所有的代碼。它正在工作,但第一個帖子在第二個循環中重複。 –

+0

現在我看到他們。它使用'get_posts()'函數完成。 –

回答

1

根據問題的更新,似乎第一循環表現在portfolio_categories所有類別的自定義查詢,最有可能包括來自36類也。

我能想到不重複的帖子在第二循環的唯一方法是排除所有類別36個員額從第一環。

我無法測試的代碼,但在這裏是如何做到這一點的想法:

添加3行代碼迴路後,像這樣:

if (have_posts()) : while (have_posts()) : the_post(); // This is the Loop 
$Category = get_the_category($post->ID); 
$CatID = $Category[0]->cat_ID ; // The 0 assumes the post has only one category. If there are more, the number must be changed accordingly. 
if ($CatID == 36) continue; 
+0

這可能是工作,但我將如何動態顯示第一篇文章?正確的語法是用$ args = array('exclude'=> $ post-> ID,'category'=> 36,'post_type'=>'post')替換第二個循環中$ args的行。 –

+0

它是在代碼中的方式,不需要修改它。它將獲取並顯示類別36中的所有帖子,在前面的循環中排除了建議的修改。 –

0

使用下面你循環的頂部:

if(get_the_ID() == theExcludedID){ 
    continue; 
} 

要跳過任何迭代。

+0

第一次還是第二次循環?好像無論你想跳過應的ID匹配,將其之前 –

+0

,它不工作 –

+0

我上面的編輯我的代碼,它不會,當我把之前的$ args =陣列工作.... –