检测你是否在一个帖子页面上的最好方法

时间:2013-10-08 作者:Eric Holmes

所以这似乎是一件很卑鄙的事情,但请跟我来。

我想通过pre_get_posts 行动这是给我的整个WP\\U查询对象。(见结尾)

我考虑使用的东西:

  • is_single() - 太宽了
  • is_singular() - 现在使用这个还为时过早,因为get_queried_object() 尚未设置
  • $query->single 财产——再次过于宽泛
  • $query->get(\'post_type\') - 未设置,因为它使用name 属性
name 真的是这里唯一的指标吗?

WP_Query Object
(
    [query] => Array
        (
            [page] => 
            [name] => abcs-of-mental-health
        )

    [query_vars] => Array
        (
            [page] => 
            [name] => abcs-of-mental-health
            [error] => 
            [m] => 0
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [menu_order] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

        )

    [tax_query] => 
    [meta_query] => 
    [queried_object] => 
    [queried_object_id] => 0
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 1
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 1
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => f473ebf7f725c2627dc5fd9a1429f626
    [query_vars_changed] => 
    [thumbnails_cached] => 
)

4 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

为了我自己的目的,我曾经尝试过解决这个问题。据我所知。。。

  • post_type 没有为post 岗位类型page post type我只看到post type键queried_object.post_type 输入query_vars 而且在query.
数据非常不一致,但如果您删除页面和CPT,我相信您可以假设post 类型

编辑:@EricHolmes的工作代码:

add_action( \'pre_get_posts\', \'something_for_single_posts_only\' ) ; 
function something_for_single_posts_only( $query ) { 
  if( $query->is_main_query() 
    && $query->is_singular() 
    && ! $query->get( \'post_type\' ) 
    && ! $query->is_page() 
    && ! $query->is_attachment() 
  ) { 
      // do something for single posts only. 
  } 
} 
我们检查is\\U单数,无post类型(CPT有post_type 在里面query_vars), 不是页面或附件。

SO网友:ferenyl

我不知道这是否有用:

function hwl_home_pagesize( $query ) {
    global $wp_query;
    if (is_main_query() && count($wp_query->posts) < 2) {

    }

}
add_action( \'pre_get_posts\', \'hwl_home_pagesize\', 1 );
使用$wp\\u query->posts(数组)检查帖子类型。

SO网友:gmazzap

经过一些测试后,我发现很遗憾,不可能在内部获得cpt的post类型pre_get_posts 钩只有is_page 在那里可以检索到作品,但不能检索到标准岗位类型或cpt。

如果您只有page and post(无cpt),请检查is_single() 具有true as response意味着post类型是post,因为它对页面返回false。

如果你也有CPT恐怕you have to perform an additional query. 我能想到的最简单的方法就是post_type post status为publish且post\\U name为必填项的列(跳过修订):

function test( $q ) {
  if ( is_single() ) {
    global $wpdb;
    $type = $wpdb->get_var( $wpdb->prepare(
      "SELECT post_type FROM $wpdb->posts WHERE post_name = %s AND post_status = \'publish\' AND post_type <> \'revision\'",
      $q->query[\'name\']
    ) );
    var_dump($type);
  }
}
add_action( \'pre_get_posts\', \'test\', 1); 
如果要检查特定的post类型,可以编写一个自定义条件标记,该标记只对具有给定post\\u类型和给定名称的行进行计数:

function is_single_post_type( $type = \'post\' ) {
  if ( is_single() ) {
    global $wpdb, $wp_query;
    $is = $wpdb->get_var( $wpdb->prepare(
      "SELECT count(ID) FROM $wpdb->posts WHERE post_name = %s AND post_status = \'publish\' AND post_type = %s",
      $wp_query->query[\'name\'], $type
    ) );
    return $is > 0;
  }
  return false;
}
当然这是必要的pre_get_post, 在以后的任何挂钩中,您都可以使用get_post_type()...

SO网友:Imperative Ideas

这就是我正在使用的内容,尽管它专门用于我自己的目录结构。

/**
 * Function to list all templates used in a page
 * @author Imperative Ideas with thanks to Rarst
 * @uri http://wordpress.stackexchange.com/a/89005
 */

function thelist() {
    $included_files = get_included_files();
    $stylesheet_dir = str_replace( \'\\\\\', \'/\', get_stylesheet_directory() );
    $template_dir   = str_replace( \'\\\\\', \'/\', get_template_directory() );
    echo \'<h3 class="debugtitle">Theme file templates used in this page</h3>\';
    foreach ( $included_files as $key => $path ) {

        $path   = str_replace( \'\\\\\', \'/\', $path );

        if ( false === strpos( $path, $stylesheet_dir ) && false === strpos( $path, $template_dir ) )
            unset( $included_files[$key] );

        if(!strpos($path, \'/wp-content/themes/\') === false) { // Files IN this directory
            if(strpos($path, \'/library/\') === false) { // Ignore this subdir
                if(strpos($path, \'/hybrid-core/\') === false) { // Ignore this subdir
                    echo $key." = ". $path."</br>"; // Finally, output the list
                }
            }
        }
    }
}
里程可能会有所不同。我检查文件是否在一个目录中,而不是在另一个目录中的strpos位需要为您的构建进行修改,并且可能需要更有效地重新分解。它们的存在是为了在某个目录结构的上方和下方剪切结果。

在页脚中运行list()将为您提供一个编号的列表。用于编译当前视图的php模板文件。当处理呈现神秘组件的子主题时,它特别有用。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post