简体   繁体   English

Wordpress:将if-else语句插入next_post_link()/ previous_post_link()参数?

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

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. 充其量只是一个PHP黑客而我正在构建的Wordpress主题迫使我学到很多东西。

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). 我遇到了一个问题,试图让下一个和上一个链接的格式根据各自的类别(而不是single.php的页面)进行不同的显示。

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-else语句吗?

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. 我遇到的问题是a)如何确定next_post_link()和previous_post_link()的类别,然后b)相应地显示它们的Link参数。 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. 如果我可以抛出白旗,我发现使用get_adjacent_post()和各种数组等的例子在这一点上似乎已经超出我的想象 I'd really appreciate someone walking me through any solutions using them. 我真的很感谢有人带我走过使用它们的任何解决方案。

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? 最后:你想为视觉样式输出大量的HTML标签吗? Or do you want to use CSS for that. 或者你想使用CSS

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. 演示如何获取相关链接的CSS类。
  • Extends a theme's output with these CSS-classes. 使用这些CSS类扩展主题的输出。
  • 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. 所以这个例子通过你的主题CSS来涵盖视觉样式的链接帖子的类别,我认为这是更好的方式。

The PHP code PHP代码

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. PHP非常灵活,所以它最终很容易完成,但不知道究竟要做什么,PHP的这种灵活性可能会成为一个问题,因为它也可能会松散跟踪,然后编写一些不起作用的代码。

Let's just say you just want to change the link-format for the next_post_link() function. 我们只想说你想改变next_post_link()函数的链接格式

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!' 该函数将使用HTML链接标记( <a href=...>Post Title</a> )替换%link ,因此如果你选择'This is the next post of my blog: %link; waiting to be read!' '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: 您还可以在其周围添加HTML标记:

<?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. 所以现在为了使主题更加容易,最好是添加类别的css类,然后你可以在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. 这将把下一个帖子链接包装到一个<span>中,该<span>包含链接帖子的所有CSS类加上一个next-post-link类,以防你需要这个CSS选择器来设置链接的样式。 Here some example CSS: 这里有一些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. 由于您有next和prev链接,因此只能更改一个参数来复制该代码。 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 . 将此函数放入主题的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. 如果你把它们放在一起,你可以轻松地设置主题CSS中的next和prev链接。

Have you tried anonymous functions ? 你试过匿名功能吗? In that way you can use if-else statements within a function 这样,您就可以在函数中使用if-else语句

Try: 尝试:

$format_string = in_category('tweet') ? 
'<span style="color:red;">%link</span>' : '%link';

next_post_link($format_string);

The second and third parameters are optional and default to title and false. 第二个和第三个参数是可选的,默认为title和false。

Edit: 编辑:

I don't know this plugin, but.. 我不知道这个插件,但..

if(in_category('tweet')){
$format_string = '<span style="color:red;">%link</span>';
$link_text = get_the_content();
}else{
$format_string = '%link';
$link_text = '%title';  
}

next_post_link($format_string, $link_text);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从next_post_link和previous_post_link(Wordpress)中排除某些类别? - How to exclude some Categories from next_post_link and previous_post_link (Wordpress)? Wordpress next_post_link / previous_post_link不属于同一类别 - Wordpress next_post_link/previous_post_link not staying in same category Wordpress:previous_post_link / next_post_link按字母顺序排列? - Wordpress: previous_post_link / next_post_link by alphabetical order? 如何将PHP代码放入wordpress分页功能(previous_post_link,next_post_link等)中? - How to put PHP code in wordpress pagination function (previous_post_link,next_post_link,etc)? WordPress-next_post_link / previous_post_link提供文本而不是链接 - Wordpress - next_post_link/previous_post_link giving text instead of links 如何在简码中显示 previous_post_link () / next_post_link () - Wordpress - How to show previous_post_link () / next_post_link () in shortcode - Wordpress 我在 single.php 中使用 previous_post_link() 和 next_post_link() 的单个帖子导航遇到问题,但不能使用帖子类型“post” - I am getting issues with my single Post navigation in single.php with previous_post_link() and next_post_link() is not working with post type "post" 使用html链接调用wordpress previous_post_link函数 - Calling wordpress previous_post_link function with an html link 将Google Analytics添加到Wordpress的next_post_link功能 - Adding Google Analytics to Wordpress' next_post_link function 从WordPress的功能获得页面标题previous_post_link() - obtaining page title from wordpress function previous_post_link()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM