本文整理汇总了PHP中WP_Query::is_home方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Query::is_home方法的具体用法?PHP WP_Query::is_home怎么用?PHP WP_Query::is_home使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_Query
的用法示例。
在下文中一共展示了WP_Query::is_home方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: pmc_eve_query_category
/**
* Remove the featured category on the home page main query
* @param WP_Query $query
* @since 1.2
*/
function pmc_eve_query_category($query)
{
if ($query->is_home() && $query->is_main_query()) {
$cat = get_category_by_slug('featured');
if ($cat) {
$query->set('cat', '-' . $cat->term_id);
}
}
}
示例2: handle
public function handle(\WP_Query $query)
{
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_home()) {
//$query->set('posts_per_page', '10');
}
//$query->set( 'posts_per_page', '2' );
}
示例3: snihub_pre_get_posts
/**
* Pre get posts for customizing the post type
*
* @param WP_Query $query
* @return void
*/
function snihub_pre_get_posts($query)
{
if ($query->is_home() && $query->is_main_query()) {
$query->set('post_type', 'snippet');
$query->set('meta_query', array(array('key' => 'no_index', 'compare' => 'NOT EXISTS')));
if (get_query_var('hot_snippets', false)) {
$query->set('order', 'DESC');
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'likes');
}
if (get_query_var('snippets_by', false)) {
$query->set('author', get_query_var('snippets_by', '0'));
}
}
}
示例4: array
/**
* Get the fork's parent post, set up a query, and load correct template.
*
* Duplicates the functionality of /wp-includes/template-loader.php and includes
* a lot of copypasta, but that's only to ensure that it follows the same logic.
*
*/
function choose_template()
{
$p = get_queried_object_id();
if (get_post_type($p) !== 'fork') {
return;
}
$pp = get_post($p)->post_parent;
$parent = get_post($pp);
if ($parent->post_type == 'page') {
$query = array('page_id' => $pp);
} else {
$query = array('p' => $pp);
}
$t = new WP_Query($query);
$template = false;
if ($t->is_404() && ($template = get_404_template())) {
} elseif ($t->is_search() && ($template = get_search_template())) {
} elseif ($t->is_tax() && ($template = get_taxonomy_template())) {
} elseif ($t->is_front_page() && ($template = get_front_page_template())) {
} elseif ($t->is_home() && ($template = get_home_template())) {
} elseif ($t->is_attachment() && ($template = get_attachment_template())) {
remove_filter('the_content', 'prepend_attachment');
} elseif ($t->is_single() && ($template = get_single_template())) {
} elseif ($t->is_page && ($template = get_page_template())) {
} elseif ($t->is_category() && ($template = get_category_template())) {
} elseif ($t->is_tag() && ($template = get_tag_template())) {
} elseif ($t->is_author() && ($template = get_author_template())) {
} elseif ($t->is_date() && ($template = get_date_template())) {
} elseif ($t->is_archive() && ($template = get_archive_template())) {
} elseif ($t->is_comments_popup() && ($template = get_comments_popup_template())) {
} elseif ($t->is_paged() && ($template = get_paged_template())) {
} else {
$template = get_index_template();
}
if ($template = apply_filters('template_include', $template)) {
include $template;
}
return;
}
示例5: showInLoops
/**
* Checks where we are are and determines if we should show events in the main loop
*
* @param WP_Query $query
* @return WP_Query
* @since 2.1
*/
public static function showInLoops($query)
{
if (!is_admin() && tribe_get_option('showInLoops') && ($query->is_home() || $query->is_tag) && empty($query->query_vars['post_type']) && false == $query->query_vars['suppress_filters']) {
// 3.3 know-how for main query check
// if (method_exists($query, 'is_main_query')) {
if (self::is_main_loop($query)) {
self::$isMainLoop = true;
$post_types = array('post', TribeEvents::POSTTYPE);
$query->set('post_type', $post_types);
}
}
return $query;
}
示例6:
/**
* Exclude featured posts from the home page blog query.
*
* Filter the home page posts, and remove any featured post ID's from it.
* Hooked onto the 'pre_get_posts' action, this changes the parameters of
* the query before it gets any posts.
*
* @since Fastfood 0.37
*
* @param WP_Query $query WP_Query object.
* @return WP_Query Possibly-modified WP_Query.
*/
function pre_get_posts($query)
{
// Bail if not home or not main query.
if (!$query->is_home() || !$query->is_main_query()) {
return;
}
$query->set('ignore_sticky_posts', 1);
}
示例7: gp_set_is_home_false
/**
* Sets `WP_Query->is_home` to false during GlotPress requests.
*
* @since 1.0.0
*
* @param WP_Query $query The WP_Query instance.
*/
function gp_set_is_home_false($query)
{
if (is_glotpress() && $query->is_home() && $query->is_main_query()) {
$query->is_home = false;
}
}
示例8: graphene_exclude_slider_categories
/**
* Exclude posts that belong to the categories displayed in slider from the posts listing
*/
function graphene_exclude_slider_categories($request)
{
global $graphene_settings, $graphene_defaults;
if ($graphene_settings['slider_type'] != 'categories') {
return $request;
}
if (is_admin()) {
return $request;
}
if ($graphene_settings['slider_exclude_categories'] != $graphene_defaults['slider_exclude_categories']) {
$dummy_query = new WP_Query();
$dummy_query->parse_query($request);
if (get_option('show_on_front') == 'page' && $dummy_query->query_vars['page_id'] == get_option('page_on_front')) {
return $request;
}
if ($graphene_settings['slider_exclude_categories'] == 'everywhere' || $graphene_settings['slider_exclude_categories'] == 'homepage' && $dummy_query->is_home()) {
$request['category__not_in'] = graphene_object_id($graphene_settings['slider_specific_categories'], 'category');
}
}
return $request;
}
示例9: is_blog
/**
* If this is the home posts page; this will return
* false if the framework has swapped it out for a
* homepage layout from theme options. Checks against
* current WP_Query object at pre_get_posts.
*
* @since 2.3.0
*
* @param WP_Query $q Current WP_Query object being worked with
* @return bool
*/
private function is_blog($q)
{
if (!$q->is_home()) {
return false;
}
if (themeblvd_get_option('homepage_content') == 'posts') {
return true;
}
if (get_option('show_on_front') == 'page' && get_option('page_for_posts')) {
return true;
}
return false;
// Shouldn't get this far
}
示例10: alter_post_type_archive
/**
* Alternates the 'posts_per_page' query_var on post type archive pages
*
* @param WP_Query $query
*/
public function alter_post_type_archive($query)
{
if (is_admin() || !$query->is_main_query()) {
return;
}
if (!$query->is_home() && !$query->is_post_type_archive()) {
return;
}
$post_type = 'post';
if (!is_home()) {
// Get query var
$post_type = $query->get('post_type');
}
if (post_type_supports($post_type, $this->global_feature)) {
$posts_per_page = (int) g1_get_theme_option('post_type_' . $post_type, 'posts_per_page');
if (-1 === $posts_per_page || $posts_per_page > 0) {
$query->set('posts_per_page', $posts_per_page);
}
}
}
示例11: getPageObject
/**
* Parse which page we are on using URL
*/
public function getPageObject($pageUrl)
{
global $wp_rewrite;
// If post type, we are using url_to_postid function
$postId = url_to_postid($pageUrl);
if ($postId) {
$postType = get_post_type_object(get_post($postId)->post_type);
return array('value' => $postId, 'title' => get_the_title($postId), 'type' => get_post($postId)->post_type, 'label' => is_array($postType->labels) ? $postType->labels['name'] : $postType->labels->name);
}
$path = str_replace(get_site_url(), '', $pageUrl);
$path = trim($path, '/');
// If path is empty, then it is front page
if (empty($path)) {
return array('value' => get_option('page_on_front') ? get_option('page_on_front') : '', 'title' => '', 'type' => 'front_page', 'label' => __('Home Page'));
}
// Otherwise, we will try to match through rewrite or by query
$rewrite = $wp_rewrite->wp_rewrite_rules();
if (is_array($rewrite) && count($rewrite) > 0) {
foreach ($rewrite as $match => $query) {
if (preg_match("#^{$match}#", $path, $matches) || preg_match("#^{$match}#", urldecode($path), $matches)) {
$query = preg_replace("!^.*\\?!", '', $query);
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
parse_str($query, $query_vars);
break;
}
}
} else {
$query = preg_replace("!^.*\\?!", '', $path);
parse_str($query, $query_vars);
}
// Workaround for fail pagename rewrite match
if (isset($query_vars['pagename']) && strpos($query_vars['pagename'], '?') !== false) {
$query = preg_replace("!^.*\\?!", '', $query_vars['pagename']);
parse_str($query, $query_vars);
}
$querypost = new WP_Query($query_vars);
if ($querypost->is_date()) {
if ($querypost->query_vars['m']) {
$date = $querypost->query_vars['m'];
} else {
if ($querypost->is_day()) {
$date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2) . zeroise($querypost->query_vars['day'], 2);
} else {
if ($querypost->is_month()) {
$date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2);
} else {
if ($querypost->is_year()) {
$date = $querypost->query_vars['year'];
}
}
}
}
return array('value' => $date, 'title' => '', 'type' => 'archive', 'label' => __("Archive"));
} else {
if ($querypost->is_category() || $querypost->is_tag() || $querypost->is_tax()) {
$tax_query = $querypost->tax_query->queries;
$taxonomy = get_taxonomy($tax_query[0]['taxonomy']);
if ($tax_query[0]['field'] == 'term_id') {
$term_id = $tax_query[0]['terms'][0];
} else {
if ($tax_query[0]['field'] == 'slug') {
$term_id = get_term_by('slug', $tax_query[0]['terms'][0], $taxonomy->name)->term_id;
}
}
return array('value' => $term_id, 'title' => get_term($term_id, $taxonomy->name)->name, 'type' => $taxonomy->name, 'label' => is_array($taxonomy->labels->name) ? $taxonomy->labels['name'] : $taxonomy->labels->name);
} else {
if ($querypost->is_search()) {
return array('value' => $querypost->query_vars['s'], 'title' => '', 'type' => 'search', 'label' => __("Search"));
} else {
if ($querypost->is_home()) {
return array('value' => '', 'title' => '', 'type' => 'home', 'label' => __("Blog Home Page"));
}
}
}
}
}
示例12: pre_get_posts
/**
* Removes or adds products CPT in search
* @param WP_Query $query
*/
public function pre_get_posts($query)
{
if ($query->is_main_query()) {
global $etp_blog_grid;
if ($query->is_search && !empty($_GET['post_type'])) {
$post_types = $_GET['post_type'];
$query->set('post_type', $post_types);
}
$post_archive = $query->is_category() || $query->is_tag() || $query->is_home();
if ($post_archive) {
$etp_blog_grid = explode(',', $this->get('blog-grid', '1,10'));
$per_page = array_product($etp_blog_grid);
if ($this->get('blog-layout', 'left-image') && $per_page) {
$query->set('posts_per_page', $per_page);
}
}
}
}
示例13: hide_cats_from_homepage
public function hide_cats_from_homepage($exclude_cat_IDs_string, \WP_Query $query)
{
if ($query->is_home() && $query->is_main_query()) {
$query->set('cat', $exclude_cat_IDs_string);
}
}
示例14: defineLookupOrder
/**
* The rule of this ordering is: from the most specific to the least.
* Most of the default WP Template Hierarchy is the same, but not all is followed.
*
* For the full example of our lookup order plesase follow to:
*
* For the default WP hierarchy follow to:
* http://codex.wordpress.org/Template_Hierarchy
*
* @param WP_Query $wp_query
* @return array
*/
protected function defineLookupOrder(\WP_Query $wp_query)
{
$result = [];
if (!$wp_query) {
return $result;
}
// prepare vars
$post = !empty($wp_query->posts) ? $wp_query->posts[0] : false;
$post_type = $post ? $post->post_type : false;
$post_slug = $post ? $post->post_name : false;
$query_post_type = $wp_query->query_vars['post_type'];
if (is_array($query_post_type)) {
// it's not usual to have multiple post types on a rewrite rule
// but even if there is, it's extremely inconsistent to rely on
// a template name with multiple post types
// if that's the case, the user will have to alter the template
// order manually
$query_post_type = false;
}
// start the template hierarchy build up
if ($wp_query->is_404()) {
// 404-[post-type]
// 404
if ($query_post_type) {
$result[] = '404-' . $query_post_type;
}
$result[] = '404';
} elseif ($wp_query->is_search()) {
// search
// archive
$result[] = 'search';
$result[] = 'archive';
} elseif ($wp_query->is_front_page()) {
// if is page on front:
// front-page
// page
// singular
// if is posts on front:
// front-page
// home
// archive-[post-type]
// [post-type]
// archive
$result[] = 'front-page';
if ($post_type) {
if ($post_type !== 'page') {
$result[] = 'home';
$result[] = 'archive-' . $post_type;
$result[] = $post_type;
$result[] = 'archive';
} else {
$result[] = 'page';
$result[] = 'singular';
}
}
} elseif ($wp_query->is_home()) {
// home
// archive-[post-type]
// [post-type]
// archive
$result[] = 'home';
if ($post_type) {
$result[] = 'archive-' . $post_type;
$result[] = $post_type;
$result[] = 'archive';
}
// for now this is not needed, test more
// } elseif ($wp_query->is_post_type_archive()) {
// $result[] = 'archive-'.$query_post_type;
// $result[] = $query_post_type;
// $result[] = 'archive';
} elseif ($wp_query->is_author()) {
// author-[user-login]
// author-[user-nicename]
// author
// archive
if ($author = get_userdata($post->post_author)) {
$result[] = 'author-' . $author->data->user_login;
if ($author->data->user_login !== $author->data->user_nicename) {
$result[] = 'author-' . $author->data->user_nicename;
}
}
$result[] = 'author';
$result[] = 'archive';
} elseif ($wp_query->is_tax() || $wp_query->is_tag() || $wp_query->is_category()) {
// taxonomy-[taxonomy]-[term-slug]
// taxonomy-[taxonomy]
// taxonomy-[post-type]
//.........这里部分代码省略.........
示例15: pre_get_posts
/**
* Exclude featured posts from the home page blog query.
*
* Filter the home page posts, and remove any featured post ID's from it.
* Hooked onto the 'pre_get_posts' action, this changes the parameters of
* the query before it gets any posts.
*
* @static
* @access public
*
* @param WP_Query $query WP_Query object.
* @return WP_Query Possibly-modified WP_Query.
*/
public static function pre_get_posts($query)
{
// Bail if not home or not main query.
if (!$query->is_home() || !$query->is_main_query()) {
return;
}
$page_on_front = get_option('page_on_front');
// Bail if the blog page is not the front page.
if (!empty($page_on_front)) {
return;
}
$featured = self::get_featured_post_ids();
// Bail if no featured posts.
if (!$featured) {
return;
}
// We need to respect post ids already in the blacklist.
$post_not_in = $query->get('post_not_in');
if (!empty($post_not_in)) {
$featured = array_merge((array) $post_not_in, $featured);
$featured = array_unique($featured);
}
$query->set('post_not_in', $featured);
}