Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set a link based off a post_parent being set - WordPress
    primarykey
    data
    text
    <p>I'm learning as I go here and wanted to reach out for a better understanding of how to handle an if statement within WordPress regarding the Parent being set or not.</p> <p><strong>What I'm trying to do:</strong></p> <p>I'm attempting to set the URL for an element based off the Parent being set for a page within the "Page Attributes" section. As it currently stands, if a Parent is set for the page, it will update the href value based off the homepage of the parent. However, if no parent is set, it is populating the page URL as the parent.</p> <p><strong>What I want it to do:</strong></p> <p>If no parent is set, echo home_url(). This will have it default to the homepage URL if no Parent is set.</p> <p><strong>Original version:</strong></p> <pre><code>&lt;?php $permalink = get_permalink($post-&gt;post_parent); ?&gt; </code></pre> <p><strong>Newer version (that needs TLC to work):</strong></p> <pre><code>PHP: &lt;?php if ($post-&gt;post_parent) { $permalink = echo get_permalink($post-&gt;post_parent); } else { $permalink = home_url(); } ?&gt; HTML: &lt;a href="&lt;?php echo $permalink; ?&gt;"&gt;Example&lt;/a&gt; </code></pre> <p>Currently, it's not working for the else statement. If I attempt to echo any type of text for the else statement, it appends it to the vainty URL set for the page I'm actively viewing.</p> <p><strong>What I need the function to do:</strong></p> <pre><code>&lt;?php if (a page/post has a parent set within the page attribute) { $permalink = echo get_permalink($post-&gt;post_parent); } else (if a page/post does not have a parent set within the page attribute) { $permalink = echo home_url(); } ?&gt; </code></pre> <p>Any help would be greatly appreciated! Thanks!</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.
 

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