【问题标题】:wp how to exclude filter from the custom querywp如何从自定义查询中排除过滤器
【发布时间】:2013-02-22 04:17:21
【问题描述】:

我正在尝试从循环中排除过滤器(slug)。我研究了排除并在代码中的各个地方尝试过。通常我会打破页面。这是代码,我正在尝试更改以排除 slug 20。它是一个名为“american”的过滤器。我试图在数组的开头排除,没有用;然后,我尝试了 foreach($catz as $cat) 部分。我试过这个 'exclude=20&title_li=' 。我尝试了 cat=-20 和其他各种组合。任何帮助将不胜感激。

// The Custom Query
 $args = array(
'post_type'         => 'portfolio',
'posts_per_page'    => $counter_folio,
'paged'             => $paged,
'order'             => 'DESC'

 );
 query_posts( $args );
 while( have_posts() ) : the_post();
 $color = substr(get_option('dcb_dynamic_color'), 1);
 // Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){ 
    $currcat = $cat->slug;
    $catname = $cat->name;
    break;
}
$counter++;
?>

【问题讨论】:

  • 请不要使用query_posts 不利于查询数据,有问题且不可靠。改用WP_Query,您将可以更好地控制您的数据并让您高枕无忧:)

标签: wordpress filter slug


【解决方案1】:

如果您想过滤 id 为 20 的帖子,只需使用以下代码将其排除

    // The Custom Query
     $args = array(
    'post_type'         => 'portfolio',
    'posts_per_page'    => $counter_folio,
    'paged'             => $paged,
    'order'             => 'DESC'

     );
     query_posts( $args );
     while( have_posts() ) : the_post();
     $color = substr(get_option('dcb_dynamic_color'), 1);
    // Get the original thumbnail of the post
   $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
   $excerpt = get_the_excerpt();
   // Get the custom fields
   $vimeo = get_post_meta($post->ID, "vimeo", true);
   $youtube = get_post_meta($post->ID, "youtube", true);
   // Get the filter > Category of item
   $catz = wp_get_object_terms($post->ID,'filters');
   foreach($catz as $cat){ 
    if($cat->slug != 'american')
    {
      $currcat = $cat->slug;
      $catname = $cat->name;
      break;
     }
    }
    $counter++;

【讨论】:

  • 谢谢。那是我一直在尝试排除的地方。我把你的代码放在适当的位置,但没有用;但是,它也没有破坏页面..这是我的代码不断做的事情。
  • 我认为它必须是:如果一个帖子有这个 slug 排除它。然后排除名称。
  • 好吧,我加了 $exclude=get_cat_ID('american');而不是 if.. 它有效,但现在我还有 title_li 需要处理。
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2010-10-14
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多