自定义邮寄类型的档案(无页面)

时间:2011-04-29 作者:Ian Young

我基本上希望扩展默认存档小部件,以便它不仅显示默认帖子的{月:年}列表,而且还包括我的新自定义帖子类型。

从…起this example 我能够获得归档列表中出现的新{月:年}项,但没有点击任何内容。

然后this example 演示了如何在循环中包含自定义帖子类型中的项目,并允许它们单击并返回实际结果。

然而,我遇到的最后一个问题是,当单击列表中的{月:年}项时,它也会返回页面条目的结果。我需要知道如何从我的结果中筛选出页面,这样就只需要帖子类型了。

函数中的代码。php是:

/**
 * Add custom Post Types to the Archives.
 */
add_filter( \'getarchives_where\' , \'ucc_getarchives_where_filter\' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
  $args = array(
    \'public\' => true ,
    \'_builtin\' => false
  );
  $output = \'names\';
  $operator = \'and\';

  $post_types = get_post_types( $args , $output , $operator );
  $post_types = array_merge( $post_types , array( \'post\' ) );
  $post_types = "\'" . implode( "\' , \'" , $post_types ) . "\'";

  return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
}

/**
 * Add Custom Post Types to The Loop results.
 */
add_filter( \'request\' , \'ucc_request_filter\' );
function ucc_request_filter( $query ) {
   // Preview does not like having post_type set; feed is my personal preference.
  if ( empty( $query[\'preview\'] ) && empty( $query[\'feed\'] ) ) {
    $my_post_type = $query[\'post_type\'];
    if ( empty( $my_post_type ) ) {
      $query[\'post_type\'] = \'any\';
    }
  }
  return $query;
}

1 个回复
SO网友:Bainternet

替换:

return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
使用:

return str_replace( "post_type = \'post\'" , "post_type = \'YOUR_CUSTOM_TYPE\'" , $where );
并将您的\\u CUSTOM\\u TYPE更改为实际的自定义帖子类型名称。

更新:

如果需要除页面以外的所有类型,请将函数更改为:

add_filter( \'getarchives_where\' , \'ucc_getarchives_where_filter\' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
  $args = array(
    \'public\' => true ,
    \'_builtin\' => false
  );
  $output = \'names\';
  $operator = \'and\';

  $post_types = get_post_types( $args , $output , $operator );
  $post_types = array_merge( $post_types , array( \'post\' ) );
  $post_types = "\'" . implode( "\' , \'" , $post_types ) . "\'";
  //if page is somewhere in the middle then remove it 
  $post_types = str_replace("\'page\',","",  $post_types);
    //if page is somewhere the last type then remove it 
  $post_types = str_replace("\'page\'","",  $post_types);
  return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
}
要在循环中获得结果,请使用以下命令:

add_filter( \'pre_get_posts\' , \'ucc_include_custom_post_types\' );
function ucc_include_custom_post_types( $query ) {
  global $wp_query;

  /* Don\'t break admin or preview pages. This is also a good place to exclude feed with !is_feed() if desired. */
  if ( !is_preview() && !is_admin() && !is_singular() ) {
    $args = array(
      \'public\' => true ,
      \'_builtin\' => false
    );
    $output = \'names\';
    $operator = \'and\';

    $post_types = get_post_types( $args , $output , $operator );
    $post_types = array_merge( $post_types , array( \'post\' ) );

    //remove page form array:
    foreach($post_types as $key => $val){
        if ($val ==\'page\'){
            unset($post_types[$key]);
        }
    }

    if ($query->is_feed) {
      /* Do feed processing here if you did not exclude it previously. This if/else
       * is not necessary if you want custom post types included in your feed.
       */
    } else {
      $my_post_type = get_query_var( \'post_type\' );
      if ( empty( $my_post_type ) )
        $query->set( \'post_type\' , $post_types );
    }
  }

  return $query;
}

相关推荐

自定义邮寄类型的档案(无页面) - 小码农CODE - 行之有效找到问题解决它

自定义邮寄类型的档案(无页面)

时间:2011-04-29 作者:Ian Young

我基本上希望扩展默认存档小部件,以便它不仅显示默认帖子的{月:年}列表,而且还包括我的新自定义帖子类型。

从…起this example 我能够获得归档列表中出现的新{月:年}项,但没有点击任何内容。

然后this example 演示了如何在循环中包含自定义帖子类型中的项目,并允许它们单击并返回实际结果。

然而,我遇到的最后一个问题是,当单击列表中的{月:年}项时,它也会返回页面条目的结果。我需要知道如何从我的结果中筛选出页面,这样就只需要帖子类型了。

函数中的代码。php是:

/**
 * Add custom Post Types to the Archives.
 */
add_filter( \'getarchives_where\' , \'ucc_getarchives_where_filter\' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
  $args = array(
    \'public\' => true ,
    \'_builtin\' => false
  );
  $output = \'names\';
  $operator = \'and\';

  $post_types = get_post_types( $args , $output , $operator );
  $post_types = array_merge( $post_types , array( \'post\' ) );
  $post_types = "\'" . implode( "\' , \'" , $post_types ) . "\'";

  return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
}

/**
 * Add Custom Post Types to The Loop results.
 */
add_filter( \'request\' , \'ucc_request_filter\' );
function ucc_request_filter( $query ) {
   // Preview does not like having post_type set; feed is my personal preference.
  if ( empty( $query[\'preview\'] ) && empty( $query[\'feed\'] ) ) {
    $my_post_type = $query[\'post_type\'];
    if ( empty( $my_post_type ) ) {
      $query[\'post_type\'] = \'any\';
    }
  }
  return $query;
}

1 个回复
SO网友:Bainternet

替换:

return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
使用:

return str_replace( "post_type = \'post\'" , "post_type = \'YOUR_CUSTOM_TYPE\'" , $where );
并将您的\\u CUSTOM\\u TYPE更改为实际的自定义帖子类型名称。

更新:

如果需要除页面以外的所有类型,请将函数更改为:

add_filter( \'getarchives_where\' , \'ucc_getarchives_where_filter\' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
  $args = array(
    \'public\' => true ,
    \'_builtin\' => false
  );
  $output = \'names\';
  $operator = \'and\';

  $post_types = get_post_types( $args , $output , $operator );
  $post_types = array_merge( $post_types , array( \'post\' ) );
  $post_types = "\'" . implode( "\' , \'" , $post_types ) . "\'";
  //if page is somewhere in the middle then remove it 
  $post_types = str_replace("\'page\',","",  $post_types);
    //if page is somewhere the last type then remove it 
  $post_types = str_replace("\'page\'","",  $post_types);
  return str_replace( "post_type = \'post\'" , "post_type IN ( $post_types )" , $where );
}
要在循环中获得结果,请使用以下命令:

add_filter( \'pre_get_posts\' , \'ucc_include_custom_post_types\' );
function ucc_include_custom_post_types( $query ) {
  global $wp_query;

  /* Don\'t break admin or preview pages. This is also a good place to exclude feed with !is_feed() if desired. */
  if ( !is_preview() && !is_admin() && !is_singular() ) {
    $args = array(
      \'public\' => true ,
      \'_builtin\' => false
    );
    $output = \'names\';
    $operator = \'and\';

    $post_types = get_post_types( $args , $output , $operator );
    $post_types = array_merge( $post_types , array( \'post\' ) );

    //remove page form array:
    foreach($post_types as $key => $val){
        if ($val ==\'page\'){
            unset($post_types[$key]);
        }
    }

    if ($query->is_feed) {
      /* Do feed processing here if you did not exclude it previously. This if/else
       * is not necessary if you want custom post types included in your feed.
       */
    } else {
      $my_post_type = get_query_var( \'post_type\' );
      if ( empty( $my_post_type ) )
        $query->set( \'post_type\' , $post_types );
    }
  }

  return $query;
}

相关推荐