snipt

Ctrl+h for KB shortcuts

PHP

If page has children or has a parent

1
2
3
4
5
<?php
$children = get_pages('child_of='.$post->ID);
if( count( $children ) != 0 || ( is_page() && $post->post_parent )) {
    // Do this
}
https://snipt.net/embed/229df9c0eb322c069790e76398390b5e/
https://snipt.net/raw/229df9c0eb322c069790e76398390b5e/
229df9c0eb322c069790e76398390b5e
php
PHP
6
2016-09-24T10:53:27
True
False
False
Feb 16, 2016 at 12:10 PM
/api/public/snipt/144278/
if-page-has-children-or-has-a-parent
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$children</span> <span class="o">=</span> <span class="nx">get_pages</span><span class="p">(</span><span class="s1">&#39;child_of=&#39;</span><span class="o">.</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="k">if</span><span class="p">(</span> <span class="nb">count</span><span class="p">(</span> <span class="nv">$children</span> <span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span> <span class="o">||</span> <span class="p">(</span> <span class="nx">is_page</span><span class="p">()</span> <span class="o">&amp;&amp;</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">post_parent</span> <span class="p">))</span> <span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="c1">// Do this</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
conditions, pages, wordpress
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.