I am interested in how to highlight the category in which I am present - with the "current" class in the template. The problem is compounded by the fact that the list of categories is not retrieved from the database WordPress (the same place, as I understand, they are stored?), that is - directly from the HTML... How to make the following code:
code:
<ul>
<li class="blabla">
<a>1</a>
</li>
<li class="blabla">
<a>2</a>
</li>
<li class="blabla">
<a>3</a>
</li>
</ul>
code to be dynamic, for example when I go into the category of "2" to "li" will be added to the class "current":
code:
<ul>
<li class="blabla">
<a>1</a>
</li>
<li class="blabla current">
<a>2</a>
</li>
<li class="blabla">
<a>3</a>
</li>
</ul>
I tried to read the codex but I have two problems - I'm not good at php and most importantly hard to read in English
I sincerely apologize for the broken English with which I asked, "that" the question
I'm not sure if I understand your question, but based on what you said, you can use css.
Example
/* This will highlight your menu item gray if it has current in its class attribute*/
.current {
background:#555;
}
If you just want a menu of the categories, you can use the Wordpress category widget. It creates a menu with a list of all the categories. If you click on the category, it will have a class of current-cat. This only happens when you are in the category filter list view, so if you click on a post with that category, it will not say 'current-cat'.
If you want something more specialized, then it's still possible. Tell me if the category widget will not work for you.
Here is the way to do it in php if you don't want to use a widget. It does basically the same thing, but you have more freedom where you want to put it.
This is the reference page: http://codex.wordpress.org/Template_Tags/wp_list_categories
Basically all you need is that function. Just place this snippet of code where you want your categories to appear:
<?php echo wp_list_categories() ?>
That should create a basic html list of all the categories. It will also set the current-cat class if you are in the category archive view.
If you want to override the current category behavior above, you can also force the menu to select a current category based on the id of the category you select. So in this example, it will add the current-cat class to the category with id three.
<?php echo wp_list_categories(array('current_category' => 3)) ?>
/* this is working #747474:gray*/
.current {
background:#747474;
}
Related
I have an archive events page that links to a single page that lists all previous events created in the past and can only be visited via the archive events page.
My question is, how can I highlight the archive events page url link when viewing this single page?
For example the below snippet targets a hard-coded nav link to highlight the events link when viewing the other page, however my menu is not hard-coded:
<nav class="main-navigation">
<ul>
<li>...</li>
<li>...</li>
<li>
<?php if(get_post_type() == 'event' OR is_page('past-events')) echo 'class="current-menu-item"';?>>
events
</li>
<li>...</li>
</ul>
</nav>
The tutorial I am following does not give the alternative coded way of targeting this similarly in the functions.php page, for a dynamic menu that is created via the wordpress dashboard menu.
I have tried searching for examples, but I havent found anything that has helped so far and in need of some outside help.
How can I do this?
I can see something wrong. you haven't put the php code in right place. corrected code is this. try this and let me know if it works
<nav class="main-navigation">
<ul>
<li>...</li>
<li>...</li>
<li <?php if(get_post_type() == 'event') echo 'class="current-menu-item"';?>>
</li>
<li>...</li>
</ul>
</nav>
Ok, I think I have found a solution -- being a novice -- has worked!
Hopefully someone can point out any changes/improvements I could make, but here it is:
To highlight a menu link for a different page than the one that you are on/viewing, for example in my scenario: I have an archive events page and within that page I have a link that takes you to a single page that lists all past previous events, but that has no link in the main navigation to it.
function nav_class($classes) {
// Remove "current-menu-item" class
$classes = array_diff($classes, array('current-menu-item'));
// If this is the "past-events" page, highlight the event menu item
if (in_array('menu-item-36', $classes) && is_page('past-events')) {
$classes[] = "current-menu-item";
}
return $classes;
}
add_filter('nav_menu_css_class', 'nav_class', 10, 2);
I hope this helps someone else out.
I'm trying to create a header to be used in my WP posts so that it displays a list item and and anchor according to the post's author. For example, if, and only if, the author has ID of 6, the header will have one more list item with an anchor to, let's say, page A. If it's not that author I don't want it to display the list item.
So in my single-post.php template I load this header I want with:
<?php get_header('bananas'); ?>
and in header-bananas.php I have:
<nav>
<ul>
<li>Home</li>
<li>Clinic's Blog</li>
<?php if (!is_author(6)){ ?>
<li id="apples">Link to page A</li>
<?php } ?>
</ul>
</nav>
However I suspect Wordpress is not looping in the header so it can't detect if the conditional is true before loading the post in single-post.php, because #apples is displaying even if that post isn't written by author of id 6.
Am I right? If so, what's the best approach to load a conditional with a Wordpress function in the header? Thank you very much!
I was using the wrong function.
The correct way of doing this would be something like this:
<?php
$myauthor = get_the_author_ID($post->ID);
if ($myauthor == 6){
echo '<li id="apples">Link to Page A</li>';
};
?>
Good day.
I am trying to modify the navigation menu layout of a drupal theme. The problem is i'm not a coder, but can follow tutorials and understand bits and peaices.
What I have is a set of menus and sub menus. At the moment the sub menus expand below their parent menus. What I want is for the sub menus to expand but live below all the parent menus so it would look like the structure below.
How it currently looks
Navigation_Menu_One
Sub_Menu_One
Sub_Menu_Two
Sub_Menu_Three
Navigation_Menu_Two
Navigation_Menu_Three
What I want it to end up looking like:
Navigation_Menu_One
Navigation_Menu_Two
Navigation_Menu_Three
Sub_Menu_One
Sub_Menu_Two
Sub_Menu_Three
What I have figured out is that the menu system is built from the following class's
<ul class="menu">
<li class="first collapsed"></li>
<li class="first collapsed"></li>
<li class="collapsed"></li>
<li class="expanded active-trail"> (The expanded Menu)
<ul class="menu"></ul> (the sub menu I want to relocate)
</li>
<li class="collapsed"></li>
<li class="collapsed"></li>
<li class="collapsed"></li>
<li class="last collapsed"></li>
</ul>
So what I want to do is be able to add something to the page that would capture the
<ul class="menu"></ul>
that appears below the
<li class="expanded active-trail">
and relocate it to below the last class or another div.
I understand I will need to do this using PHP from one of the template files with in the theme. Would any one have any idea how I would go about doing this. The theme I am using is the Corolla theme if that helps at all, but I guess it would be the same for any theme.
Hi and welcome to StackOverflow.
It seems to me from your description that you should be able to achieve this without writing any code. Simply navigate to site building > menus > navigation and use the drag handles to move the sub menus so that they are underneath the desired menu item.
I cannot figure out how to use the wp_list_categories function to do what I need it to:
<?php if(is_category() or is_page('realisations') or is_single()) { ?>
<ul id="subpage">
<?php wp_list_categories('child_of=3&title_li=<h4>Les secteurs</h4>'); ?>
</ul>
<?php }; ?>
I have it set up so that all of the project categories are child categories of the a main category (child_of=3) This is to avoid conflicts with the news section of the site.
The problem is that I need the category the post is in to highlight when viewing the single.php page template, but I don't know how to accomplish that. When on a category page the category view highlights correctly because I styled the class that WordPress adds into the generated list .current-cat.
I answered my own question! This wonderful plugin has the function that I didn't need to write myself: http://www.screenshine.net/blog/1474_wordpress-plugin-show-active-category
It sets a filter to add into the wp_list_categories() function, the only thing I dislike about it is that it sets a css class on the anchor instead of the list item, which is inconsistent with WordPress functionality. It works in a pinch though, which is what I am in. You can stick it in your functions.php to prepackage the plugin and do minor edits.
I'm developing a Wordpress theme. One of the sections on the front page is titled News. In that section the user should be able to insert information himself (I'll make a separate file called news.php which will php included into the markup), but I also want the content to be automatically updated once the user makes a post.
For example, if the user writes a new post, I want the news section to be automatically updated to something like this:
A new post was written, find it here (where 'here' is a hyperlink pointing to the url of the post).
edit Let me try to give a little more details so it's clearer.
The news section will be an unordered list.
Thus:
<ul>
<li>
News item 1
</li>
<li>
News item 2
</li>
</ul>
I want the user to be able to add content to the news section, aka make new News Items, but using the visual editor of Wordpress such that the user doesn't have to understand the code and doesn't have to copy/paste the LIs.
Furthermore, whenever a new post is published, I want it to show up as:
"new post published, find it here" where 'here' is a hyperlink with linking to the post.
Is this possible to accomplish?
Thanks, Amit
make a Categorie, or a Custom Post Type called, "News" so when the user creates a new post, but wants it to be in the "News" section all he has to do is click on the Category "News." You could also create a custom post type, just for "News" and have custom taxonomies etc as well.
Then when you want to print the News post in your theme, write use the query post function and limit the loop to "category_name=news"
If i correctly understood, you need write something like this:
<?php
query_posts(array('posts_per_page' => 1));
the_post();
?>
A new post was written, find it here
Latest news list
<?php
query_posts(array('posts_per_page' => 6));
?>
<ul>
<?php $count=0; if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>