The Devel module query log doesn't appear in a custom theme - drupal

The Devel module query log appears correctly (at the bottom of the page) when I use one of the default themes, but not when I switch to my custom theme. The query log seems to appear within the inner pages in the custom theme, though not the front page or administrator pages (if those are set to use the custom theme).
I have been through page.tpl.php and html.tpl.php and I can't see any missing variables.
How does the query log get printed on the page?

STEP 1:
Check in your template.php you can find the next hooks. If it doesnt exists, create it.
function YOURTHEME_preprocess_node(&$variables) {
$variables['messages'] = theme('status_messages');
}
function YOUTRHEME_status_messages($variables) {
$m = drupal_get_messages();
foreach ($m as $type => $messages) {
if (count($messages) > 0) {
$output .= "<div class=\"messages\">";
$output .= " <ul>";
foreach ($messages as $key => $message) {
if (count($messages) > 0) {
$output .= '<li class="message '.$type.'"><span class="text">'. $message . "</span></li>";
}
}
$output .= " </ul>";
$output .= "</div>";
}
}
return $output;
}
STEP 2
In your template check if var $message is printed. If it doesnt exists, create it.
print $message;

Related

How to add active class for current item and ancestor in new WooCommerce Product Categories List Block widget?

I am using a Product Categories List Block to list all product categories in sidebar in shop and category pages (like filters). I am listing categories in my site by using a wordpress Widget. Categories are shown but there is no active class for current category or ancestor category. How to make it that if user is on current category, the li item will get current-cat class and ancestor current-ancestor class?
HTML STRUCTURE OF WIDGET
I needed to solve this myself, so here is my current solution.
Since there is currently no filter available for the default WooCommerce blocks, I solved it using PHP's DOMDocument parser while rendering the block.
You can add a filter to render_block to catch the html for the specific block, in this case the block name is woocommerce/product-categories. By adding checks for non-admin & non-json requests we make sure the changes only happen on the front-end output.
If you add the code below to your theme's functions.php, a class current-category-item is added to the list item that matches the current url.
function custom_render_block_wc_product_categories(string $block_content, array $block): string
{
if(
$block['blockName'] !== 'woocommerce/product-categories'
|| is_admin()
|| wp_is_json_request()
) {
return $block_content;
}
$html = '';
global $wp;
$current_slug = trim($wp->request,'/');
$dom = new DOMDocument();
$dom->loadHTML($block_content);
$elements = $dom->getElementsByTagName('a');
if( $elements['length'] ){
foreach ($elements as $node){
$href = parse_url($node->getAttribute('href'));
$path = trim($href['path'], '/');
if( $path === $current_slug ){
$class = $node->parentNode->getAttribute('class');
$class .= ' current-category-item';
$node->parentNode->setAttribute('class', $class);
break;
}
}
}
$html .= "<div class='block-outer-wrapper'>";
$html .= "<header><h4>" . __('Categories','woocommerce') . "</h4></header>";
$html .= $dom->saveHTML();
$html .= "</div>";
return $html;
}
add_filter('render_block', 'custom_render_block_wc_product_categories', 10, 2);
I also added a header to the block, but these lines are optional as long as you return $dom->saveHTML() after your changes.

Drupal - Show child terms of current term, only if child items exist - code modification needed

Having fought with the problem of showing child terms for a given taxonomy term in a block, I finally stumbled on a piece of code that does exactly what I want here
As per the instructions I've added the following to my template.php
function themename_child_terms($vid = 1) {
if(arg(0) == 'taxonomy' && arg(1) == 'term') {
$children = taxonomy_get_children(arg(2), $vid);
if(!$children) {
$custom_parent = taxonomy_get_parents(arg(2));
$parent_tree = array();
foreach ($custom_parent as $custom_child => $key) {
$parent_tree = taxonomy_get_tree($vid, $key->tid);
}
$children = $parent_tree;
}
$output = '<ul>';
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
}
I've then created a block and added :
<?php // $vid is the vocabulary id.
print themename_child_terms($vid = 1);
?>
This shows the child terms of the current term perfectly. However, it shows ALL terms that exist under the parent term, even if there is no piece of content using that term.
e.g.
viewing the page with all items in Term 1, I get
child 1
child 2
child 3
correctly listed in the block. But, if there are no pieces of content tagged with 'child 3' for example, it's still showing up that term in the block. This isn't very useful as it links to an empty term page.
How would I modify the code to only show children that actually have items associated with them. So if there are no children tagged 'Child 3', then that term would not show up. Is it an easy modification?
Thank you kindly for any solutions.
Nick
Working with Drupal 6
Thanks to user jerdiggity for posting the following reply over on drupal.stackexchange here. Works perfectly.
Hmm... I'd try something like this:
Change this part of your code:
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
to something like this:
// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
// If not empty, run the foreach loop:
foreach ($children as $term) {
// Then check to see if any nodes exist for that term id:
$number_of_nodes = taxonomy_term_count_nodes($term->tid);
// If there ARE nodes...
if ($number_of_nodes > 0) {
// ... then add them to the output:
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
}
}
Hope that helps... :)

How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

how to show taxonomy subcatgory in breadcrumb of drupal

I am using Taxonomy_Manager and Menu_breadcrumb modules
my categories looks like:
+BUSINESS
++Agriculture
++Banking & Finance
++Construction & Real Estate
+News
++ Behind the news
++ Peace and War
now the question is: if i browse any sub-category, it will not appear in the breadcrumb
(the breadcrumb will be "Home>>") while if i browse one of the main categories, it will appear normally in the breadcrumb ("Home>>News")
i have tried taxonomy_breadcrumb but this didnt fix the issue :(
how can i set the subcategories to appear in the breadcrumb??
Thanks for your help
Use any of these modules:
http://drupal.org/project/hansel
http://drupal.org/project/custom_breadcrumbs
I solved all my breadcrumb-taxonomy related problems overriding the druapal breadcrumb function. You have to go to your theme folder and add the follow function in your template.php file.
function YOUR_THEME_NAME_breadcrumb( $variables )
{
// init
$breadcrumb = $variables['breadcrumb'];
// taxonomy hierarchy
$hierarchy = array();
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)))
{
$tid = (int)arg(2);
$parents = array_reverse(taxonomy_get_parents_all($tid));
foreach( $parents as $k=>$v)
{
if( $v->tid==$tid ) continue;
$breadcrumb[] = l($v->name, 'taxonomy/term/'. $v->tid);;
}
}
// rendering
if (!empty($breadcrumb))
{
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
$output .= '<div class="breadcrumb">' . implode("<span class='separator'>ยป</span>", $breadcrumb) . '</div>';
return $output;
}
}

Drupal Site Map Module

I am looking for a module that can create sitemap in Drupal, but couldn't find any. I tried Site Map module, but it can only generate a sitemap page; it can't create a sitemap block at the end of every page. I also tried site menu module, but it can't create a sitemap block as shown above, as well.
Maybe it's just that I don't know how to configure, but I read every readme file, and tried for a few days, still can't get it to work.
Anyone has any idea?
I had the same problem, after trying a module (site-map) but missing customization options I wrote a custom module. Took less time then messing with the site-map module, for just getting a site map the following code is enough (adapt your-menu):
function sitemap_render_menu ($menu) {
$output = "<ul>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$output .= "<li>" . $link["title"] . "</li>";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
}
$output .= "</ul>";
return $output;
}
function sitemap_content () {
$output = "<h1>Sitemap</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data("your-menu"));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
There is a basic comparison of sitemap modules at http://groups.drupal.org/node/15980
I have used sitemenu and it worked for my needs, but the real answer depends on how you structure your site with taxonomy, content types, etc.
Something like Auto Menu might work for you here as well. You could simply add the menu it generates to a footer block on your front page.
With the Site Map module installed, this php code will print the site map.
<?php echo theme('site_map'); ?>
You could create an empty views block and specify the above for the empty text, selecting the PHP Code input format.
There may be a better way to create a custom block to display php code, but I don't know it.
My idea here is to use Views module with customized block type.
I think you can benefit from Menu block module. as you can create menu blocks for all the menus you need in the footer. Then you can add them all in footer or in a one block using minipanels block (from panels module).
You can use Footer_sitemap module which provides us with the configurable block.
https://drupal.org/project/footer_sitemap
This is a little mod of best answer it uses current theme to show hierarchy
function sitemap_render_menu ($menu) {
$output = "<ul class='menu'>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$cc=($item["below"]) ? "class='collapsed'" : '';
$output .= "<li $cc>" . $link["title"] . "";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
$output .= "</li>";
}
$output .= "</ul>";
return $output;
}
function sitemap_content ($title,$menu) {
$output = "<h1>$title</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data($menu));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
print sitemap_content("Navigational menu","Navigation");
A simple solution which doesn't depend on content being included as a menu item can be achieved by :
Creating a new view
Output as a block
Using Fields:
Content Title (configured to "Link this field to the original piece of content"
Content Type (configured to "Exclude from display")
Format as
Unformatted list with settings - Grouping field Nr.1 select Content:Type;
Filter Criterea:
Content: Published (Yes)
Content Type - Configure to select the content types you wish to include;
Sort criteria - Configure as per your preference

Resources