Drupal 6: Printing Unadulterated Primary Links and all children - drupal

How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!
I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.
I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:
<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php
$href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
print "<a href='" . $href . "'>" . $link['title'] . "</a>";
?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.
I sincerely thank you in advance to lessening my gargantuan headache!

It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.
This is one I used to alter the classes of my primary links:
function primary_links_add_icons() {
$links = menu_primary_links();
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
if ($links) {
foreach ($links as $link) {
$link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
$output .= '<li class="sublevel">' . $link .'</li>';
};
$output .= '</ul>';
}
return $output;
}
And then in page.tpl.php I just called it like this:
<?php if ($primary_links) :?>
<?php print '<div id="menu">'; ?>
<?php print primary_links_add_icons(); ?>
<?php print '</div>'; ?>
<?php endif;?>

I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc
You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed.
This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.
You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently.
MarkLNH

Related

how to display wp tags in dropdwon with checkbox in admin

I have Install WordPress and i need all tags should be display in drop down like category
Also I want search filter over there
when i want to put any comma separate tag in search box all tag should be checked in drop down automatically
You can use this function to get all the tags in the WP.
$tags = get_tags(array('get'=>'all'));
example,
$tags = get_tags(array('get'=>'all'));
$output .= '<ul class="tag-cloud-list">';
if($tags) {
foreach ($tags as $tag):
$output .= '<li>'. $tag->name .'</li>';
endforeach;
} else {
_e('No tags created.', 'text-domain');
}
$output .= '</ul>';
return $output;
The same way you can use the checkbox in the place of UL and LI. Also, you can use the add_meta_box() to add the tags to the all post type.
For more details, Please visit.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://developer.wordpress.org/reference/functions/get_tags/

wordpress add field to post_class

I need to add a custom field to my article post but not sure how to add an additional class to it.
Currently the classes get pulled through like this <?php post_class($classes); ?>.
However I need to add a custom field to this as well. To demonstrate ive added a class= but this doesnt work as class= is being added twice.
<?php post_class($classes); ?> class="<?php the_field( "size" ); ?>
So i need post_class and the_field to work together.
Thanks for your answers but I've found a simple way to do this
<?php post_class(get_field('field_name')); ?>
You can do this with two different way,
First:- Add following code in theme's functions.php file:
This will add your class where post_class is called.
function category_id_class($classes)
{
global $post;
if($post->post_type == 'post')
{
$classes[] = get_field( "size" );;
}
return $classes;
}
add_filter('post_class', 'category_id_class');
Second:- Add this following code directly into your page:-
$post_classes = get_post_class();
$post_classes= implode(' ', $post_classes);
echo 'class="'.$post_classes. the_field( "size" )'"';
Hope this will help you little bit.
So why you could not do like this:
<?php post_class(the_field( "size" )); ?>
Because it is working like this:
<?php post_class('my_custom_class'); ?>

Wordpress - Add comma to all but last item, remove underscore

I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>

Can't get my WordPress site to update its own feed?

I'm using the following code to display some entries of my WordPress blog as a feed in the sidebar. The problem is, it's not updating no matter what I do. It still only shows the first "Hello World" post, even though I've added others, and it doesn't even show the updated name of that post after I've changed it. Thought this might be a caching issue, but if I actually click into the feed XML, the data is updated- which makes no sense to me??
<?php
// Blog Feed:
$rss_url = get_option('home')."/feed/";
?>
<ul class="side-feed">
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(3); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
echo count($items);
}
if ($limit == 0) echo '<div>(None)</div>';
else foreach ($items as $item) : ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
Wordpress caches feeds for 12 hours by default, to change this you need to hook into the wp_feed_cache_transient_lifetime filter and return the number of seconds you want to cache for.
add_filter('wp_feed_cache_transient_lifetime', create_function('', 'return 60*60;'));

Drupal 7 - Menu to UL

all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.
I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably in something simple like an ul.
Is there any function that takes a menu and produces an ul containing all the links?
Maybe there is some way you can reduce the menu's block to just an ul.
I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.
Thank you!
I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.
But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.
//----------- primary menu (horizontal with drop-downs) -------------------------
$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;
$html = '<ul>';
foreach($menu as $item_menu) { //for each main element
$isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);
if ($isSecondLevel) {
$html.= '<li>';
} else {
$html.= '<li class="sg">';
}
$html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';
$html .= $item_menu['link']['link_title'];
$html .= '</a>';
//is there any sub elements to display
if ($isSecondLevel) {
$html.= '<ul>';
foreach($item_menu['below'] as $item_submenu) { //for each sub element
$isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';
$html.= '<li>';
$html.= '<a href="'.url($item_submenu['link']['link_path']).'">';
$html.= $item_submenu['link']['link_title'];
$html.= '</a>';
$html.= '</li>';
}
$html.= '</ul>';
}
$html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;
This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>
Simon.
P.S. Others, please feel free to edit this code for clarity/simplicity.
I advice you to use
menu_tree_output
like this:
print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));
You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.
If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus

Resources