So i've been searching and testing for a couple days and can't seem to figure this out.
I'm using the K2 content module and I need to add "?Itemid=111" to the end of the URL's it generates for the "Read More" link on pages.
This is not done by default and i've found where I can add it, but i'm having problems getting the Itemid (menu id) from the article id.
All the examples i've found use
$app = JFactory::getApplication();
$menu = $app->getMenu();
$activeId = $menu->getActive()->id;
And that works, whenever you're on that page, and it does add the correct ID. But for the articles I display on the homepage it does not work correctly as it adds the Itemid (menu id) of the homepage, and not the menu id of that specific article.
Before when using Joomla 1.5 I would use the built-in function
getItemid($articleid)
That doesn't seem to work for me anymore.
Does anybody have any suggestions or can anybody point me in the right direction on how I could solve this and "Get menu id (Itemid) from the article id"?
Thanks!!!
You can get menu id of an article using the below code-
$link = 'index.php?option=com_content&view=article&id='.(int)$articleId ;
$menu = JSite::getMenu();
$menuItem = $menu->getItems( 'link', $link, true );
$Itemid = $menuItem->id;
Hope this will help.
Related
I have a problem with Joomla!. Is there a way to set a module visible in certain category but not in the articles? (so it will display on the category page, but not in the articles of that category or in subcategories)
I had the oposite problem on previous works and fixed it with some cat id's and php in the index.php, but I don't know how to achieve this effect, is someone there that had the same problem and fixed it?
Regards
create a override of the category view in template/your_template/html/ folder and use the following code inside the override file :
<?php
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('modules');
$position = "YOURPOSITIONNAME";
$options = array('style' => 'raw');
echo $renderer->render($position, $options, null);
?>
Is this useful ?
I don't develop on Joomla, but i found this link.
http://www.inmotionhosting.com/support/edu/joomla-25/extensions/modules-on-homepage
Why not use Advanced Module Manager which has several advanced options that will suit your requirement https://www.regularlabs.com/extensions/advancedmodulemanager#free-vs-pro .
Lots of people, both here on SOW and elsewhere, are having my same exact problem.
I've installed the BBpress plugin on my site here. This plugin enables a FORUM on the site.
I've wrestled for days (to no avail) on this documented issue where the plugin causes the wrong menu item to be highlighted when any Forum sub-menu item is chosen. Instead, the Blog menu item gets highlighted by default.
Check it out: Click on Forum and this resulting page highlights the "Forum" menu item okay. But click on "Test forum" (or drill down into any other Forum item) and the resulting page always has "Blog" highlighted.
Here's what I know so far:
It can't be fixed with CSS alone.
I am using Permalinks (with "Post name" selected)
PHP assigns a .current-menu-item and/or a .current_page_item class (along with their corresponding -ancestor and -parent classes) to menu items based on the page you've navigated to. For some reason, it is failing to see any Forum submenu as a current page. As a result, it is defaulting to "Blog" as the current page item.
So I know I need to do the following:
have Wordpress check the URL upon each page load
if "/forums/" is part of the URL, remove all .current-menu* and .current_page* classes (the wildcard is necessary to purge the ancestor/parent classes).
then assign .current-menu-item and .current_page_item classes to the "Forum" menu item.
The problem is, while I think i'm getting close, I don't know how - exactly - to do this.
I've tried a bunch of things.
I found this Jquery solution but don't know how to implement it, nor am I confident in this approach:
// First add a “forum-class” to your forums menu item in your custom menu
// Then add this to your js file with YOURURL = the url of your site and forums = your forums slug :
$(function() {
var forumURL = window.location.href.indexOf("YOURURL/forums/");
if(forumURL > -1){
$('li.forum-class').addClass('current-menu-item');
}
});
Then, I found this approach which seemed closer to what I wanted, but it introduces a new class and doesn't seem to clear the incorrect classes from the Blog menu. Plus, I don't know where exactly to put this code, or where to create the js file.
if (strtolower($thats_all->post_title) == "forum" || strtolower($thats_all->post_title) == "forums") {
$addclass = ' class="current_page"';
} else {
$addclass = '';
}
Finally, I found this which is what I think I need, but I'm having trouble implementing it (I've tried sticking it in my menu-primary.php and header.php but no go).
// this forces the class current-menu-item to a menu item which I named 'forums'
add_filter('nav_menu_css_class', 'remove_link_parent_menu_classes', 420 ,3);
function remove_link_parent_menu_classes($classes, $item, $args){
if(strpos($item->url, '/forums/') !== true)
return array_diff($classes, array('current_page_item', 'current-menu-item'));
return $classes;
}
I think I'm close. Really close. Just need a push in the right direction. I need to know:
will the latter solution above work?
which file, and where exactly in the file, do I need to place the code?
Thanks!
I finally cracked it! Using the above clues, I figured out this code that worked for me.
add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
function namespace_menu_classes( $classes , $item ){
if ( get_post_type() == 'forum' ) {
$classes = str_replace( 'current_page_parent', '', $classes );
$classes = str_replace( 'menu-item-16', 'current_page_parent', $classes );
}
return $classes;
}
Copy the above at the end of the function.php file.
Then change 'forum' to the slug name you've chosen and replace 'menu-item-16' with the correctly numbered menu item that you want highlighted (you will need to use your web browser's developer tools to inspect your site to be able to find this).
This took me a week to figure out (and learn the php required)! Good luck and hope this helps everyone else out there.
I have a page named "Shop". On that page I display a custom menu, which contains links to categories like "Books", "Shoes" etc. Whenever I click one of those links/categories it takes me to a relevant category page, for example /category/books/.
I do not want it to redirect me to a category page, I want it to display posts on the same page ("Shop") that the menu item was clicked on. The only problem I have encountered trying to accomplish this, is the fact that I do not know how to change the custom menu behavior. I don't want it to redirect me, but instead send a GET value to the same page ("Shop"). Then the shop page would take that GET value and display relevant posts.
I know how to display posts from a category etc. I just don't know how to change the behavior of the custom menu.
Could anybody help me? I would be grateful.
Original Answer:
Use wp_get_nav_menu_items:
$menu_slug = 'YOUR_MENU_SLUG';
$menu_id = get_nav_menu_locations()[$menu_slug];
$menu_items = wp_get_nav_menu_items($menu_id);
foreach($menu_items as $item){
if($item->object == 'category'){
print('<p>Title: ' . $item->title . '<br>ID: ' . $item->object_id . '</p>');
}
}
wp_get_nav_menu_items returns an array of menu items. You get several information on the item, like type (post, category, ...), id, title and so on.
See http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items for a full list of properties.
Hope this helps :)
Update:
Assuming your permalink setting is set to default, the following code will print a modified menu that uses GET.
$menu = wp_nav_menu(array(
'theme_location'=>'YOUR_MENU_LOCATION',
'echo'=>0,
));
$new_url = $_SERVER['SCRIPT_NAME'] . '?shop_page=$1';
$menu = preg_replace('/href="[^"]*cat=(.+)"/', 'href="'.$new_url.'"', $menu);
print($menu);
The regex may not be the best as it ignores other GET values and I'm not experienced on them. If your permalink setting is different and every time you change it, you will have to edit the regex.
i created a HTML module and loaded in virtuemart category layout but i need to show all category and subcategory name list so that when user added new category in future , it will updated automatically . no need to hardcode every time. please help on this topic. you can load these categories name by using while loops in PHP. please reply fast.
Try this,
If you are doing with module then in your module file(mod_yourmodule.php) add the following codes.
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
$categoryModel = VmModel::getModel('Category');
$cats = $categoryModel->getCategoryTree();//Params $parentId=0, $level = 0, $onlyPublished = true,$keyword = ''
echo "<pre/>";
print_r($cats);
Hope its helps..
As VirtueMart don't provide this hierarchical structure at front-end you have to build your own method. Though at back-end you can use #Jobin's answer. I have implement and tested it for the front-end, it works with ease. Check the answer here
https://webkul.com/blog/get-parent-child-categories-hierarchical-tree-joomla-virtuemart/
I am currently developing a Joomla 1.7 website, And I am using J2Store shopping Cart so I have to make make an article for every Product.
Then to display I create a Joomla Featured Articles Menu Item for the appropriate Category, my problem is this, The default Article Title isn't really appropriate for the page layout and because there will be many products / articles so as a time saver I would like to include some PHP code to retrieve the Article Title I have tried the following used in conjunction with the Sourcerer plugin.
<?php echo JFactory::getDocument()->getTitle(); ?>
But unfortunately it displays the Menu Title not the individual Article Titles, I also found the following code but I cant get it to work with Joomla 1.7
<?php
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
} ?>
Try this:
$product =& JTable::getInstance("content");
$product->load($product_id);
echo $product->get("title");
And replace $product_id with whatever ID needed.
Look at this file:
components/com_content/views/featured/tmpl/default.php
That's the original template for the Featured Articles view, you can override it as explained in this link:
http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core
to include a piece of code like this (in the file you created on your template folder):
<?php
foreach($this->items as $fItem):
echo $fItem->title.'<br/>';
endforeach;
?>
Voilà, you will see all the titles from the displayed featured articles.