1

I'm using a custom HTML CSS menu which I like on a WordPress theme. Since I don't know how to make it work from the backend (at all), I'm trying to hide menu items when visiting its page with the help of the page title.

I found out that $single_post_title is the page title in WordPress. So I tried to hide menu items using following code. I used this for other menu items as well.

<?php
    $port = "Portfolio";
    if ($port != $single_post_title) {
        $port = "Portfolio";
} else {
?>
   <style type="text/css">
       #portfolio{
       display:none;
}  </style>
<?php 
}
?>

The menu code:

 <div class="col-md-9" class="fancymenu"> 
    <a id="#portfolio" href="/Portfolio/"><?php echo $port ?></a>
    <a id="#about" href="/About-Me/"><?php echo $about ?></a>
    <a id="#contact" href="/Contact-me/"><?php echo $contact ?></a>
 </div>

When I keep playing around with this, sometimes it hides (when replacing != with ==) & sometimes it doesn't.

I hope someone could help me figure this out.

Thanks heaps!

1 Answer 1

2

If you don't mind, I'm going to pick apart your code a bit (don't worry, it's to help you!)

There's a couple issue's you're dealing with (and going to be dealing with). First of all, I'd seriously consider sticking within the WordPress ecosystem if you're going to use it, unless you're going Headless with it.

Barring that, let's continue. In general, you shouldn't rely on variables that you haven't defined in your current scope yourself. Who knows where $single_post_title was defined, or if it even was.

WordPress has some relatively 'nicely' named PHP functions to interact with Posts and Pages (many of which will show up as the first result in Google for a search like "Get the current page title in WordPress")

The standard way of getting a page/post title is with the get_the_title() function.

Another issue is that it's quite a mess to load in some CSS to hide an element if you have access to the element's source code, just skip the element conditionally!

Lastly, generally speaking ID's don't need (or rather just shouldn't have) a pound sign in front of them. Languages that target them will put that in there for you. Something else to consider is your hard coded href attributes have upper case letters, like it's a combination of a slug and title. I'd clarify what's what.

All what I said in mind, I'd drop the first block of code you have, and replace the menu code with the following:

<?php $current_page_title = get_the_title();
    echo '<div class="col-md-9" class="fancymenu">';
        if( $current_page_title != 'Portfolio' ) echo '<a id="portfolio" href="/Portfolio/">Portfolio</a>';
        if( $current_page_title != 'About Me' ) echo '<a id="about" href="/About-Me/">About Me</a>';
        if( $current_page_title != 'Contact Me' ) echo '<a id="contact" href="/Contact-me/">Contact Me</a>';
    echo '</div>';
?>

You could make it even easier to maintain with a simple loop:

<?php
    // Get the current Page Title
    $current_page_title = get_the_title();

    // Define our Pages as an Array
    $pages = array( 'Portfolio', 'About Me', 'Contact Me' );

    // Loop through that array
    foreach( $pages as $page ){
        if( $current_page_title != $page ){
            // In here, it means the "page" from the array isn't the "current page"
            // that's being viewed, so echo out the item.
            echo '<a id="'. sanitize_title( $page ) .'" href="'. sanitize_title( $page ) .'">'. $page .'</a>';
        }
    }
?>

Note the loop as it stands will replace your ID's like about with about-me.

So, now that you've read through allll of that stuff - I'd really consider learning how the WordPress features work, as the Navigation Menu feature handles all of this stuff (and more) for you, and will make maintaining the site in the future much easier. Your current method is more of an HTML/PHP template that uses WordPress solely as the CMS to manage page/post content, which can get cumbersome later unless it's by design.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you heaps! This is by far the best support I've ever received online. You helped me more than you can ever imagine!!! Learned a lot & thanks so much. Can I send you an iTunes card or something to appreciate your support? Cheers
My pleasure! While I do have a kofi link buried somewhere, don't feel the need to pay me for this answer - it's hardly worth it. I'd get more joy out of getting just one more person started as a developer! Feel free to reach out if you need any further assistance or want additional resources to get you started!
Do send me your Kofi link when you find it! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.