Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, September 10, 2013

Randomly Rotate Strings for Display on a WordPress Page Using PHP (Shortcode Exec)

I was asked by our marketing team if we could have our WordPress-based homepage randomly display one of several possible tag lines. Well, sure... with PHP it's dead simple. And if you have Shortcode Exec PHP installed in your WordPress site, it's actually even easy under WordPress.

Here's how I did our particular page. It will randomly choose a string from a PHP array, and present it in a clean looking, rounded-corner wrapper. If you use this, of course you may want to modify the strings and styles to suit your needs. Note, rounded corners aren't fully supported by most IE versions ~ I try not to waste time developing for IE, but if you have a solution, feel free to post it here.

Create a new Shortcode Exec PHP like so...
/* Rotate random hyperbole lines on the homepage */
$hyperboleStrings = array(
1  => "We have the best product",
2  => "Our solution solves problems",
3  => "Industry leading solutions"
);
$whichStringToShow = rand(1, sizeOf($hyperboleStrings));
echo $hyperboleStrings[$whichStringToShow];
Now all you have to do is save it and go to the page you want to add it to. In my case, I had to go to Appearance -> Editor -> header.php...
<div class="rounded_corners" id="hyperbole-line">
<?php echo do_shortcode("[homepage_hyperbole_rotator]"); ?>
</div>
And, here's my CSS...
.rounded_corners {
-moz-border-radius: 10px; /* firefox */
-webkit-border-radius: 10px; /* safari, chrome */
-khtml-border-radius: 10px; /* KDE HTML layout engines */
border-radius: 10px; /* css3 */
}
#hyperbole-line {
background-color: #20b0b0;
width: 85%;
padding: .25em;
text-align: center;
margin: 10px auto 10px auto;
color: #fff;
font-style: italic;
font-size: 1.75em;
padding: 0;
letter-spacing: 1px;
}

Thursday, September 5, 2013

Exclude WordPress Recent Posts Having a Certain Category

Say you have a homepage that you list recent posts on, using the "Recent Posts" widget or something.

Also say that you want to exclude posts that have a certain category. In my case, I have regular blog posts, and I also have a category of blog posts for "Press Releases" that I have a separate section on my homepage for.

Well, I don't want duplicated post links on my homepage, now, do I?

So, here's the hack (I prefer to avoid the term "solution" when dealing with WordPress)...

In my theme's functions.php file (Appearance -> Editor -> Theme Functions), I added the following bit of code at the end (change the red text to the appropriate ID of the category you wish to exclude - you should be able to specify multiple categories by separating them with spaces e.g. '-1 -2 -3'):
function exclude_category($query)
  {
  if ( $query->is_home() )
    {
    $query->set('cat', '-TAG ID OF CATEGORY');
    }
  return $query;
  }
add_filter('pre_get_posts', 'exclude_category');
From what I'm able to gather, the function intercepts the standard procedure for getting posts, via the add_filter function. More of your typical WordPress voodoo black magic, if you ask me... building layers upon layers upon layers /smh.