• Resolved harshclimate

    (@harshclimate)


    I have 4 links on my main nav but two aren’t accepting .current-menu-item for some reason.

    HOME (using custom url)
    PHOTOGRAPHY (CPT archive page archive-photography.php)
    FIELD JOURNAL(CPT archive page archive-fieldjournal.php)
    INFORMATION (using page.php)

    photography and field journal aren’t highlighting for some reason. I’m assuming that because I set up the CPT’s as pages instead of just leaving them as an archive is messing with things? But that has never happened to me before. I’ve done this many times. Here’s a bit more info…

    (header.php)

    <?php $args = array('theme_location' => 'primary'); ?>
    <?php wp_nav_menu($args); ?>

    (functions.php)

    register_nav_menus(array(
      'primary' => __('Primary Header Menu'),
      'footer'  => __('Primary Footer Menu')
    ));

    (HTML output)

    <ul>
      <li id="menu-item-76" class="menu-item menu-item-type-custom menu-item-object-custom 
      current-menu-item current_page_item menu-item-home menu-item-76"><a 
      href="https://localhost/wordpress">Home</a>
      </li>
      <li id="menu-item-281" class="menu-item menu-item-type-post_type menu-item-object-
      page menu-item-281"><a href="https://localhost/wordpress/photography/">Photography</a>
      </li>
      <li id="menu-item-284" class="menu-item menu-item-type-post_type menu-item-object-
      page menu-item-284"><a href="https://localhost/wordpress/field-journal/">Field 
      Journal</a>
      </li>
      <li id="menu-item-119" class="menu-item menu-item-type-post_type menu-item-object-
      page menu-item-119"><a href="https://localhost/wordpress/information/">Information</a>
      </li>
    </ul>
Viewing 11 replies - 1 through 11 (of 11 total)
  • Custom theme?

    Moderator bcworkz

    (@bcworkz)

    Hiya harshclimate,

    By CPT archives being setup as pages, do you mean the archive query is a new custom WP_Query placed on a page template? So the menu item is the “Page” type, as labeled at the right end of the item widget in the Menu Structure area? That shouldn’t be a problem, the menu item when current should have both current-menu-item and current_page_item classes. WP doesn’t care what the page does, it’s a page.

    To determine why the current_* class is not assigned, you could step through _wp_menu_item_classes_by_context() to see why the items are passed over. It’s probably more productive to simply add your own current page check for these particular items. Use the ‘wp_nav_menu_objects’ filter. Get the current queried object and see if it matches up with any of the menu objects. When it does, add ‘current-menu-item’ to the classes list if it is not already there.

    Thread Starter harshclimate

    (@harshclimate)

    bc! Yeah, I’m using custom queries, but I did reset posts at the end of each…

    Thread Starter harshclimate

    (@harshclimate)

    Okay, so the other thing is when I test the page with an if statement, it doesn’t echo out which page it is.

    <?php if(is_page('photography')) { echo 'this is the photography page'; } ?>

    This statement returns nothing. but when I change
    is_page to is_archive('photography') it does return true.

    • This reply was modified 6 years, 9 months ago by harshclimate.
    Thread Starter harshclimate

    (@harshclimate)

    Okay, I know this sucks because it’s not very dynamic, but I could just make two custom links that go to like https://www.website.com/photography and then it’ll use current-menu-item. I just feel like I’m still doing something wrong with either naming conventions or maybe I build my CPT’s incorrectly.

    I’m ready for bed :/

    Moderator bcworkz

    (@bcworkz)

    Bed?? Coders don’t sleep! Sleep is for noobs ??

    kidding of course, good on you for looking after yourself.

    Custom queries are fine, I just wanted to confirm I was correctly grasping the situation. The menu code runs before those, so menus are only seeing a single page query. So you have a page slug that matches the category term queried on the page? That’s fine too, just checking.

    Where are you checking is_page()? Template tags like that reflect the current global queried object, not a custom query. It sounds like the global query is not for a page like I thought it would be.

    You said in the OP “I set up the CPT’s as pages”. What precisely does that mean? Only pages can be pages. Please post one of your CPT addition code.

    The post object with the slug “photography”. Is that a true WP page or a CPT “page”? Are there any other objects assigned the photography slug besides the page and category term? A CPT perhaps?

    Thread Starter harshclimate

    (@harshclimate)

    question 1) I was checking is_page in the header. Since I wanted to find out where the problem was in the main menu, I wanted to stick to a place I thought I had to be.

    question 2) The CPT’s are using a page template (or were). I’ve changed those to archive-(slug) instead of page-(slug).

    Registered CPT Code

    <?php // This is the Photography custom post type ?>
    <?php
        $CPT_name = 'Photography';
        $CPT_singular = 'Photographs';
        $CPT_lowercase = 'photography';
    
        function CPT_Photography() {
        	$labels = array(
        		'name' => $CPT_name,
        		'singular_name' => $CPT_singular,
        		'add_new' => 'Add New Entry',
        		'add_new_item' => 'Add New Entry',
        		'edit_item' => 'Edit ' . $CPT_name,
        		'new_item' => 'New ' . $CPT_name,
        		'view_item' => 'View ' . $CPT_name,
        		'search_items' => 'Search ' . $CPT_singular,
        		'not_found' =>  'None found',
        		'not_found_in_trash' => 'Not found in trash.',
        		'parent_item_colon' => '',
        		'menu_name' => 'Photography'
        	);
    
        	$args = array(
        		'labels' => $labels,
        		'public' => true,
        		'publicly_queryable' => true,
        		'show_ui' => true,
        		'show_in_menu' => true,
                'show_in_nav_menus' => true,
        		'query_var' => true,
        		'rewrite' => true,
        		'capability_type' => 'post',
        		'has_archive' => true,
        		'hierarchical' => true,
        		'menu_position' => 4,
                'menu_icon' => 'dashicons-camera',
        		'supports' => array('title', 'thumbnail', 'editor', 'custom-fields')
        	);
        	register_post_type( 'Photography', $args );
        }
        add_action( 'init', 'CPT_Photography' );
    ?>

    I’m using CPT’s in a plugin so i could keep reusing it without much ado.

    question 4) As far as I know, there are no other objects assigned to photography. Unless stuff in the trash in admin might be conflicting with what’s actually live.

    Well, to my defense, sleep is only a subjective term. While I might say ‘sleep’ I really mean go to bed and stay awake for hours then fall asleep and wake up millions of times throughout the night! YUCK!

    Moderator bcworkz

    (@bcworkz)

    Thanks for clarifying. That’s not what I expected at all! It’s all fine (other than the menu not working), I was just thrown off by “page” terminology. Even though a page template may be used for displaying content, that does not make it a “page” as far as WP coding goes. Only page post types can be considered pages regardless of template used.

    You need to move the following lines to inside the CPT_Photography() function declaration:

        $CPT_name = 'Photography';
        $CPT_singular = 'Photographs';
        $CPT_lowercase = 'photography';

    I tried testing your code on my site and it wouldn’t even load right with those lines on the outside. Once moved inside, all was fine. Oh, I also visited my permalinks settings screen (no changes made) to cause the rewrite rules to be regenerated. Be sure to always do this after adding any code affecting generated permalinks.

    When you added the menu item for photography, did you use the custom link option and manually enter the desired photography archive URL? That’s what I did after placing your code and moving those lines. The resulting menu item had the .current-menu-item class without doing anything else special.

    Try moving those lines and visiting the permalinks setting screen. Then check the menu HTML again after navigating to the photography archive list (do a manual reload first). If the class is still missing, there’s probably a conflict between your theme or plugins. You can use the health-check plugin’s troubleshooting tab to invoke an out of the box state. Once you’re in troubleshooting mode, restore whatever module contains the CPT code. If the class is still missing after reloading the photography archive page, the conflict is within your module itself.

    If the class appears with only your module active, restore other modules one by one until the class again disappears. The last activated module is causing a conflict.

    Off topic sidebar:
    So sorry to hear you suffer from sleep issues. I can relate all too well. The fact we’re in good company doesn’t make it any better. It seems like I’ve tried everything to get a good night’s sleep. None of the measures were satisfactory for one reason or another. I’ve given up trying and just try to live with it. I’m told by docs that’s not a viable solution. They don’t have to suffer the side effects (and addiction in some cases) of the stupid pills they keep trying to push on me. While modern medicine is often miraculous, it’s also a failure in some very basic situations.

    Thread Starter harshclimate

    (@harshclimate)

    Your sidebar: Yeah, I hear that. I keep hearing everything under the sun to help me sleep. The only thing that was suggested was that I get more exercise, but the kind of work I do is so demanding from 7am to 4pm that I feel too unmotivated to start working out, or whatever. I’m a plumber at hospital of all things. I get about 10k steps at work, but since I’ve been doing it for 7 years, my body is pretty used to it. I need to mix it up.

    One of the major issues that I had was trying to slow my brain down. I tend to bring work home (in my head) and I get too stressed. I’ve been on anti-depressants to calm me down. They help a lot in my day to day but not for sleep. But they do work wonders slowing things down a bit and preventing me from getting pissed off at myself for dropping a paper clip!

    Code helps. I am no coder at all. I love doing HTML and css. It’s something that really helps me forget my life. PHP is something I’ve been trying to do for YEARS and just can’t grasp it. I’m more of a right brained person and can’t use the left to save my life. I figure I’m okay with that. The world needs both left and right brained people. I get very meticulous with my HTML. I’ve been known to design sites about 25 times until I’ve used the least amount of HTML and CSS markup possible. So I guess OCD plays a huge role ??

    Thanks for the tip on the CPT stuff. I’ll give it whirl and make those moves. I do the permalink refresh every time I make changes to the CPT’s so that’s all good.

    Feel free to post snippets to streamline anything I post. I post quite a bit trying to figure junk out!

    PS -> Moving those variables inside the function DID WORK! You rock! Now, I wonder what else I messed up?!? I should send you my function.php file so you can clean it up! LOL

    • This reply was modified 6 years, 9 months ago by harshclimate.
    • This reply was modified 6 years, 9 months ago by harshclimate.
    Moderator bcworkz

    (@bcworkz)

    Glad to hear that little move solved it! I wasn’t sure it would. They’re just labels after all. I guess the nav menu routine uses labels somehow to determine the current page. Either that or the error broke something farther down.

    It’s a good idea to code PHP with WP_DEBUG defined as true in wp-config.php. Just don’t leave it that way on a production server for an extended period if you aren’t coding. It’s a slight security risk. The best thing to do for PHP work is set up a local WP installation on your own computer where you can try things out without fear of messing up anything important. Taking the FTP or the WP editor out of the loop is a much larger help than anyone could imagine until they’ve tried it. You’ll never go back.

    PHP will tell you you’ve done something obviously wrong like trying to use external variables within a function. There’s plenty it won’t catch, but it really helps. Unless you run Linux on your computer, you need a virtual machine to setup a local WP installation. A lot of devs swear by “Local by Flywheel”. I’ve not used it myself, as I have Linux running natively.

    I’ve news for you. If you do HTML and CSS, you’re a coder ?? So you don’t speak PHP very well. Lots of coders don’t. OK, they aren’t programming languages like PHP, there is no logical branching. It’s still computer code none the less. Pedantics insist PHP isn’t a true programming language either, it’s scripting. If you are good at CSS, you have my respect. I’ve often no idea how to get something working in CSS. It’s just a long string of try something and see if it works efforts.

    I guess PHP can be no different. Somehow it makes more sense to me. For me, the best way to learn a new programming language is to cook up some sort of project that forces you to figure things out. The trick is to find one that’s a bit beyond your abilities but not so much that it all seems hopeless. But even large projects can be broken down into attainable parts. Attack one small part at a time and eventually it’ll all come together. The result may be atrocious in organization and style, but it’ll work and you will have learned a ton.

    The other good way to learn is help out others in these forums! Especially this “Developing” one where there’s lots of code. Even if you can’t answer, figuring out what other’s code is trying to do is instructive. As you surely realize, just keeping at it is the most important thing to do. It’ll sink in eventually if you’re determined enough. Only fools and liars would say coding is easy. It takes a good effort.

    That’s probably about as far of topic we should wander in this thread. I’ve more thoughts on sleep, though nothing you haven’t heard, I’m sure. Anyway, it’s not for here. If you would like to continue off topic, you’re welcome to join WP Slack and direct message me there. Same username. This is not an invitation to personalized WP help. That stays here in the forums. Slack is a chat-like platform where all the hardcore WP nerds hangout in various public channels. Forum mods and other forum regulars use #forums channel. Everyone is welcome, but there’s no support there. Chat support has remained on IRC.

    Thread Starter harshclimate

    (@harshclimate)

    I have slack! I’ll see you there.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘.current-menu-item not showing up on two links’ is closed to new replies.