Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: Inserting if-else statement into next_post_link() / previous_post_link() parameters?

Tags:

php

wordpress

First question here, though reading and searching has saved my bacon a number of times.

I'm a PHP hack at best, and a Wordpress theme I'm building is forcing me to learn a lot.

I've run into a problem in trying to get the next & previous links' formats to display differently based on their respective categories (and not the single.php's page).

Here's my best try so far:

<?php next_post_link(
    '%link',
    if ( in_category( 'tweet' ) ) {
        get_the_content()
    } else {
        echo '%title'
    };,
    FALSE 
)?>

This results in a syntax error.

My first question is can I even use an if-else statement within a function's parameters?

If I'm approaching it in completely the wrong way, then it's back to the drawing board. I'd appreciate any alternate suggestions, then, as well!

I know the following work:

next_post_link( '%link', get_the_title(), FALSE );
next_post_link( '%link', '%title', FALSE );

To try and be more clear, I'm looking to affect output of the next/previous links based on the category of the links themselves, and not the present host page they are on.

The problem I'm having is a) how to determine what category the next_post_link() and previous_post_link() are, and then b) display their Link parameter accordingly. If I may throw up the white flag, the examples I've found using get_adjacent_post() and various arrays and the like seem way over my head at this point. I'd really appreciate someone walking me through any solutions using them.

like image 854
Will Phillips Avatar asked Jun 18 '11 09:06

Will Phillips


1 Answers

What to aim for?

I think you're mixing stuff a bit too much which makes your problem more complicated than it needs to be. I know with many new things, this can happen quite easily, so I think it's worth to spend some thoughts about the what to be done before starting to code. It should help in the end.

Start to formulate what you want to achieve. If I understand you right you write that you want to display the next and prev links below the current post differently based on category.

But which category? The one of the current post that displays? Or the category of the prev/next linked post?

Next to that, posts can have multiple categories. Do you want to change the display based on one or on multiple of or even all of the categories?

And finally: Do you want to output tons of HTML tags for the visual styling? Or do you want to use CSS for that.

How to do it?

After you have thought about what you actually want to achieve, you can make your mind how this can be done.

In the following I'll show some example code that will do the following:

  • Explains the use of the link-format parameter you want to change.
  • Shows how to get the CSS-classes for the links in question.
  • Extends a theme's output with these CSS-classes.
  • Make it re-useable for different files within your theme.

So this example covers the categories of the linked posts for visual styling via your Theme's CSS which I think is the preferable way.

The PHP code

PHP is pretty flexible so it's in the end easy to get that done, but without knowing what exactly to do, this flexibility in PHP can turn out to be a problem because it's also possible to loose the trail and then code something non-working.

Let's just say you just want to change the link-format for the next_post_link() function.

The function will replace %link with the HTML link tag (<a href=...>Post Title</a>), so if you take 'This is the next post of my blog: %link; waiting to be read!' as parameter, you've added some text around the link.

<?php
  $format = 'This is the next post of my blog: %link; waiting to be read!';
  next_post_link($format);
?>

You can additionally add HTML tags around it as well:

<?php
  $format = '<span style="font-size:2em">%link</span>';
  next_post_link($format);
?>

Which will make all next post links having a double of their standard font-size.

So now to make this a bit more easy for themeing, best would be to add css classes of the category/ies then you can change the display easily within the CSS.

<?php
  $inSameCategory = true; // depends on what you want, can be false as well
  $nextPost = get_adjacent_post($inSameCategory, '', false);
  $cssClasses = get_post_class('next-post-link ', $nextPost);
  $format = sprintf('<span class="$s>%link</span>', $cssClasses);
  next_post_link($format);    
?>

This will wrap the next post link into a <span> containing all CSS-classes of the linked post plus a next-post-link class in case you need this for the CSS-selector to style the links. Here some example CSS:

.next-post-link.category-tweet {font-size: 2em;}

Making it reuseable

As you have a next and prev link you could copy over that code only for changing one parameter. But we're lazy and instead we wrap it into a function of it's own:

  /**
   * my theme's format string for prev|next_post_link()
   *
   * @param bool $previous (optional) previous (true, default) or next (false) post
   * @param bool $inSameCategory (optional) use same category, default: true
   * @return string
   */
  function my_prev_next_link_format($previous = true, $inSameCategory = true) {
    $postId = get_adjacent_post($inSameCategory, '', $previous);
    $cssClasses = get_post_class('next-post-link ', $nextPost);
    $format = sprintf('<span class="$s>%link</span>', $cssClasses);
    return $format;
  }

Place this function into your theme's functions.php. It's available then in all your templates. Using it becomes now very easy:

<?php prev_post_link(my_prev_next_link_format()); ?>
...
<?php next_post_link(my_prev_next_link_format(false)); ?>

If you've put this all together, you can easily style the next and prev links within your theme's CSS.

like image 114
hakre Avatar answered Nov 08 '22 22:11

hakre