I'm trying to diligently follow what the codex says about modifying the appearance of the comments form (located here) but I can't get it to work. The default comments form appears despite my array of arguments that should change it.
In the template I have: comments_template('mycomments.php');
Then...
<?php
//inside my mycomments.php file
$comment_args = array (
'fields' => array('author', 'email'),
'must_log_in' => true,
'title_reply' => 'Review this product',
'label_submit' => 'Submit',
);
comment_form($comment_args, $post->ID); ?>
Am I doing something wrong?
I figured it out. There was nothing wrong with my arguments. Instead... I needed a / infront of the relative link to my customized comments template:
comments_template('/mycomments.php');
Moral of the story: If you're having trouble getting the template to work (at all), make sure you're actually loading the right template file!
Related
I am trying to remove numbers from pagination links on a paginated Wordpress post to leave only next/previous buttons. I have the code as follows and it still does not work as required. If I set a display:none; css rule to the tags within the .paging p class this affects all links including the next/previous links as the links do not have a separate class to them.
<?php
wp_link_pages(array(
'before' => '<p class="paging" style="margin-bottom: 5em;">' . __(''),
'after' => '</p>',
'next_or_number' => 'next', # activate parameter overloading
'nextpagelink' => __('<span class="pagelink right">NEXT</span>'),
'previouspagelink' => __('<span class="pagelink left">PREVIOUS</span>'),
'pagelink' => '%',
'echo' => 1 )
); ?>
Here is an example of a post illustrating the problem: http://famtrav.staging.wpengine.com/destinations/uk/15-fun-things-july-2016/
Is there another way of me achieving the required result? I hope this makes sense. Many thanks.
I have now solved this. The problem was caused by conflicting code. Specifically the wp_link_pages function was being defined both in the themes functions.php file and in the post-template.php file within the /wp-includes/ folder with instructions in one incidence contradicting the other. I commented out the instructions in the functions.php file and altered the post-template.php file with the correct code and this solved the problem.
Setup an Archive list on the right of my page and the design has a "view" button after each item.
I'm trying to have that view button link to the archive month page.
Looking at doing something with the after and adding a href="" to the view button, However not sure what to reference to get that to happen.
My current code is as follows :
<?php
// Get Archives'
$args = array (
'type' => 'monthly',
'order' => 'DESC',
'after' => '<div class="pull-right"><a class="view" href="#">View</a></div>',
'limit' => '6'
);
$archives = wp_get_archives( $args );
?>
As you can see the 'after' parameter in the array is where I am trying to add the href.
Hope that makes sense.
Thank You!
Few things about wp_get_archives:
It doesn't return anything except if you force the echo parameter to 0 - otherwise calling the function will result of the archive links to be printed.
The after parameter is dependant of the format parameter - it is only used when using the "html" (default - list mode) or "custom" format. What it does is to display the value you pass as parameter after the link. So you don't need to reference the link inside. Use it in combination with the before parameter to achieve what you want to do here.
You don't really need to set the type as monthly, as it is the default value for this parameter. Same for the order parameter, which default is allready DESC.
So a valid call would be:
wp_get_archives(array(
'format' => 'custom',
'before' => '<div class="pull-right">',
'after' => '</div>',
'limit' => 6
));
You probably notice that it doesn't output exactly what you are trying to do, as it miss the class view on your links. You need to add a filter on get_archives_link in order to achieve this (this go in your theme functions.php):
add_filter('get_archives_link', 'get_archive_links_css_class' );
function get_archive_links_css_class($link) {
return str_replace('href', 'class="view" href', $link);
}
This will add the class before the href attribute.
This issue is driving me crazy.
This morning, the Wordpress theme I'm creating included a very simple functions.php file, and my site worked properly. Today, I wrote a new function to iterate through categories and list certain posts, and that's the only change I made to functions.php.
After uploading the revised functions file to the server, I get a 500 error every time I access my site. I though, "OK, my new function broke something. I'll just remove it from functions.php and start over." However, after reverting to the original version of the file (the version that worked this morning), I'm still getting the 500 error.
If I remove functions.php from the server, my pages load (without their sidebars obviously, but they load). As soon as I upload the file again (same version that worked this morning), I get the 500 error.
I checked the error log, and I've found that, when I have functions.php on the server, Wordpress (or php more likely) can no longer find the get_template_directory_uri() function. I've done nothing to alter any of the standard Wordpress functions, and this only happens when I've got functions.php uploaded.
What might be causing functions.php to break the standard functions?
Here's my functions.php file:
<?php
/* *********************************************
* Create left-hand sidebar for single.php
*
* TODO: Finalize $sricks_single_left_sidebar.
* ********************************************/
$sricks_single_left_sidebar = array(
'name' => 'Left Sidebar',
'id' => 'lt-single',
'description' => 'This is the sidebar on the left side of the single.php template page.',
'before_widget' => '<!-- Left-Single Sidebar --><div class="lt-single">',
'after_widget' => '</div> <!-- End Left-Single Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_single_left_sidebar );
/* *********************************************
* Create header_search sidebar to be used on
* all pages
* ********************************************/
$sricks_header_search_sidebar = array(
'name' => 'Header Search',
'id' => 'header-search',
'description' => 'This is the search box in the page header.',
'before_widget' => '<!-- Header-Search Sidebar --><div class="header-search">',
'after_widget' => '</div> <!-- End Header-Search Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_header_search_sidebar );
?>
To find the cause of issues in WordPress when you get 500 level errors, a blank white screen, or any other strange or unexpected behavior, define the WP_DEBUG constant in wp-config.php.
Like this:
define( 'WP_DEBUG', true );
If you're developing or making changes to a site, it's usually a good idea to enable this feature until you're done editing. It can make it very easy to spot and correct mistakes that could otherwise vex for hours.
Thanks everyone, especially Spencer Cameron. It turns out I forgot the open and close parentheses in my new function's declaration. Wordpress reads functions.php before any of the built-in functions, so it just got stuck there.
When I submitted my code, I left out my new function because I had deleted everything from the body of the function; what could go wrong? Lesson learned...
I have a custom Drupal 7 module which is hooking into the media module and I'm having trouble with adding some js into its render.
The module is defining a theme like this:
function media_flash_theme($existing, $type, $theme, $path) {
return array(
'media_flash_video' => array(
'variables' => array('uri' => NULL, 'options' => array()),
'file' => 'media_flash.theme.inc',
'path' => $path . '/includes/themes',
'template' => 'media-flash',
)
);
}
And my formatter (view) is returning my element like so:
$element = array(
'#theme' => 'media_flash_video',
'#uri' => $file->uri,
'#options' => array(),
);
return $element;
Now, I have a preprocess function which adds some js:
function media_flash_preprocess_media_flash_video(&$variables) {
$path = libraries_get_path('swfobject');
drupal_add_js($path . '/swfobject.js');
...
}
And also a drupal add js in my template file:
/**
* #file media_flash/includes/themes/media-flash.tpl.php
*/
$javascript = '
(function ($) {
...
})(jQuery);
';
drupal_add_js($javascript, 'inline');
The issue is weird. When I'm logged in then everything works fine, all the time. However, when I am using it as an anonymous user it all works fine the first load (after cache clear) but then the two javascripts arent added anymore.
I have tried to change my preprocess function so it adds the javascript with this $variables['scripts'] = drupal_get_js(); but this also has the same behaviour.
Ive googled around a bit and found some suggestions but nothing's worked thus far. Any help is appreciated.
Thanks,
EDIT: So I looked into this a bit more and the code is being executed through the filter module. It seems as though the first time it gets executed it gets cached and then it is just recieved from the cache each time after that, so the drupal_add_js code isn't run again.
Is there a way around this caching or do I need to remove my js from this part all together?
You can always add the js in the page level "hook_page_alter(&$page)"
I am having a hard time understanding what hook_theme() does.
My understanding is that it has something to do with making it possible to override templates.
I was looking at:
$theme_hooks = array(
'poll_vote' => array(
'template' => 'poll-vote',
'render element' => 'form',
),
'poll_choices' => array(
'render element' => 'form',
),
'poll_results' => array(
'template' => 'poll-results',
'variables' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
),
'poll_bar' => array(
'template' => 'poll-bar',
'variables' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL),
),
);
Could you provide an example of how it works?
It provides a place for a module to define its themes, which can then be overridden by any other module/theme. It will also provide the opportunity for any module to use a hook such as mymodule_preprocess_theme_name to change the variables passed to the eventual theme function or template file.
There are basically two ways to initialise a theme function:
theme('poll_results', array('raw_title' => 'title', 'results' => $results, etc...));
and
$build = array(
'#theme' => 'poll_results',
'#raw_title' => 'title',
'#results' => $results,
etc...
); // Note the '#' at the beginning of the argument name, this tells Drupal's `render` function that this is an argument, not a child element that needs to be rendered.
$content = render($build); // Exact equivalent of calling the previous example now that you have a render array.
Please keep in mind, you should avoid calling theme() directly (per the documentation in theme.inc) since it:
Circumvents caching.
Circumvents defaults of types defined in hook_element_info(), including attached assets
Circumvents the pre_render and post_render stages.
Circumvents JavaScript states information.
In Drupal 8, theme() is a private function, _theme(). For more detail, please see www.drupal.org/node/2173655.
When you compare the two of these to the poll_results element in the example you give above you can probably work out what's happening...since PHP is not a strongly typed language Drupal is providing 'named arguments' through either a keyed array passed to the theme function, or as hashed keys in a render array.
As far as 'render element' is concerned, this basically tells the theme system that this theme function will be called using a render array, with one named argument (in this case form). The code would look something like this:
$build = array(
'#theme' => 'poll_choices',
'#form' => $form
);
This will pass whatever's in the $form variable to the theme function as it's sole argument.
Regarding the template key:
'poll_vote' => array(
'template' => 'poll-vote',
'render element' => 'form',
)
defines a theme called poll_vote which uses a template file (hence the template key) with a name of 'poll-vote.tpl.php' (this is by convention). The path to that template file will be found by using the path to the module that implements it (e.g. modules/poll/poll-vote.tpl.php), so it's fine to put template files in sub-folders of the main module folder.
There are two ways to actually return the output for a theme function, by implementing the physical function name (in this case it would be theme_poll_vote) or by using a template file. If the template key is empty Drupal will assume you've implemented a physical function and will try to call it.
Template files are preferable if you have a fair bit of HTML to output for a theme, or you simply don't like writing HTML in strings inside PHP (personally I don't). In either case though, the variables passed when you call the theme (either using theme() or a render array as described above) are themselves passed through to the template file or theme function. So:
function theme_poll_results(&$vars) {
$raw_title = $vars['raw_title'];
$results = $vars['results'];
// etc...
}
If you were using a template file instead for the same method the variables would be available as $raw_title, $results, etc, as Drupal runs extract on the $vars before parsing the template file.
I'm sure there's a lot I've missed out here but if you have any more specific questions ask away and I'll try to help out.
Drupal 6
I was stuck all day with this and now successfully implemented, so sharing my finding here, may it will help understand hook_theme.
There are 3 steps involved:
hook_theme
function YOURMODULENAME_theme() {
return array(
'xxx_xxx' => array(
'template' => 'xxx-xxx', // define xxx-xxx.tpl.php inside module
'arguments' => array('xxx' => null), //define $xxx so it will available in your xxx-xxx.tpl.php
),
);
}
echo/return the theme in your .tpl or any .module
$output = theme('xxx_xxx', $xxx);
Now variable are magically available in you xxx-xxx.tpl.php.
<?php echo $xxx ?>
Note: you can pass $xxx as array,object or anything :)
There is yet another way: (can be found in Bartik theme)
The scenario here is that we have created our own module and want to override the default output for let's say a node with title 'zzz' only.We don't know and don't really care how the default output is generated. All we need is to tell Drupal to use our own custom template file (node--custom--name.tpl.php) to render that specific node.
These are the steps:
Tell Drupal where our template file lives. (Keep in mind that this function will take effect only once and after clearing Drupal's cache):
// Implements hook_theme()
function mymodulename_theme() {
$theme = array();
$theme['node__custom__name'] = array(
'render element' => 'node',
'template' => 'path_from_mymodule_root/node__custom__name',
);
return $theme;
}
Tell Drupal where and when to use it
//Implements hook_preprocess_node()
function mymodulename_preprocess_node($vars) {
if($vars['node']->title == 'zzzz') {
$vars['theme_hook_suggestions'][] = 'node__custom__name';
... your other code ...
}
}
Now Drupal will use our template file for that specific case only, provided that 'node--custom--name.tpl.php' file is in the declared path, otherwise it will keep searching according to the suggestions naming conventions for a fallback template.