1

I am trying to conditionally change a link based on what page a user is on, but I am having trouble finding the correct code to perform the action.

Here is what I want to do (In English to explain the code)

If a user is not logged-in and on a page that is either keyword or a child of keyword (for example, domain.net/keyword/pricing or domain.net/keyword/about or simply domain.net/keyword)

Then Show X

If user is not logged in and on any other page besides those mentioned above

Then Show Y

If user is logged in

Then Don't do anything

I've tried multiple conditionals and read the wordpress codex, but can't figure this one out. I am novice php user, so I wouldn't be surprised if the answer was quite simple.

I appreciate the help in advance!

1

If it is about child post types* or taxonomies**:

// page/post/cpt parent check:
$post_object = get_queried_object();
// check if the page has a parent
if ( $post_object->post_parent )
    // do stuff

// cat/tax parent check:
$taxonomy_object = get_the_category( get_query_var('cat') );
// check if the cat/tag/tax has a parent:
if ( $taxonomy_object->parent )
    // do stuff

It could be that you have to check the if against 0 !== $post_/taxonomy_object, but it should work close to shown above.


*) Post types are the built in ones like post and page, or custom ones like custom post types.

**) Taxonomies are the built in ones like category (hierarchical), tags (non-hierarchical), post_format, etc, or your custom ones like custom taxonomies.

0

I'm assuming you are referring to pages and child pages. If so, you can use this in your template within the loop:

$page_title = "Foo";
global $post;
$parent = $post->post_parent;

if(is_page($title))
    echo "bar";
if($parent){
    $parent = get_post($parent);
    if($parent->post_title == $title)
        echo "bar";
}
if(is_user_logged_in())
    echo "Foo Bar";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.