You are viewing an old revision of this post, from June 3, 2015 @ 06:18:13. See below for differences between this version and the current revision.

Remove a category from the front-page.php

The correct way to do this is to make use of pre_get_posts which change the query vars just before the main query is executed. Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with pre_get_posts, visit the WP_Query codex page: [php]function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-1,-1347' ); } } add_action( 'pre_get_posts', 'exclude_category' );[/php]

Revisions

  • June 3, 2015 @ 06:18:43 [Current Revision] by PeterLugg
  • June 3, 2015 @ 06:18:43 by PeterLugg
  • June 3, 2015 @ 06:18:13 by PeterLugg

Revision Differences

June 3, 2015 @ 06:18:13Current Revision
Content
Unchanged: The correct way to do this is to make use of <a href="http:// codex.wordpress.org/Plugin_ API/Action_Reference/ pre_get_posts" rel="nofollow" ><code>pre_get_ posts</code></a> which change the query vars just before the main query is executed.Unchanged: The correct way to do this is to make use of <a href="http:// codex.wordpress.org/Plugin_ API/Action_Reference/ pre_get_posts" rel="nofollow" ><code>pre_get_ posts</code></a> which change the query vars just before the main query is executed.
Unchanged: Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with <code>pre_get_ posts</code>, visit the <a href="http:// codex.wordpress.org/Class_ Reference/WP_Query" rel="nofollow" ><code>WP_Query</code></a> codex page:Unchanged: Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with <code>pre_get_ posts</code>, visit the <a href="http:// codex.wordpress.org/Class_ Reference/WP_Query" rel="nofollow" ><code>WP_Query</code></a> codex page:
Deleted: [php]&lt;/pre&gt; 
 Added: [php]function exclude_category( $query ) {
Deleted: &lt;pre class=&quot;default prettyprint prettyprinted&quot; &gt;&lt;code&gt;&lt;span class=&quot;kwd&quot; &gt;function&lt; /span&gt;&lt;span class=&quot;pln&quot;&gt; exclude_category&lt; /span&gt;&lt;span class=&quot;pun&quot; &gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; $query &lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;)&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;{&lt;/span&gt;Added: if ( $query-&gt;is_home() &amp;&amp; $query-&gt;is_ main_query() ) {
Deleted: &lt;span class=&quot;kwd&quot; &gt;if&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; $query&lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;-&gt;&lt; /span&gt;&lt;span class=&quot;pln&quot; &gt;is_home&lt; /span&gt;&lt;span class=&quot;pun&quot; &gt;()&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;&amp;&amp; &lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; $query&lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;-&gt;&lt; /span&gt;&lt;span class=&quot;pln&quot; &gt;is_main_query&lt; /span&gt;&lt;span class=&quot;pun&quot; &gt;()&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;)&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; 
Deleted: $query&lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;-&gt;&lt; /span&gt;&lt;span class=&quot;kwd&quot; &gt;set&lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;(&lt;/span&gt; &lt;span class=&quot;str&quot; &gt;'cat'&lt; /span&gt;&lt;span class=&quot;pun&quot; &gt;,&lt;/span&gt; &lt;span class=&quot;str&quot; &gt;'-1,-1347'&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;);&lt;/span&gt;Added: $query-&gt;set( 'cat', '-1,-1347' );
Deleted: &lt;span class=&quot;pun&quot; &gt;}&lt;/span&gt;Added: }
Deleted: &lt;span class=&quot;pun&quot; &gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; 
 Added: }
Deleted: add_action&lt; /span&gt;&lt;span class=&quot;pun&quot; &gt;(&lt;/span&gt; &lt;span class=&quot;str&quot; &gt;'pre_get_ posts'&lt;/span&gt;&lt;span class=&quot;pun&quot; &gt;,&lt;/span&gt; &lt;span class=&quot;str&quot; &gt;'exclude_ category'&lt;/span&gt; &lt;span class=&quot;pun&quot; &gt;);&lt;/span&gt; &lt;/code&gt; &lt;/pre&gt;Added: add_action( 'pre_get_posts', 'exclude_category' );[/php]
Deleted: &lt;pre&gt;[/php] 

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: , ,

No comments yet.

Leave a Reply