Display different styles on alternate rows of table (within a loop) - css

I am populating a table with query results and I want the style of the rows to alternate i.e <tr> or <tr class="alt>. (Not using CSS3)
I using a while loop for displaying a row in my table for every result in the resultset.
How do I do this? I am lost.
Help please.
My solution:
if($i % 2) { //this means if there is a remainder
echo "<tr class='alt'>";
} else { //if there isn't a remainder we will do the else
echo "<tr>";
}
$i++;

With a regular for i loop, you'd use modulus (which is the % operator) on i to see if it's a multiple of 2.
In a while loop, you'll need to use another sort of incrementer, perhaps just declaring one before you loop and incrementing it each pass though:
$i = 0;
while (condition) {
$class = (i%2 == 0) ? 'alt' : '';
echo '<tr class="' + $class + '">';
$i++;
}
Caveat: I don't write much PHP, please treat the above as pseudo code. It should be pretty close to working though, if not straight-away correct.

Related

Show shortcode for a given word only once

I'm trying to implement a custom functionality to a wordpress shortcode plugin that shows a tooltip for specified words by automatically calling for information from Wikipedia.
Currently my code snippet works fine, but it shows the tooltips for each occurrence of the word in an article. For example if I specified the word : "dessert" in an array it will show the tooltip for all 5 words "dessert" found in the post. I'm looking for a way to limit the tooltip shortcode to be applied only once per word ( in this case "dessert" which has been specified in the array).
Me code snippet is this:
function add_wikitips($content) {
if( is_single() && is_main_query() ) {
$arr = array('dessert');
for ($i = 0; $i < count($arr); $i++) {
$content = str_replace($arr[$i], '[wiki]'.$arr[$i].'[/wiki]', $content);
}
}
return $content;
}
add_filter('the_content', 'add_wikitips');
I tried adding ob_end_clean(); then
static $content = null;
if ($content === null) {
return $content;
}
, but these methods didn't work. Probably because I didn't implement them properly.
Thanks a lot in advance for any advice and suggestions.
Probably, you can try this: Using str_replace so that it only acts on the first match?
You want the first work to have Wiki link. So replace the first occurrence only when you are doing str_replace()
May be like:
function str_replace_first($from, $to, $content)
{
$from = '/'.preg_quote($from, '/').'/';
return preg_replace($from, $to, $content, 1);
}
echo str_replace_first('abc', '123', 'abcdef abcdef abcdef');
// outputs '123def abcdef abcdef'
Mentioned in Karim's amswer

WordPress rowactions for WP_List_Table ColSpan

I'm currently using the WP_List_Table for displaying my data for my plugin and have rowactions tied to the first column (as show in multiple tutorials). I have approximately 9 columns in my table and would like for the actions row to span across all of the columns (or at least more than one) in the table so that when the browser is resized, the actions aren't wrapped in a single cell. Does anyone know how to accomplish this?
You can override the function called single_row() and write your row with colspan.
ie, Define this function in your child class which extends WP_List_Table.
public function single_row( $item ) {
static $row_class = '';
$row_class = ( $row_class == '' ? ' class="alternate"' : '' );
echo '<tr' . $row_class . '>';
$this->single_row_columns( $item );
echo '</tr>';
// You can add a new row here with custom styles and attributes or edit the above one.
}

Wordpress the_meta and get_post_meta- Hide empty values in custom fields

I'm trying to hide the display of custom fields if they have no values. If I display them with the_meta, then the headers show, whether or not there are any values in the Custom Fields.
It generates this html:
<ul class="post-meta">
<li>
<span class="post-meta-key">My Custom Field Title</span>
</li>
<li>
</ul>
I really don't want titles on the page if there's no content. So I tried:
<?php
$ck = get_post_custom_keys($post_id); //Array
foreach ($ck as $k) {
$cv = get_post_custom_values($k, $post_id ); //Array
foreach ($cv as $c) {
echo (' - ');
echo ($c);
}
}
?>
Echo for $c (the custom values applied to the post) looks like this:
With no values:
1 - 1343633746:1 - - field_5014a45c9a2df - field_5014a48c38f9d - - field_5014a48c2cc82
With values:
1 - 1343603999:1 - 3 cups of eggs - field_5014a45c9a2df - field_5014a48c38f9d - 2 cups of flour - field_5014a48c2cc82
This is probably the long way to go about it. It's showing values for things like "last edit" too, not just the two fields that I put in to test.
How I can show the Custom Fields only if they have values? I'm writing a theme, so I do not know the names of the fields, or how many, beforehand.
Thanks!
You can check for empty values with empty(), you can check if the field starts with underscore (internal values like _edit_last) and skip those.
<?php
$ck = get_post_custom_keys($post_id); //Array
foreach ($ck as $k) {
if (substr ($k, 0, 1) == '_')
{ // skip keys starting with '_'
continue;
}
$cv = get_post_custom_values($k, $post_id ); //Array
foreach ($cv as $c) {
if (empty ($c))
{ // skip empty value
continue;
}
echo ($k . ': ' . $c . '<br/>');
}
}
?>
Is your theme only displaying certain custom fields? Instead of testing against everything, why not just test against whether those specific custom fields are empty?
E.g.
$item = get_post_meta($post_id, "custom_field_1", true);
if (!empty($item)) { // if that field has something
[...] // display what you need to
}

If else not working..

I want to show a template content if ad not available
i use this code but
both ad & template content are showing
<?php
if(dt_show_ads('position=Header&before=<div>&after=</div>'))
{
?>
<!-- Some content here -->
<?php
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
?>
dt_show_ads('position=Header&before=<div>&after=</div>') seems to not return any value. This evaluates to false in PHP.
What dt_show_ads() does, though, is to insert ad html (if there is any).
Therefor, no matter if there are ads or not, your else part is always executed.
A quick google query didn't turn up any sensible documentation on dt_show_ads for me, you might wanna try this though:
if (strlen($ads = dt_show_ads('position=Header&before=<div>&after=</div>&echo=false')) !== 0)
{
echo $ads;
// whatever other content you want to show
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
Edit:
Since, per your comment, dt_show_ads() doesn't support the WP-semi-standard echo argument, you'll need to buffer its output to be able to check it:
ob_start();
dt_show_ads('position=Header&before=<div>&after=</div>');
$ads = ob_get_contents();
ob_end_clean();
if (strlen($ads) !== 0)
{
echo $ads;
// whatever other content you want to show
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
There is no posibility to get both states of the IF statement. There is something wrong with your code. You may have not show up here all of your code correctly ?
Answer #2
The function that you check:
if(dt_show_ads('position=Header&before=<div>&after=</div>'))
{
}
else
{
}
It can print out the appropriate HTML and at the end return a false in example. In that case you get false for the first IF statement and the because of the false you get the else part.
To be sure what is the result of the dt_show_ads(); do that:
echo "<pre>" . print_r(dt_show_ads('position=Header&before=<div>&after=</div>'), true) . "</pre>";
<?php
if (!dt_show_ads('position=Header&before=<div>&after=</div>')) {
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';}
?>

How can the last word of an element be styled?

Given a dynamically loaded heading (using PHP/WordPress) is there a pure CSS method of styling the final word? For instance:
<h1 class='featured'> This is my page title</h1>
Becomes
<h1 class='featured'> This is my page <span id="finalWord">title</span></h1>
If using CSS is not viable, is exploding the content of the <h1> tag the suggested way to do this?
AFAIK there is not a :last-word pseudo class in CSS (although that would be cool). You could use JavaScript for something like this, but if you have access to the <h1> server-side I'd do it with PHP. It's going to be part of the source code and probably easier.
A simple algorithm would be to find the last space in the string with strrpos() and use substr() to piece it back together wrapped in <span>.
I guess a really crude way would be to create a way to all the words and put it in a PHP array. Then echo all the values, and if it's the last one then put the <span id="finalWord"> before and the </span> after.
EX:
Step 1: Create the array
$your_text = "Your text goes here";
$pieces = explode(" ", $your_text);
Now you have all your data in a array. Each word is in it's object.
echo "<h1 class='featured'>";
$the_count = count($pieces);
foreach ($your_text as $i => $value) {
$i = $i + 1;
if ($i == $the_count) { echo "<span id='finalWord'>"; }
echo $value;
if ($i == $the_count) { echo "</span>"; }
}
echo "</h1>";
So basically what this code does is count how many objects are in your array and will check if the object being displayed is the last one. If it is, it will put the correct ID on it.
I just typed this code out real quick, so there could be some errors.
Coulton
is there a pure CSS method of styling
the final word
No such method in principle. CSS cannot modify the DOM.
Take text of dom element, find last word using your own criteria for the "word", wrap it into span and set innerHTML by the result.
Sorry for the follow up on an ancient post, this is the one that came up in my google searches for "wrap the last word in a string with a span tag using php" so I figured this is where I would put the solution I came up with.
Using the above as a starting point, I created this function to accomplish what I needed:
function wrap_last($string, $wrap_start = '<span class="font-bold">', $wrap_finish = '</span>') {
$string_array = explode(' ', $string);
$count = count($string_array);
$new_array = array();
foreach ($string_array as $i => $value) {
$i = $i + 1;
$array_part = '';
if ($i == $count) {
$array_part .= $wrap_start;
}
$array_part .= $value;
if ($i == $count) {
$array_part .= $wrap_finish;
}
$new_array[] = $array_part;
}
$new_string = implode(' ', $new_array);
return $new_string;
}

Resources