Right now I have a front end form that lists 4 separate taxonomies in checkbox inputs that a user selects. I can list all of the terms one after the other but I can't figure out out to list them with proper hierarchy like so:
-parent
-child
-child
-child
-parent
-child
-child
etc.
Right now my code to output to the form is
$closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&get=all');
$counter = 0;
foreach ($closed_schools as $close) {
$counter++;
$option = '<fieldset id="'.$close->slug.'"><label for="'.$close->slug.'">'.$close->name.'</label>';
$option .= '<input type="checkbox" name="terms[]" id="'.$close->slug.'" value="'.$close->slug.'">';
$option .= '</fieldset>';
echo $option;
}
I have figured out a solution to my problem. It may not be the most elegant solution but I am able to achieve the layout I was looking for. Instead of get=all I replace that with the parent categories id so it only lists the children of that specific id. Since they are in a checkbox array I then perform several loops under each parent to separate the checkbox's each time just adding a new number $closed_schools_1, $closed_schools_2 and so on. would love to here from anyone who may have an easier less repetitive solution but for time being this will work.
<?php
$closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=35');
$counter = 0;
foreach ($closed_schools as $closed_school) {
$counter++;
$option = '<fieldset id="'.$closed_school->slug.'">
<label for="'.$closed_school->slug.'">'.$closed_school->name.'</label>';
$option .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school->slug.'" value="'.$closed_school->slug.'">';
$option .= '</fieldset>';
echo $option;
}
?>
<?php
$closed_schools_2 = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=55');
$counter = 0;
foreach ($closed_schools_2 as $closed_school_2) {
$counter++;
$option_2 = '<fieldset id="'.$closed_school_2->slug.'">
<label for="'.$closed_school_2->slug.'">'.$closed_school_#->name.'</label>';
$option_2 .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school_2->slug.'" value="'.$closed_school_2->slug.'">';
$option_2 .= '</fieldset>';
echo $option_2;
}
?>
Related
I'm trying to create 3 rows of buttons to select. Think sorting boxes. AT&T has it on this page. Where you see iPhone, Samsung and then sorting. I'm trying to do the same thing. AT&T's example page
The first row will be sub categories of parent categories. That part I was successful in completing. However, I'm struggling to call to all the parent attributes.
<?php
global $product;
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
$product_attributes = array_keys($product_attribute);
$count = 0;
echo '<a class="archive-attributes" href="#">';
foreach ($product_attributes as $product_attribute){
$attribute_name = ucfirst( str_replace('pa_', '',
$product_attributes) );
echo $attribute_name . '';
if($count != count($product_attribute)) echo '';
else echo '';
}
echo '</a>';
}
?>
I only need the parent for this row. When one of these is clicked the children of that attribute will display below it. What am I doing wrong?
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... :)
// uses $this->_labelAlign, $this->_col2space, $this->_errorTxtColor, $this->_field, $this->_maxFields
// if $this->_field[$x]['rows'] > 0 assumes text area
public function formatRow ()
{
$output = '';
for ($x = 1; $x <= $this->_maxFields; $x++)
{
if ($this->_field[$x]['active'] == 'Y' || $this->_field[$x]['active'] == 'R') {
$name = 'mod_simpleemailform_field' . $x . '_' . $this->_instance;
$value = (isset($_POST[$name])) ? htmlspecialchars($_POST[$name]) : '';
// prevents Joomla from reformatting using javascript
if (strpos($value, '#')) {
$value = str_replace('#', '#', $value);
}
// 2011-12-03 DB: added CSS classes for input, table, row, th and td
$row = '';
$row .= '<tr' . $this->_trClass . '>';
// labels
$row .= "<th" . $this->_thClass . " align='" . $this->_labelAlign . "'>" . $this->_field[$x]['label'] . "</th>";
// space between cols
$row .= "<td" . $this->_spaceClass . " width='" . $this->_col2space . "'> </td>";
// input field
$row .= "<td" . $this->_tdClass . ">";
// check to see if text area
if ($this->_field[$x]['rows'] > 0) {
$row .= sprintf('<textarea name="%s" id="%s" rows="%d" cols="%d" %s>%s</textarea>',
$name,
$name,
$this->_field[$x]['rows'],
$this->_field[$x]['cols'],
$this->_inputClass,
$value);
} else {
$row .= sprintf('<input type="text" name="%s" id="%s" size="%d" value="%s" maxlength="%d" %s/>',
$name,
$name,
$this->_field[$x]['size'],
$value,
$this->_field[$x]['maxx'],
$this->_inputClass);
}
$row .= ($this->_field[$x]['error'])
? $this->formatErrorMessage($this->_errorTxtColor, $this->_field[$x]['error'])
: '';
$row .= "</td>";
$row .= "</tr>\n";
$output .= $row;
}
}
return $output;
}
This is my module code for the output which shows in table format and i don't want this type of layout as a output... I had also try to do changes with tr, td and th tag but faild to get proper out... so please if anyone help me with this.
Wrong format output what i am getting
Correct format output what i want
Ok, here goes. You have a series of
label
spacer...
input field
So replace the table with a
<div class='form-x'
and set its width as you need in the css, i.e.
div.form-x {width:450px; padding:12px;}
Then you want to replace the cells: the first th will become a label, the first td will be dropped altogther, the second will retain only its content (i.e. input or textarea).
To make your life easier, group each couple label-input within a div so:
<div class='formelement'>
<label for='element-1'><?php echo $this->_labelAlign ?></label>
<input ... or <textarea...
</div>
and in the css
div.formelement {padding:20px 0 0 0}
div.formelement label {display:block;}
div.formelement input, div.formelement textarea {width:100%}
Finally the submit button should be placed in a div with
div.submitbutton {text-align:center}
Regarding styling the input type=file, forget it. It's rendered differently by each browser, there are workarounds though but that's a different story.
I'm busy trying to put together a 3x3 magazine-style layout for the home page of my blog.
I want the divs containing each post to flow vertically so there aren't big white spaces beneath each row of 3. It seems like the easiest way to do this would be to have three columns and fill each of these (which will stop there from being a big space beneath each post-block) and then place these three columns side-by-side.
The problem is that the Wordpress loop needs to pull posts sequentially. I don't know how to change the order of the Wordpress loop and, even though I've tried some PHP trickery using counts and for loops, I can't seem to get it to work.
Any thoughts or advice on a basic Wordpress loop or CSS way to make it work would be much appreciated as it's driving me crazy!
You can see how it is currently at http://www.unleashreality.com/
This looks like a job for jQuery Masonry.
Check it out at http://masonry.desandro.com/
The direct way using loop manipulation could be simpler assuming your layout is going to fixed one as shown in the mockup image.
<?php
$count = 0;
$column_1 = '';
$column_2 = '';
$column_3 = '';
$ad_block = '<div id="ad-block">Ad code here</div>';
while ( have_posts() ) : the_post();
$count++;
$content = '';
$content .= '<div class="post-block">';
$content .= '<h2>'.get_the_title().'</h2>';
$content .= '<div class="excerpt-block">'.get_the_excerpt().'</div>';
$content .= '<div class="continuebutton">...READ THIS ARTICLE</div>'; // Add appropriate code here..
if($count == 1 || $count == 4 || $count == 6) {
$column_1 .= $content;
} else if($count == 2 || $count == 7) {
$column_2 .= $content;
} else if($count == 3 || $count == 5 || $count == 8) {
$column_3 .= $content;
} else {
// Shouldn't come here...
}
// Insert the ad block in column 2 after adding 1st row
if($count == 2) {
$column_2 .= $ad_block;
}
endwhile;
?>
<div id="col1"><?php echo $column_1;?></div>
<div id="col2"><?php echo $column_2;?></div>
<div id="col3"><?php echo $column_3;?></div>
Adjust the code to work with inner pages.
If you want to do this without Javascript, you'll need to buffer the HTML for each column in your post loop and then output it in one shot once the loop is finished.
Something like the following:
<?php
// Hold the buffered column output
$cols = array("", "", "");
// Keep track of column we're appending to
$currentCol = 0;
// Get the posts
$posts = get_posts();
foreach($posts as $post){
// Run the WP post filters
setup_postdata($post);
// Append the content to the current column
$cols[$currentCol] .= '<div class="item">';
$cols[$currentCol] .= get_the_title();
$cols[$currentCol] .= get_the_content();
$cols[$currentCol] .= '</div>';
// Increment the current column and make sure we haven't
// gone over our total columns
if(++$currentCol >= count($cols)){
$currentCol= 0;
}
}
?>
<div id="col1"><?php echo $cols[0]; ?></div>
<div id="col2"><?php echo $cols[1]; ?></div>
<div id="col3"><?php echo $cols[2]; ?></div>
the code looks like this
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
there is an option in wp-admin>settings>reading>For each article in a feed, show
if this is set to full text
the excerpt() must return full length article instead of specified length.
how to do this?
Good question! The answer is simple: write your own function!
Open up functions.php in your favorite editor and mash random buttons on your keyboard until you get something like this:
function my_awesome_excerpt ($post_id = false, $full = false, $length = 22) {
if (!$post_id) global $post;
else $post = get_post($post_id);
$text = $post->post_content;
if ($full) return $text;
else {
$text_array = explode(' ', $text);
$return_string = array();
for ($i = 0; $i <= $length; $i++)
array_push($return_string, $text_array[$i]);
$new_awesome_string = '<p>';
$new_awesome_string .= implode(' ', $return_string);
$new_awesome_string .= '</p><p class="readmore">';
$new_awesome_string .= '<a href="' . get_permalink($post_id) . '">';
$new_awesome_string .= 'Read More' . '</a></p>';
return $new_awesome_string;
}
}
Now, you're ready for the cool part. Once you're in your loop, you can write out some magic like this:
echo my_awesome_excerpt();
and it will automagically spit out an excerpt. It's using the global post variable and everything! You can even use it outside of the loop:
echo my_awesome_excerpt($cpt->ID, 22);
and set your own special length!
Or maybe you just know in your heart that it's not worth it, you just want to show the whole thing. How's that look?
Inside the loop, you're going to have to give it a post ID, sorry about that.
echo my_awesome_script($post->ID, false);
I hope this helps. Have a great day!