snipt

Ctrl+h for KB shortcuts

PHP

WordPress: functions.php code to hide a single category of post from the home page

1
2
3
4
5
6
7
// No "Quick Tips" on the homepage
function preventHomepageTips($query) {
  if($query->is_home() && $query->is_main_query()) {
    $query->set('cat', '-40'); // 40 is Quick Tips's category ID
  }
}
add_action('pre_get_posts', 'preventHomepageTips');
https://snipt.net/embed/f16c5baa8f81233e018836aa2f6a699d/
https://snipt.net/raw/f16c5baa8f81233e018836aa2f6a699d/
f16c5baa8f81233e018836aa2f6a699d
php
PHP
7
2016-09-29T04:49:58
True
False
False
Dec 02, 2015 at 08:00 AM
/api/public/snipt/142799/
wordpress-functionsphp-code-to-hide-a-single-category-of-post-from-the-home-page
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">// No &quot;Quick Tips&quot; on the homepage</span> </span><span id="L-2"><a name="L-2"></a><span class="x">function preventHomepageTips($query) {</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> if($query-&gt;is_home() &amp;&amp; $query-&gt;is_main_query()) {</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> $query-&gt;set(&#39;cat&#39;, &#39;-40&#39;); // 40 is Quick Tips&#39;s category ID</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> }</span> </span><span id="L-6"><a name="L-6"></a><span class="x">}</span> </span><span id="L-7"><a name="L-7"></a><span class="x">add_action(&#39;pre_get_posts&#39;, &#39;preventHomepageTips&#39;);</span> </span></pre></div> </td></tr></table>
code, functions, wordpress
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.