• dlrobins

    (@dlrobins)


    OK, I’ve been tearing my hair out with this one – my PHP-fu clearly is not strong enough. I’m trying to get the value of a custom field in each page into a variable so that I can echo that variable into a loop to add a class to each
    li in my navigation. Does that make sense? Maybe my code will give you an idea of what I’m trying to acheive…

    <?php global $wpdb, $acc_col;
    
    			$pages = $wpdb->get_results( "SELECT id, post_title, post_name, guid FROM wp_posts WHERE post_type = 'page' AND post_status = 'publish' AND post_parent = '0' ORDER BY menu_order, post_date DESC" );
    			$x = 1;
    
    			foreach ($pages as $page) {
    				$count = ($x == count($pages)) ? 'last' : '';
    				$acc_col = get_post_meta(get_the_ID(), 'acc_col', true); ?>
    				<li class="<?php echo $count ?> <?php echo $page->post_name; ?>
    				<?php global $post; $acc_col = get_post_meta($post->ID, 'acc_col', true); echo $acc_col; ?>">
    					<a href="<?php echo get_permalink($page->id) ?>"<?php if ($active == $page->post_name): ?> class="active"<?php endif ?>><?php echo strtolower($page->post_title)?></a>
    				</li>				
    
    <?php
    			$x++;
    			}
    ?>

    I have to confess I ‘liberated’ a lot of the code from elsewhere and tried to craft (butcher?) it to meet my needs. Anyone got any idea why this isn’t working?

    Any help greatly appreciated 🙂

    David.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    (@alchymyth)

    what exactly is not working?
    i.e. what is the output of your code, and what is missing?

    you seem to be mixing $post and $page – is that on purpose?

    you are using $acc_col twice:
    here:
    $acc_col = get_post_meta(get_the_ID(), 'acc_col', true); ?>
    and here:
    <?php global $post; $acc_col = get_post_meta($post->ID, 'acc_col', true);

    Thread Starter dlrobins

    (@dlrobins)

    Sorry – probably not making myself clear.

    My intended output for each item in the navigation menu is (roughly):

    <li class="pagename colour"><a href="blah...">Page Name</a></li>

    Where ‘pagename’ is lifted from wpdb and ‘colour’ is lifted from the value of a custom field in the page in question.

    I’m getting the pagename no problem but getting nothing through for ‘colour’ – view source for the page just shows that the value hasn’t been returned. eg:

    <li class=" meet-the-team "><a href="http://castle.ideasbyeden.co.uk/index.php/meet-the-team/">team</a></li>

    You can safely assume that obvious coding stupidness is not deliberate 😉

    Why not just select the meta in the query, it’ll reduce the total queries by at least one per page returned from the query (get_post_meta will be a single query for each call, this avoids that).

    // Don't use $pages var, it could be in use by WordPress, prefix your vars with something distinctive to avoid naming collisions
    
    $my_pages = $wpdb->get_results( "
    	SELECT id, post_title, post_name, meta_value
    	FROM $wpdb->posts
    		JOIN $wpdb->postmeta
    		ON $wpdb->posts.ID = $wpdb->postmeta.post_id
    	WHERE post_type = 'page'
    	AND post_status = 'publish'
    	AND post_parent = 0
    	AND meta_key = 'acc_col'
    	AND meta_value != ''
    	GROUP BY post_id
    	ORDER BY menu_order, post_date DESC
    ");

    You also won’t need to check if it’s present for each result, because the query only fetches pages that have that meta_key .. (where it’s not an empty value).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘get_post_meta as class into navigation menu’ is closed to new replies.