简体   繁体   English

Wordpress中的类别筛选器简码引用问题

[英]Category Filter Shortcode Quotation Issue in Wordpress

I have been trying to get rid of this issue for few days. 我一直在努力摆脱这个问题几天。 I know where the issue occurs, but couldn't figure out how to solve the issue. 我知道问题出在哪里,但不知道如何解决问题。 Please have a look at the code first: 请先看一下代码:

function portfolio_list_shortcode($atts){

    $q = new WP_Query(
        array('posts_per_page' => -1, 'post_type' => 'portfolio')
        );      


    $list = '$terms = get_terms("portfolio_cat");
            $count = count($terms);
                echo '<div id="wrap"><div class="filtering"><div class="filter" data-filter="all">Show All</div>';

            if ( $count > 0 ){

                foreach ( $terms as $term ) {

                    $termname = strtolower($term->name);
                    $termname = str_replace(' ', '-', $termname);
                  echo '<div class="filter" data-filter=".'.$termname.'">'.$term->name.'</div>';
                }
            }';

    $list.= '</div><div id="Container">';

    while($q->have_posts()) : $q->the_post();
        $idd = get_the_ID();

        $list .= '<div class="mix '.$termname.'">'.get_the_post_thumbnail('portfolio-thumb').'</div>'; 

    endwhile;
    $list.= '</div></div>';
    wp_reset_query();
    return $list;
}
add_shortcode('portfolio', 'portfolio_list_shortcode');  

Running the above code causes the following error: 运行上面的代码将导致以下错误:

Parse error: syntax error, unexpected 'id' (T_STRING)

How can I get rid of echo ' '; 我如何摆脱echo ' '; as I am already using single quotation in return ' ' 因为我已经在return ' '使用了单引号

Thanks 谢谢

I don't know much about wordpress, nor the reason why it looks like this, but nesting quotes three times can get very confusing and should be avoided if possible. 我对wordpress知之甚少,也不了解它为什么会这样,但是将引号嵌套三遍可能会造成混乱,应尽可能避免。

The PHP in your example is invalid in several ways: 您的示例中的PHP在几种方面无效:

  1. Quotes are not properly escaped 引号未正确转义
  2. Some of the HTML within your PHP is just written as PHP and not being echoed PHP中的某些HTML只是作为PHP编写的,没有回显
  3. Your function is returning PHP code 您的函数正在返回PHP代码

It requires some thorough fixing, but I can't answer that without an explanation as to what the function should do. 它需要进行一些彻底的修复,但是如果不解释该函数的功能,我将无法回答。 Should it return anything, and if so, what should it return? 它应该返回什么,如果是,应该返回什么?

Assuming it shouldn't return anything, but just print some HTML, you might be looking for something like this: 假设它不返回任何内容,而只是打印一些HTML,则您可能正在寻找如下所示的内容:

function categories() {
    $terms = get_terms("portfolio_cat");
    $count = count($terms);
    echo '<li><a href="javascript:void(0)" title="" data-filter=".all" class="active">All</a></li>';

    if ( $count > 0 ){
        foreach ( $terms as $term ) {
            $termname = strtolower($term->name);
            $termname = str_replace(' ', '-', $termname);
            echo '<li><a href="javascript:void(0)" title="" data-filter=".'.$termname.'">'.$term->name.'</a></li>';
        }
    }
}

A good hint when dealing with messy code is making use of a text editor with syntax highlighting. 处理凌乱代码时的一个很好的提示是使用带有语法高亮显示的文本编辑器。 It makes it easier to spot missing and unescaped quotes, semicolons, misspelled function names and missing braces. 它使查找遗漏和未转义的引号,分号,函数名称拼写错误和括号丢失更加容易。 I can highly recommend Notepad++ or SumblimeText for that purpose. 为此,我强烈建议使用Notepad ++或SumblimeText。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM