wp_get_archives link to that particular archive page - wordpress

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.

Related

Wordpress 3.8.1 wp_nav_menu walker that doesn't work?

in an attempt to a custom menu in my brand new theme, I have embedded an array with some parameters as shown in the support page available at the URL http://codex.wordpress.org/Function_Reference/wp_nav_menu
However, these values seems to be overridden or not to work properly.
This is the code I implemeneted
<?php
$defaults = array('container' => 'div', 'container_id' => 'menu', 'container_class' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'fallback_cb' => 'wp_page_menu');
wp_nav_menu( $defaults );
?>
whereas this is the output
<div id="menu" class="menu-main-container"><ul id="menu-main" class="menu">test</ul></div>
Although the container class is an empty string, the code rendered include something not expected.
As for the items_wrap, referring to the guide that says "If a numbered token is omitted from the format string, the related parameter is omitted from the menu markup.", I'd like to understand what I can do to avoid token 1 and 2 to be printed.
I guess I have to go for a custom walker. Is this the case?
As for your first question ( the menu class ) .
You are not really using an empty string. By leaving the string empty you are actually using the default value :
From codex :
$container_class
(string) (optional) The class that is applied to the container
Default: menu-{menu slug}-container
which is what you have in the code ...
class="menu-main-container">
as for the second question, to omit those tokens just put another value without those tokens or with specific parameters along the lines of ..
'items_wrap' => '<ul id="my_whatever_id" class="my_whatever_class">%3$s</ul>',
or
'items_wrap' => '%3$s'
or even a callback
apply_filters("my_filter_menu_items_wrap",'<ul class="nav">%3$s</ul>'),
That being said, I am not quit sure what is the disturbance that the default class / values is creating for you .

Giving a widget my own id or class in Wordpress

I'm writing a Wordpress plugin with a widget. In my jQuery code i'd like to point to that widget, so I though giving it an HTML ID would be nice. (Edited, thanks for pointing out.)
After doing some searching on the internet (and the Codex) I know that I can give ID's to the widgets in the theme, but it's not what I was looking for. This method has flaws. Changing the theme may cause errors (of course, I know it has to be changed in the functions.php, but it's just meh). The other thing is that my widget got a number which may change without me knowing.
So to be 100% sure it works and will work in the future, can I give my widget my own ID?
I might not understand your question exactly and what "ID" do you mean ( widget ID , or actual HTML div ID - which are actually one and the same.. ) but If you have read the codex , the example for how to give an ID is given there ..
function __construct() {
parent::__construct(
'foo_widget', // Base ID
__('Widget Title', 'text_domain'), // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
Another way to do the same ( and helpul if you are talking about HTML elements like divs - you can assign a class )
function My_Widget() {
function My_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays nothing ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
Note that a numerator will be automatically added to your widget´s ID based on how many instances were initiated like :
foo_widget
foo_widget-2
foo_widget-3
etc ...
EDIT I - after comments
At any rate , IMHO it is a bad idea to hard-code a fixed ID in a widget for the simple reason that the preference for a widget from the developer point of view is to always allow support for multiple instances . Giving an an HTML ID anywhere on the widget will cause validation errors AND in the case of jQuery - also JS errors for the simple cause that if a user will have 2 widgets , it will also have a duplicated ID .
In other words - It is the exact opposite of your statement in the original question.
So to be 100% sure it works and will work in the future, can I give my
widget my own ID
Giving a fixed hard coded ID to your widget will in fact ensure that it will NOT work 100% of the time.
The preference is always to target such issues with a class ( or with something like div[id^="my_widget_id"] ) and let wordpress "do it´s thing" ( by auto incrementing IDs ).
For the exact same reason - a theme should always have the same structure in the register sidebar() function like so :
<?php $args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'unique-sidebar-id',
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">', // LOOK AT THIS LINE
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' ); ?>
This will permit the specific sidebar to auto increment the ID of the widgets in order to avoid the above mentioned problem .
From the codex :
before_widget - HTML to place before every widget(default: '') Note: uses sprintf for variable
substitution
All that being said, and if you are insisting of giving a hard-coded fixed ID somewhere there ( instead of a the methods described above ) you can always put that at a nested divor span INSIDE your widget´s HTML output, But I would think that if you have read attentively this answer - you will avoid it now.
Now,- since you have not included any code in your question ( which is always a bad practice here on SE ) there is little more I can do to help. If you encounter any problems targeting the widget without an ID - I suggest you to open a new question and maybe point a link at the comments here, so myself ( and others ) can help you with it ..

Get table header to use pager url drupal 7

I have got a unique issue. In my drupal site, I have got a lot of nodes displayed in a teaser and for each node, I am using a table with a pager. The problem is, on initial load of the page, the table sort doesn't work. However, if I use the pager and move to a different page and then back, then the sorting works.
I investigated this issue and found out that on initial load, the url for the header links use the primary link, which is: mysite.com/node?sort=asc&order=Name. After I click on the pager, then the url for the header links, changes to this: mysite.com/mycallbackfunction?_=1348208999187&page=1&nid=13&pager_id=1&sort=desc&order=Name
If you notice, the url obviously are different, which is okay, but the main thing is, the nid is missing in the initial load. I would like the table headers to use the pager url, or some url that I specify, so the nid would appear in both cases as the function returns nothing if there is no nid.
This is the relevant code in mycallbackfunction which displays the table:
//Attach a theme table
$html = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
"class" => array(
"my_table_box"
),
"cellspacing" => '0',
"cellpadding" => '0'
)));
//Pager theme
$html .= theme('pager', array(
'element' => $pager_id,
'parameters' => array(
'nid' => $nid,
'pager_id' => $pager_id),
'quantity' => ceil($count/5)
));
Hope someone might have an insight into this weird problem.
You can pass the custom parameter in theme('pager') function.
<?php
// e.g.
theme('pager', array('parameters' => array('nid' => $nid))
?>);
Let me know if you are looking for something else.
Good Luck!
-Imran

Wordpress comment_form( ) won't accept my arguments

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!

Overriding the user registration form in Drupal 6

I want to be able to customise the user registration form in Drupal 6
I have found thousands of tutorials that show me how to override the structure in which you can output the form as a whole, but I want to move form elements around etc and I cant quite seem to see the best way to do this
To expand on Jeremy's answer, you're going to want to study Drupal's Form API and user_register(). In short, you build an associated array; each element in the array corresponds to one form element.
Each form element in the array is its own associated array. They can have a type: textfield, select menu, checkboxes, etc.: see the Form API reference for all the types.
Each form element can also have a weight: this is how you order elements around. Lower numbered weights show up before higher numbered weights in the form.
One of the element types available to you is fieldset: this is what will allow you to group elements together. When you use a fieldset, it creates a section of the form with its own weight values.
So, let's say you have a form with three fields: Name, Company, and E-mail address. The Name should show up first, Company second, E-mail address third. You could specify the form like so:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note the #weight key. If you wanted Company to appear after E-mail address, you'd set $form['company']['#weight'] to something higher than 3.
Now let's say you wanted to group Name and Company into a fieldset called Personal Information. Your form would now look something like this:
$form['personal'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#weight' => 1,
);
$form['personal']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['personal']['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note that Name and Company are now array elements of $form['personal'].
If you want to make Name show up after Company in the fieldset, set its #weight higher than 2. Because the Name is now part of a fieldset that has a lower #weight than the E-mail address field, even if you set $form['personal']['name']['#weight'] to 4, it wouldn't make the Name show up after E-mail address.
So what you're going to attempt to do is use hook_form_alter() to alter the user_register form to change the weights of certain form elements, create your own fieldsets, and move certain form elements into your newly created fieldsets.
There are ways to do this within your theme, but I prefer creating a custom module for this. Create your custom module, and implement hook_form_alter():
function test_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user_register') { // Only modify the user registration form
// Before you can get down to business, you need to figure out the
// structure of the user registration form. Use var_dump or kpr to dump
// the $form array.
// Note: if you want to use kpr on the user registration form, give
// anonymous permission to see devel information.
// kpr($form);
// Move Name field to after E-Mail field
$form['name']['#weight'] = 2;
$form['mail']['#weight'] = 1;
// Group Name and E-mail together into a fieldset
$form['personal_info'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
);
$form['personal_info']['name'] = $form['name'];
$form['personal_info']['mail'] = $form['mail'];
// The last block only copied the elements: unset the old ones.
unset($form['name']);
unset($form['mail']);
}
}
In more complex forms, moving things from one fieldset to another might yield unexpected results when submitting the form. This is because $form['name'] isn't the same as $form['group']['name'], which isn't the same as $form['other_group']['name']. You don't have to worry about that on the user_register form for the most part, but check out the handbook page on #tree and #parents for more information about this.
This covers modifying existing fields in the user registration form: if you want to add new fields, I highly recommend using Content Profile. If you want to create custom fields on your own, it's going to get a lot more complex as you're going to have to implement your own validate and submit handlers. Content Profile handles this for you: check out its README to see how to activate it for registration forms.
Using hook_form_alter you can do whatever you want with a form.
For example changing the weight can change the position on the page.
If you try:
MYMODULE_form_user_profile_form_alter(&$form, $form_state) {
// do your processing here
var_dump($form);
}
replacing MYMODULE with the name of your module.
You will see the structure of the form, you can change values in there to alter, labels weights descriptions etc.
In a module, first use hook_theme() , now assuming the name of your module is 'd6_forms' :
function d6_forms_theme() {
return array(
'user_register' => array(
'template' => 'templates/user-register-form',
'arguments' => array('form' => NULL),
),
);
}
This will make the user_register form look for a template, in the specified folder.
So make sure that in your module folder, there is a folder called 'templates', with a file 'user-register-form.tpl.php'.
You notice that in the hook_theme() , the extenstion of the template file ( .tpl.php ) is not supplied. That's normal, you don't need to specify it there.
Do make sure however, that the template has that extension, and that it's not just named 'user-register-form.php' !
In that template file, you have access to the $form variable , so print it there to see what fields are in there.
The devel module is recommened, since it's able to print big Drupal arrays in a fancy way ( using dpm() ).
If you do not have Devel module, or don't want to use it, this also works : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.
To print a field, just use <?php print drupal_render($form[field_name]); ?>, this will print the field and make sure that it works as intended.
So for example, if you want to print the 'name' field in the $form array, just use <?php print drupal_render($form['name']); ?>.
You don't have to print every field ! Just print the fields that you want to move somewhere ( which, with a basic Drupal register form, are about 3 : name, email & submit ).
To print all the remaining fields, just end your template with <?php print drupal_render($form); ?>.
It is important that you don't forget this, since the $form var contains stuff that is absolutely needed for your form to work ( like a token, etc .. ).
So good standard behaviour when templating a form, is to print that piece of code first at the bottom of your template.
This is an entire example of a small register form template, with some basic html :
<?php
// What is in that $form var ? To check, uncomment next line
// print '<pre>' . print_r($form, 1) . '</pre>';
?>
<div style="background-color:#ddd;padding:10px;">
<?php print drupal_render($form['name']); ?>
<?php print drupal_render($form['mail']); ?>
</div>
<div>
<?php print drupal_render($form['submit']); ?>
</div>
<?php print drupal_render($form); ?>
maybe this will help:
http://drupal.org/node/44910
You just need to Enable the Profile Module which would give access to place more fields in sign up form.
Go through this Simple Video tutorial which would be very helpful for beginners in Drupal .
http://planetghost.com/add_more_fields_to_sign_up

Resources