Jump to content

Only show if var exists


OhTheNoes

Recommended Posts

I apologize in advance for any misuse of terminology. I find that I am rather ignorant in such things.

 

I'm working on a WordPress theme. Specifically on the part that shows the "next: link-to-next-post" and "previous: link-to-prev-post" links.

 

Here is the code that I have:

<?php
  $nextvar=next_post_link('%link');
  $prevvar=previous_post_link('%link');
?>

<?php if (isset($nextvar)) { ?>
<h3>Next</h3>
<p><?php next_post_link('%link'); ?></p>
<? } ?>

<?php if (isset($prevvar)) { ?>
<h3>Previous</h3>
<p><?php previous_post_link('%link'); ?></p>
<? } ?>

 

The first part intends to check if the links exist. And this works fine.

 

However, the only output I'm getting are the values of $nextvar and $prevvar. The headers are nonexistent. What's wrong?

 

Any help is much appreciated, and I thank you in advance. :shy:

Link to comment
https://forums.phpfreaks.com/topic/231888-only-show-if-var-exists/
Share on other sites

Your code works for me, but only if I put the full php tag '<?php' not just '<?'.. I didn't use the next_post_link() functions, I just made each of those variables equal to a string, so as to force the set or unset state. And echoed accordingly.. So just try changing the php tags before the ending curley braces.

 

Denno

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.