Question

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 ' '; as I am already using single quotation in return ' '

Thanks

Was it helpful?

Solution

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.

The PHP in your example is invalid in several ways:

  1. Quotes are not properly escaped
  2. Some of the HTML within your PHP is just written as PHP and not being echoed
  3. Your function is returning PHP code

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:

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top