WordPress.org

Ready to get started?Download WordPress

Forums

Using is_page to display parent page lin in a menu (5 posts)

  1. LucyRyder
    Member
    Posted 1 year ago #

    Hello there!
    I am using the Child Page navigation plugin for child page menus on the 2nd level pages of my site:
    http://www.thedancestudioleeds.com/courses-classes/
    (right hand teal menu)

    I am trying to add some code to the plugin which will display also the parent page link at the top of this child page link menu, but only on certain 2nd level pages.

    The code needs to basically say:
    "if is_page id 6, display the parent page link as the first
    <li> in this child page menu, and give it a class of <li class="current_page">"
    then:
    "if is pages 8,10, or 33, again display the parent page link as the first </li>
    <li>, but do not then give it the class."
    I'm trying this on my local version o the site which unfortunately i haven't hooked up t a live URL for you to see, but So far I have the link appearing correctly at the top of the child page menu when on the parent page, but can't get it to work on the other child pages when the parent links still needs to be at the top of the menu. The code I have tried is:

    $parent_title = get_the_title($post->post_parent);
                        if (is_page(6)){
                         echo "<li class='current_page'><a href='".get_permalink($post->post_parent)."'>$parent_title</a></li>";
    
                    }
    				if (is_page(array( 8, 10, 33 ))){
                         echo "<li><a href='".get_permalink($post->post_parent)."'>$parent_title</a></li>";
    
                    }

    Although the first if statement seems to work, I also don't know if just using $parent_title in the `</li>
    <li></li>`
    tag is correct?

    Any help would be massively appreciated!!!!

    Thanks in advance kind coders :)

    [Moderator Note: Please post ALL code or markup snippets between backticks or use the code button. As it stands, your post may now have been permanently damaged/corrupted by the forum's parser.]

  2. bcworkz
    Member
    Posted 1 year ago #

    If your code is being called from within a query loop, the is_page() function is unreliable. See Function_Reference/is_page#Cannot_Be_Used_Inside_The_Loop

  3. LucyRyder
    Member
    Posted 1 year ago #

    HI, thank for your reply - I'm not sure if the is_page would be inside The Loop - I am wanting to tweak an existing plugin Child Page Navigation with my extra code and there is no Loop in the plugin file...how would i find out if it would be inside The Loop or not? And your link doesn't tell me what to use instead?

    Many thanks! I'm not a PHP developer so it's a bit of guess work on my part :)

  4. LucyRyder
    Member
    Posted 1 year ago #

    I've now tried to code below, which works on the first 'if statement' of displaying the parent page link in my secondary side menu - when on the parent page.
    The for the 'elseif statement' i'm trying to say; if on a childpage of this parent, still display the parent page link; but don't give it a class of current page. This second statement does not work.

    $parent_title = get_the_title($post->post_parent);
    		if(is_page('page-2b')){
    		echo "<li class='current_page'><a href='".get_permalink($parent_title->ID)."'>".get_the_title($parent_title->ID)."works</a></li>";
    		}
    
    		elseif (is_page('page-2b') || $post->post_parent=="20") {
    		echo "<li><a href='".get_permalink($parent_title->ID)."'>".get_the_title($parent_title->ID)."works</a></li>";
                     }
    		else {}

    (this 2nd attempt is different to me instead trying to list the child pages on the second if/elseif as above)

    Anyone for any suggestions pleeeeaaaasssseee? - I feel like i'm nearly there, but oh so far!

    Many thanks :)

  5. LucyRyder
    Member
    Posted 1 year ago #

    * sorry the 'works' in the list item to be output was just for testing i know that's not needed!*

Topic Closed

This topic has been closed to new replies.

About this Topic