Note that there are some explanatory texts on larger screens.

plurals
  1. POConditionally alter next_posts_link() / previous_posts_link() in Wordpress
    primarykey
    data
    text
    <p>I have an archive template I'm working on, and at the bottom of the page I'm using </p> <pre><code>&lt;?php next_posts_link("&amp;laquo; Older Posts", $wp_query-&gt;max_num_pages); ?&gt; &lt;?php previous_posts_link("Newer Posts &amp;raquo;", $wp_query-&gt;max_num_pages); ?&gt; </code></pre> <p>to display the pagination.</p> <p>These functions, it seems, check whether or not there are newer/older posts to display, and conditionally determines whether or not to display the prev/next links.</p> <p>The behavior I am trying to achieve is that if there are not older/newer posts to be displayed on a given page, That an anchor tag is still created, but not given an href attribute and given a separate class.</p> <p>i.e. - on a page that contains the most recent posts, I want to show</p> <pre><code>&lt;a class="previous-posts" href="THE_PREVIOUS_POSTS_PAGE"&gt;&amp;laquo; Older Posts&lt;/a&gt; &lt;a class="next-posts disabled"&gt;Newer Posts &amp;raquo;&lt;/a&gt; </code></pre> <p>instead of just an "Older Posts" link with nothing next to it (for layout reasons).</p> <p>Any ideas about either where I can edit the behavior of the default functions or how I can craft my own?</p> <p>UPDATE:</p> <p>Mike Lewis' answer was perfect for Next_posts, but I still seem to have messed up previous_posts.</p> <pre><code>&lt;?php if ($prev_url = previous_posts($wp_query-&gt;max_num_pages, false)){ // next_posts url was found, create link with this url: ?&gt;&lt;a href="&lt;?= $prev_url ?&gt;"&gt;&amp;laquo; Newer Posts&lt;/a&gt;&lt;?php } else { // url was not found, do your alternative link here ?&gt;&lt;a class="disabled"&gt;&amp;laquo; Newer Posts&lt;/a&gt;&lt;?php } ?&gt; &lt;?php if ($next_url = next_posts($wp_query-&gt;max_num_pages, false)){ // next_posts url was found, create link with this url: ?&gt;&lt;a href="&lt;?= $next_url ?&gt;"&gt;Older Posts &amp;raquo; &lt;/a&gt;&lt;?php } else { // url was not found, do your alternative link here ?&gt;&lt;a class="disabled"&gt;Older Posts &amp;raquo; &lt;/a&gt;&lt;?php } ?&gt; </code></pre> <p>The first function always displays the disabled link, while the second behaves exactly as expected. Any idea what's up here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload