功能是否打印两次-有什么建议吗?

时间:2020-08-17 作者:Michael

我的函数正确返回结果,但无论出于何种原因,它都会返回两次。有什么建议吗?它用于查询和列出与自定义分类法匹配的自定义帖子类型的快捷码。

function useful_tools_list($atts){
    $atts = shortcode_atts(
        array(
            \'type\'      => \'\',
            \'desc\'      => \'\',
            \'ul\'        => \'\',
            \'li\'        => \'\',
            \'merge_tag\' => \'\',
            \'value\'     => \'\',
            \'tags\'      => \'\',
         ), 
        $atts
    );
    
    $results = \'\';
    if (($atts[\'merge_tag\']) && (!preg_match(\'/\\b\'.$atts[\'value\'].\'\\b/\', $atts[\'merge_tag\']))) {
        $results .= \'\';
    } else {
        $all_terms = get_terms(\'types\');

        foreach($all_terms as $term) {
            wp_reset_query();
            if (($atts[\'type\']) && ($atts[\'type\'] != "")) {
                $args = array(\'post_type\' => \'useful-tools\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'types\',
                            \'field\' => \'slug\',
                            \'terms\' => $term->slug,
                        ),
                    ),
                    \'types\' => $atts[\'type\'],
                    \'tag\' => $atts[\'tags\'],
                 );
            } else {
                $args = array(\'post_type\' => \'useful-tools\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'types\',
                            \'field\' => \'slug\',
                            \'terms\' => $term->slug,
                        ),
                    ),
                    \'tag\' => $atts[\'tags\'],
                 );
            }

            $loop = new WP_Query($args);
            if($loop->have_posts()) {
                if ((!$atts[\'type\']) || ($atts[\'type\'] == "")) {
                    $results .= \'<h2>\'.$term->name.\'</h2>\';
                }
                if (($atts[\'ul\']) && ($atts[\'ul\'] == "true")) {
                    $results .= \'<ul class="tools-type-ul">\';
                }
                while($loop->have_posts()) : $loop->the_post();
                $postID = get_the_ID();
                $desc = \'\';
                $actualDesc = get_post_meta($postID, \'_post_desc\', true);
                if ($atts[\'desc\'] == \'true\' && $actualDesc != \'\') {
                    $desc = \' - \'.get_post_meta($postID, \'_post_desc\', true);
                }
                if (($atts[\'li\']) && ($atts[\'li\'] == "true")) {
                    $results .= \'<li><a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.get_the_title().\'</a>\'.$desc.\'</li>\';
                } else {
                    $results .= \'<a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.get_the_title().\'</a>\'.$desc.\'<br>\';
                }
                endwhile;
                if (($atts[\'ul\']) && ($atts[\'ul\'] == "true")) {
                    $results .= \'</ul>\';
                }
            }
        }
    }
    return $results;
}
add_shortcode(\'useful-tools\', \'useful_tools_list\');
编辑:也尝试了这个,它也输出了正确的结果,但输出了两次。

$all_terms = get_terms(\'useful-tools\');
        
        foreach($all_terms as $term) {
            wp_reset_query();
            if (($atts[\'type\']) && ($atts[\'type\'] != "")) {
                $args = array(\'post_type\' => \'useful-tools\',
                    \'types\' => $atts[\'type\'],
                    \'tag\' => $atts[\'tags\'],
                 );
                
            }

1 个回复
最合适的回答,由SO网友:Michael 整理而成

在意识到我试图对两种不同类型的列表使用相同的短代码后,只需将它们用两种不同的短代码分开就更容易了。

要列出所有帖子并按类型进行分类,请执行以下操作:

/**
 * Shortcode for listing all tools matching tags
 * Usage: [useful-tools-all desc="true" li="true" tags="health, teens"]
 * Alternative PHP Usage: useful_tools_listall(array(desc => \'true\', li => \'true\', \'tags\' => \'health, teens\' ))
 */
function useful_tools_listall($atts){
    $atts = shortcode_atts(
        array(
            \'desc\'      => \'\',
            \'li\'        => \'\',
            \'tags\'      => \'\',
         ), 
        $atts
    );
    
    $results = \'\';
    $all_terms = get_terms(\'types\');

    foreach($all_terms as $term) {
        wp_reset_query();

        $args = array(\'post_type\' => \'useful-tools\',
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'types\',
                    \'field\' => \'slug\',
                    \'terms\' => $term->slug,
                ),
            ),
            \'tag\' => $atts[\'tags\'],
         );

        $loop = new WP_Query($args);
        if($loop->have_posts()) {
            $results .= \'<h2>\'.$term->name.\'</h2>\';
            if (($atts[\'li\']) && ($atts[\'li\'] == "true")) {
                $results .= \'<ul class="tools-type-ul">\';
            }
            while($loop->have_posts()) : $loop->the_post();

                $postID = get_the_ID();
                $desc = \'\';
                $actualDesc = get_post_meta($postID, \'_post_desc\', true);
                if ($atts[\'desc\'] == \'true\' && $actualDesc != \'\') {
                    $desc = \' - \'.get_post_meta($postID, \'_post_desc\', true);
                }
                if (($atts[\'li\']) && ($atts[\'li\'] == "true")) {
                    $results .= \'<li><a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.get_the_title().\'</a>\'.$desc.\'</li>\';
                } else {
                    $results .= \'<a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.get_the_title().\'</a>\'.$desc.\'<br>\';
                }

            endwhile;
            if (($atts[\'li\']) && ($atts[\'li\'] == "true")) {
                $results .= \'</ul>\';
            }
        }
    }
    return $results;
}
add_shortcode(\'useful-tools-all\', \'useful_tools_listall\');

并在不按类型分类的情况下列出帖子(也不针对每种类型重复):

/**
 * Shortcode for listing tools by type and tags
 * Usage: [useful-tools-only type="documents" desc="true" ul="" li="true" merge_tag="{:3:value}" value="B" tags="health, teens"]
 * Param "type" must be the taxonomy\'s exact slug name, not the display name, and it is case-sensitive
 * Params "ul" and "li" set to "true" if you want to include <ul> and <li> tags. 
 * Keep "ul" empty and set "li" true when using in Gravity Form confirmation.
 * Params "merge_tag" and "value" are for conditional components in Gravity Form confirmation.
 */
function useful_tools_listonly($atts){
    $atts = shortcode_atts(
        array(
            \'type\'      => \'\',
            \'desc\'      => \'\',
            \'ul\'        => \'\',
            \'li\'        => \'\',
            \'merge_tag\' => \'\',
            \'value\'     => \'\',
            \'tags\'      => \'\',
         ), 
        $atts
    );
    
    $results = \'\';
    if (($atts[\'merge_tag\']) && (!preg_match(\'/\\b\'.$atts[\'value\'].\'\\b/\', $atts[\'merge_tag\']))) {
        $results .= \'\';
    } else {
        
        $myposts = get_posts(
            array(
                \'showposts\' => -1,
                \'post_type\' => \'useful-tools\',
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'types\',
                        \'field\' => \'slug\',
                        \'terms\' => $atts[\'type\'],
                    )
                ),
                \'tag\' => $atts[\'tags\'],
            )
        );

        if ((!$atts[\'type\']) || ($atts[\'type\'] == "")) {
            $results .= \'<h2>\'.$atts[\'type\'].\'</h2>\';
        }
        if (($atts[\'ul\']) && ($atts[\'ul\'] == "true")) {
            $results .= \'<ul class="tools-type-ul">\';
        }
        
        foreach ($myposts as $mypost) {
            
            $postID = $mypost->ID;
            $desc = \'\';
            $actualDesc = get_post_meta($postID, \'_post_desc\', true);
            if ($atts[\'desc\'] == \'true\' && $actualDesc != \'\') {
                $desc = \' - \'.get_post_meta($postID, \'_post_desc\', true);
            }
            if (($atts[\'li\']) && ($atts[\'li\'] == "true")) {
                $results .= \'<li><a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.$mypost->post_title.\'</a>\'.$desc.\'</li>\';
            } else {
                $results .= \'<a href="\'.get_post_meta($postID, \'_post_url\', true).\'">\'.$mypost->post_title.\'</a>\'.$desc.\'<br>\';
            }
            
        }
        
        if (($atts[\'ul\']) && ($atts[\'ul\'] == "true")) {
            $results .= \'</ul>\';
        }
    }
    return $results;
}
add_shortcode(\'useful-tools-only\', \'useful_tools_listonly\');
感谢您的帮助@t2pe和@Tom。