• I need some help writing a plugin to change my main navigation menu
    (I’ve done LOTS of reading of the WP docs – but no practical experience yet!)

    The code I have so far is my plugin:

    function colour_links()
    {
    	$stringToLookFor = "My Dummy";
    	$stringToReplace = "<strong>My Dummy</strong>";
    
    	$wp_list_pages = str_replace($stringToReplace, $stringToLookFor, $wp_list_pages);
    	return $wp_list_pages;
    }
    
    add_filter('wp_list_pages', colour_links);

    OK: I’m sure there’s plenty of incorrect code above
    I’d appreciate if you could point out where I was going wrong

    In the theme I’m using, they use wp_list_pages() to display the top menu
    So, I need to write code that overwrites the default actions for this function?

    All I want to actually do is change the colour of specific menu items (and maybe make them bold) – the names of these will intentionally be hard coded
    In the above code, I have simply tried to make bold – just to test

    I’m a little stuck where to start!

    I’ve looked up and there’s a WP hook for ‘wp_list_pages’
    (https://adambrown.info/p/wp_hooks/hook/wp_list_pages?version=2.9&file=wp-includes/post-template.php)

    I’m pretty sure I’m using $wp_list_pages wrongly above

    Any help would be great!

    Thanks

    Omar

Viewing 5 replies - 1 through 5 (of 5 total)
  • I think you are very close. I see two small changes. First, the function needs to have an input parameter. Second the function name in the add_filter should be in quotes.

    function colour_links( $wp_list_pages )
    {
    	$stringToLookFor = "My Dummy";
    	$stringToReplace = "<strong>My Dummy</strong>";
    
    	$wp_list_pages = str_replace($stringToReplace, $stringToLookFor, $wp_list_pages);
    	return $wp_list_pages;
    }
    
    add_filter('wp_list_pages', 'colour_links');
    Thread Starter OM2

    (@om2)

    ok, thanks
    i think thats a little better
    but the problem is: $wp_list_pages
    i assumed this was a variable that contained the list of pages in html format?

    what i want to do is simply change the colour (and maybe do some other simple formatting) to the links

    where am i going wrong?
    what should i be changing?

    does anyone know of any other plugin that use the filter wp_list_pages hook?
    i’ve tried looking at other plugins, but cant seem to find any – they all do other complex things – what i need is simple and straight forward

    thanks

    Can you use CSS to format the links? The following will change all links to red:

    .page_item a {color: red}

    Thread Starter OM2

    (@om2)

    yes, i can – but i only want specific menu items to be what i want – all others should stay the same

    i thoguht this would be easy! i’m going round in circles ??

    Don’t give up. I think this will help. What you get as a parameter in your function looks like this:

    <li class="pagenav";>Pages
    <ul><li class="page_item page-item-303"><a href="//localhost/test/?page_id=303" title="A template test">A template test</a>
    <li class="page_item page-item-300"><a href="//localhost/test/?page_id=300" title="A test of listorderbycustom">A test of listorderbycustom</a>

    If you look closely, you will see that the post title ‘A template test’ occurs twice – once in ‘title=”A template test”‘, and once in ‘>A template test<‘. You just need to adjust your search/replace strings by adding the > and < signs.

    $stringToLookFor = ">My Dummy<";
    	$stringToReplace = "><strong>My Dummy</strong><";

    I think that will work.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need help writing a plugin to change my navigation menu’ is closed to new replies.