Wordpress 在简码中插入参数?

问题描述

只和几个前端编辑一起工作在这些编辑器非常不同并且不灵活!的情况下,我使用 code snippets 以一致的方式在任何地方显示我需要的信息。

如此简单,以至于我觉得没有人理解我在做什么或试图做什么。请仔细阅读并查看插图。

就我今天而言

我有一个名为 “property_city”分类法附加到 CTP“财产”(没什么特别的)

enter image description here

它让我感兴趣,因为我想以这种方式显示它的条款 [父级] -> [父级的子级] -> [父级的子级] -> 等所有的层次结构

让我们试着发布一个广告好吗?

我的公寓位于曼哈顿,所以我选择了曼哈顿认情况下不显示纽约

有了这个代码片段,这是可能的

(/!\不要混淆,我们只使用snippet代码,没有PHP文件或模板来修改。我们只注入一个snippet。/!)

function taxonomy_hierarchy() {
global $post;
$post_id = $post->ID;
$return = '';
$terms = wp_get_post_terms( $post->ID,'property_city' ); //Put your custom taxonomy term here
foreach ( $terms as $term ) {

// this gets the parent of the current post taxonomy
    if ($term->parent != 0) {
        $return .= $term->name. ',' .get_term( $term->parent,'property_city' )->name;
    } else {
        $return .= $term->name;
    }
}
return $return;
}
add_shortcode( 'parent-child','taxonomy_hierarchy' );

完成! 纽约,现在显示曼哈顿。

我的问题是如何使这个短代码[parent-child]更灵活? 只需输出文本(=nolink)参数输出链接(=link)。

在我们的例子中它看起来像这样

[parent-child=nolink] 用于我的循环例如

enter image description here

[parent-child=link] 用于帖子。

enter image description here

如果你知道怎么做,谢谢

解决方法

看看 add_shortcode() documentation,您会看到回调函数传递了三个参数。最重要(与此相关)的是第一个 $atts 参数。

我会做这样的事情:

add_shortcode( 'parent-child','taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    $atts = shortcode_atts( array(
        'link' => true,'taxonomy' => 'property_city'
    ),$atts,'parent-child' );

    global $post;
    $terms = wp_get_post_terms( $post->ID,$taxonomy );

    /* You can pass conditions here to override
     * the link var based on certain conditions. If
     * it's a single post,current user is editor,etc.
     */

    ob_start();
    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent,$taxonomy );
            echo (filter_var($atts['link'],FILTER_VALIDATE_BOOLEAN)) ? sprintf( '<a href="%s">%s</a>,',esc_url( get_term_link($parent_term) ),$parent_term->name ) : "{$parent_term->name}," ;
        }

        echo (filter_var($atts['link'],FILTER_VALIDATE_BOOLEAN)) ? sprintf( '<a href="%s">%s</a>',esc_url( get_term_link($term) ),$term->name ) : $term->name ;
    }   

    return ob_get_clean();
}

此,使用 shortcode_atts() function 允许您为短代码设置一些默认参数。我还对其进行了设置,以便 taxonomy 也可以被覆盖,这使得它更具可扩展性(供以后使用,用于其他项目等)

我也稍微更改了代码以使用 Output Buffering,因为与字符串连接 (imo) 相比,它在处理 Ternary Comparisons 和这样的输出时更快更干净。

它的作用是在确定输出链接名称或纯文本名称之前检查是否已传递 $link 属性,并将结果回显到输出缓冲区中。

这将允许您获得以下结果:

[parent-child]
  • <a href="#">New York</a>
  • <a href="#">New York</a>,<a href="#">Manhattan</a>

[parent-child link="true"]
  • <a href="#">New York</a>
  • <a href="#">New York</a>,<a href="#">Manhattan</a>

[parent-child link="false"]
  • New York
  • New York,Manhattan

[parent-child link="false" taxonomy="some_other_taxonomy"]
  • Top Level Term
  • Top Level Term,Child Level Term

等等。正如我在 PHP 注释中提到的,您可以在 $link 循环之前的任何时间根据您想要的任何条件覆盖 foreach 布尔值。因此,如果 $link 为真,您可以使 is_single() 始终返回真,或者如果当前用户不是编辑者或您能想到的任何其他用户,则始终返回假。

文档和函数参考: