首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress代码标准与短代码中的工作$link参数

Wordpress代码标准与短代码中的工作$link参数
EN

WordPress Development用户
提问于 2021-01-07 12:02:31
回答 1查看 63关注 0票数 1

我有这段代码,

代码语言:javascript
复制
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    extract( 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
     * $link 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 ($link != false) ? sprintf( '%s, ', esc_url( get_term_link($parent_term) ), $parent_term->name ) : "{$parent_term->name}, " ;
        }

        echo ($link != false) ? sprintf( '%s', esc_url( get_term_link($term) ), $term->name ) : $term->name ;
    }   

    return ob_get_clean();
}

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

代码语言:javascript
复制
**[parent-child]**
• New York
• New York, Manhattan

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

**[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

一切正常工作,并显示如预期!just...my参数不起作用。

我没能得到的工作。在我研究短代码如何工作的过程中,我偶然发现了这些帖子,

link1 link2 link3

他们都说永远不要在短代码中使用<#>提取echo --这在wordpress代码中是非常糟糕的做法,而使用$atts$ bad 是非常糟糕的做法。

这让人怀疑这段代码的有效性..。

My现在的问题是:如何使这个短代码与链接=false一起工作,并且在短代码中没有提取和回显。

如果您understood我所说的并且知道如何使它更符合wordpress标准,谢谢!

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2021-01-07 15:42:25

如何使这个短代码与link=false一起工作,并且在短代码中没有提取。

您永远不能将布尔值作为短代码参数传递,而应该将其视为字符串。您对param linkfalse的比较应该用作'false' (string)。

代码语言:javascript
复制
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    // Don't extract, rather parse shortcode params with defaults.
    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city'
    ), $atts, 'parent-child' );

    // Check the $atts parameters and their typecasting.
    var_dump( $atts );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    /* You can pass conditions here to override
     * $link 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 );
            if ($atts['link'] !== 'false') {
                printf( '%s, ', esc_url( get_term_link($parent_term) ), $parent_term->name );
            } else {
                echo $parent_term->name . ', ';
            }
        }

        if ($atts['link'] !== 'false') {
            printf( '%s', esc_url( get_term_link($term) ), $term->name );
        } else {
            echo $term->name;
        }
    }

    return ob_get_clean();
}

在短代码中没有回波

这不是关于使用echo,而是要求您不要在短代码中回显输出。相反,您应该返回它,因为WordPress将用您的输出值替换短代码。在这里,您正在使用ob_start()ob_get_clean()函数缓冲输出并返回它。这很好,也是常用的技术。

票数 2
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/381091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档