snipt

Ctrl+h for KB shortcuts

PHP

Basic Wordpress Loop

1
2
3
4
5
6
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
https://snipt.net/embed/99701b047aa99f02d82e462612f20cd2/
/raw/99701b047aa99f02d82e462612f20cd2/
99701b047aa99f02d82e462612f20cd2
php
PHP
7
2019-07-20T01:52:25
True
False
False
/api/public/snipt/45776/
basic-wordpress-loop
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">if</span> <span class="p">(</span> <span class="nx">have_posts</span><span class="p">()</span> <span class="p">)</span> <span class="o">:</span> <span class="k">while</span> <span class="p">(</span> <span class="nx">have_posts</span><span class="p">()</span> <span class="p">)</span> <span class="o">:</span> <span class="nx">the_post</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="cp">&lt;?php</span> <span class="k">endwhile</span><span class="p">;</span> <span class="k">else</span><span class="o">:</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-5"><a name="L-5"></a><span class="x">&lt;p&gt;</span><span class="cp">&lt;?php</span> <span class="nx">_e</span><span class="p">(</span><span class="s1">&#39;Sorry, no posts matched your criteria.&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&lt;/p&gt;</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">&lt;?php</span> <span class="k">endif</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
Loop, Wordpress

PHP

Control Wordpress posts per page

1
2
3
4
5
6
    <?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('showposts=20&paged='.$page_num); ?>
         
    
https://snipt.net/embed/d1230e43e996fccde11a5dfb423aed02/
/raw/d1230e43e996fccde11a5dfb423aed02/
d1230e43e996fccde11a5dfb423aed02
php
PHP
6
2019-07-19T10:58:59
True
False
False
Aug 16, 2012 at 09:09 AM
/api/public/snipt/45775/
control-wordpress-posts-per-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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x"> </span><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$page_num</span> <span class="o">=</span> <span class="nv">$paged</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="nv">$pagenum</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="nv">$pagenum</span> <span class="o">=</span><span class="mi">1</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="nx">query_posts</span><span class="p">(</span><span class="s1">&#39;showposts=20&amp;paged=&#39;</span><span class="o">.</span><span class="nv">$page_num</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></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></pre></div> </td></tr></table>
Wordpress, loop, php, posts