the_toolman Posted July 21, 2019 Share Posted July 21, 2019 Hi, I am trying to add the following to a line of PHP to give a link a class. It works fine when I do it like this and the below automatically adds the class="" part to the HTML <section id="title" <?php job_listing_class(); ?>> But when I try the following, it doesn't work: $output .= '<li><a href="' . get_term_link( $subterms ) . '" ' . job_listing_class(); . ' >' . $subterms->name; if($jobs_counter=='yes'){ $output .= '<span>' . $subterms->count . '</span>'; } $output .= '</a> It's this part that I can't get to work: $output .= '<li><a href="' . get_term_link( $subterms ) . '" ' . job_listing_class(); . ' >' . $subterms->name; Can anyone help me with the code I have? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/ Share on other sites More sharing options...
Barand Posted July 21, 2019 Share Posted July 21, 2019 (edited) It would appear that your function is echoing the class and therefore it works in the first code EG function job_listing_class() { echo 'class="xyz"'; } In the second code you are concatenating the output from the function but there isn't any. In this case the function would need to return a string value EG function job_listing_class() { return 'class="xyz"'; } Then you can echo '<section id="title" ' . job_listing_class() . ' >'; or, in your HTML <section id="title" <?=job_listing_class()?> > EDIT: Generally, it is better for functions to return values. Edited July 21, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568550 Share on other sites More sharing options...
the_toolman Posted July 21, 2019 Author Share Posted July 21, 2019 Hi, Thanks for the reply. I see what you mean. The problem I'm having is adding the actual <?=job_listing_class()?> to this line of PHP: <a href="' . get_term_link( $subterms ) . '"> $output .= '<ul>'; foreach ($chunk as $subterms) { $output .= '<li><a href="' . get_term_link( $subterms ) . '">' . $subterms->name; if($jobs_counter=='yes'){ $output .= '<span>' . $subterms->count . '</span>'; } $output .= '</a></li>'; } $output .= '</ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568551 Share on other sites More sharing options...
Barand Posted July 21, 2019 Share Posted July 21, 2019 You use the <?= ...?> synatx when inserting a PHP expression into your HTML. With your 10 minutes ago, the_toolman said: <a href="' . get_term_link( $subterms ) . '"> you are trying to insert into a string value within your PHP code so you would just concatenate the function output as shown in my "echo" example $output .= '<li><a href="' . get_term_link( $subterms ) . '" ' . job_listing_class() . '>' . $subterms->name; (assuming your function now returns the string value instead of echoing it) Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568552 Share on other sites More sharing options...
the_toolman Posted July 21, 2019 Author Share Posted July 21, 2019 Hi, Thanks for the reply. Yes, it's doing what you said, its echoing it: class=""class=""class=""class=""class=""class=""class=""class=""class=""class=""class=""class=""class="" I will see if I can work out another way. Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568555 Share on other sites More sharing options...
the_toolman Posted July 21, 2019 Author Share Posted July 21, 2019 Ok, I have trid this which sort of does what I am wanting it to do,: $output .= '<li class="category-item"><a href="' . get_term_link( $subterms ) . '" class="' . $subterms->name . '">' . $subterms->name; I'm basically trying to give each item in a list of categories its own class so I can style it differently. The above works, but it outputs the class with spaces in: <a href="https://www.website.co.uk/phase-2/category/arts-and-crafts-competitions/" class="Arts & Crafts Competitions">Arts & Crafts Competitions<span>0</span></a> Is there a way I can remove the spaces from the class or replace the space with a hyphen? I assume I can't use classes with spaces in. Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568556 Share on other sites More sharing options...
Barand Posted July 21, 2019 Share Posted July 21, 2019 What does your job_listing_class() function look like? Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568557 Share on other sites More sharing options...
maxxd Posted July 22, 2019 Share Posted July 22, 2019 It looks like you should urlencode($subterms->name). What CMS are you using - knowing this might help you get more specific information about your problem. Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568564 Share on other sites More sharing options...
the_toolman Posted July 22, 2019 Author Share Posted July 22, 2019 Hi, It's using WordPress with WP-Job Manager plugin Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568567 Share on other sites More sharing options...
the_toolman Posted July 22, 2019 Author Share Posted July 22, 2019 Hi, I have come up with another solution: $category_name = str_replace(' ', '-', strtolower($subterms->name)); This works, but it is keeping the "&" in the category names. for example: food-&-drink How can I replace the "&" with "and" ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568569 Share on other sites More sharing options...
Barand Posted July 22, 2019 Share Posted July 22, 2019 17 hours ago, Barand said: What does your job_listing_class() function look like? Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568572 Share on other sites More sharing options...
maxxd Posted July 22, 2019 Share Posted July 22, 2019 Try esc_attr() or get_post_field('post_title', $post, 'display') Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568575 Share on other sites More sharing options...
the_toolman Posted July 22, 2019 Author Share Posted July 22, 2019 I am not sure where the job_listing_function is - I don't know much about PHP. This is the contents of the page I am editing: <?php /* * Helpers */ function jobseek_partition( $list, $p ) { $listlen = count( $list ); $partlen = floor( $listlen / $p ); $partrem = $listlen % $p; $partition = array(); $mark = 0; for ($px = 0; $px < $p; $px++) { $incr = ($px < $partrem) ? $partlen + 1 : $partlen; $partition[$px] = array_slice( $list, $mark, $incr ); $mark += $incr; } return $partition; } /** * Limits number of words from string */ if ( ! function_exists( 'jobseek_string_limit_words' ) ) : function jobseek_string_limit_words($string, $word_limit) { $words = explode(' ', $string, ($word_limit + 1)); if (count($words) > $word_limit) { array_pop($words); //add a ... at last article when more than limit word count return implode(' ', $words) ; } else { //otherwise return implode(' ', $words); } } endif; // Shortcode prints grid of categories add_shortcode('jobs_categories', 'jobseek_jobs_categories'); function jobseek_jobs_categories( $atts ) { extract(shortcode_atts(array( 'columns' => '4', 'orderby' => 'count', 'number' => '99', 'hide_empty' => 0, 'jobs_counter' => 'no', 'type' => 'all', 'parent_id' => '', ), $atts)); $output = ''; if( $type == 'all' ) { $categories = get_terms( array( 'taxonomy' => 'job_listing_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, ) ); if ( !is_wp_error( $categories ) ) { $output .= '<div class="category-groups columns-' . $columns . '">'; $chunks = jobseek_partition($categories, $columns); foreach( $chunks as $chunk ) { $output .= '<ul>'; foreach ( $chunk as $term ) { $output .= '<li><a href="' . get_term_link( $term ) . '">' . $term->name; if( $jobs_counter == 'yes' ) { $output .= '<span>' . $term->count . '</span>'; } $output .= '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } if( $type == 'only_parents' ) { $categories = get_terms( array( 'taxonomy' => 'job_listing_category', 'parent' => 0, 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, ) ); if( !is_wp_error( $categories ) ) { $output .= '<div class="category-groups columns-' . $columns . '">'; $chunks = jobseek_partition($categories, $columns); foreach ( $chunks as $chunk ) { $output .= '<ul>'; foreach ( $chunk as $term ) { $output .= '<li><a href="' . get_term_link( $term ) . '">' . $term->name; if( $jobs_counter == 'yes' ) { $output .= '<span>' . $term->count . '</span>'; } $output .= '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } if( $type == 'group_by_parents' ) { $parents = get_terms( array( 'taxonomy' => 'job_listing_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, 'parent' => 0 )); //CATEGORY HEADERS if ( !is_wp_error( $parents ) ) { foreach( $parents as $key => $term ) : $subterms = get_terms("job_listing_category", array("orderby" => $orderby, "parent" => $term->term_id, 'hide_empty' => $hide_empty)); $output .= '<h3><a href="' . get_term_link( $term ) . '">'. $term->name .'</a></h3>'; if( $subterms ) : $chunks = jobseek_partition($subterms, $columns); foreach ($chunks as $chunk) { $output .= '<div class="category-boxes ' . strtolower($term->name) . '-category-wrapper' . '">'; foreach ($chunk as $subterms) { $category_name = str_replace(' ', '-', strtolower($subterms->name)); $output .= '<div class="category-item"><a href="' . get_term_link( $subterms ) . '" class="'. $category_name .'">' . $subterms->name; if($jobs_counter=='yes'){ $output .= '<span>' . $subterms->count . '</span>'; } $output .= '</a></div>'; } $output .= '</div>'; } endif; endforeach; } } if( $type == 'parent' ) { if ( !is_wp_error( $categories ) ) { $subterms = get_terms( array( 'taxonomy' => 'job_listing_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, 'parent' => $parent_id, )); $term = get_term( $parent_id, "job_listing_category" ); if( $subterms ) { $output .= '<div class="category-groups"><h3 class="parent-jobs-category"><a href="' . get_term_link( $term ) . '">'. $term->name .'</a></h3>'; $chunks = jobseek_partition($subterms, $columns); foreach ( $chunks as $chunk ) { $output .= '<ul>'; foreach ($chunk as $subterms) { $output .= '<li><a href="' . get_term_link( $subterms ) . '">' . $subterms->name; if($jobs_counter=='yes'){ $output .= '<span>' . $subterms->count . '</span>'; } $output .= '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } } return $output; } add_shortcode('resume_categories', 'jobseek_resumes_categories'); function jobseek_resumes_categories( $atts ) { extract(shortcode_atts(array( 'columns' => '4', 'orderby' => 'count', 'number' => '99', 'hide_empty' => 0, 'resumes_counter' => 'no', 'type' => 'all', 'parent_id' => '', ), $atts)); $output = ''; if( $type == 'all' ) { $categories = get_terms( array( 'taxonomy' => 'resume_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, ) ); if ( !is_wp_error( $categories ) ) { $output .= '<div class="category-groups columns-' . $columns . '">'; $chunks = jobseek_partition($categories, $columns); foreach ($chunks as $chunk) { $output .= '<ul>'; foreach ($chunk as $term) { $output .= '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } if( $type == 'group_by_parents' ) { $parents = get_terms( array( 'taxonomy' => 'resume_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, 'parent' => 0 ) ); if ( !is_wp_error( $parents ) ) { foreach( $parents as $key => $term ) { $subterms = get_terms( array( 'taxonomy' => 'resume_category', 'orderby' => $orderby, 'parent' => $term->term_id, 'hide_empty' => $hide_empty ) ); if( $subterms ) { $output .= '<div class="category-groups columns-' . $columns . '"><h3 class="parent-resumes-category"><a href="' . get_term_link( $term ) . '">'. $term->name .'</a></h3>'; $chunks = jobseek_partition($subterms, $columns); foreach( $chunks as $chunk ) { $output .= '<ul>'; foreach( $chunk as $subterms ) { $output .= '<li><a href="' . get_term_link( $subterms ) . '">' . $subterms->name . '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } } } if( $type == 'parent' ) { if ( !is_wp_error( $categories ) ) { $subterms = get_terms( array( 'taxonomy' => 'resume_category', 'orderby' => $orderby, 'hide_empty' => $hide_empty, 'number' => $number, 'parent' => $parent_id, ) ); $term = get_term( $parent_id, 'resume_category' ); if( $subterms ) { $output .= '<div class="category-groups columns-' . $columns . '"><h3 class="parent-resumes-category"><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></h3>'; $chunks = jobseek_partition($subterms, $columns); foreach( $chunks as $chunk ) { $output .= '<ul>'; foreach( $chunk as $subterms ) { $output .= '<li><a href="' . get_term_link( $subterms ) . '">' . $subterms->name . '</a></li>'; } $output .= '</ul>'; } $output .= '</div>'; } } } return $output; } ?> The part where I am updating is under the comment //CATEGORY HEADERS Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568577 Share on other sites More sharing options...
the_toolman Posted July 23, 2019 Author Share Posted July 23, 2019 I used this in the end: $cat_name = $subterms->name; $amp = array(" & "); $and = array("-and-"); $category_name = str_replace($amp, $and, strtolower($cat_name) ); Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568638 Share on other sites More sharing options...
ginerjm Posted July 23, 2019 Share Posted July 23, 2019 Or you could have just said: $cat_name = str_replace('&', 'and ', strtolower($cat_name)); No need to define arrays for this purpose nor to create a second field to hold category name. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568639 Share on other sites More sharing options...
the_toolman Posted July 24, 2019 Author Share Posted July 24, 2019 Thanks for the reply. I ended up with this code to remove spaces too. I now have an issue with the category's displaying a comma in the classes, for example: <span class="movies,-film-and-tv"></span> Could anyone tell me how can I remove the comma from the string? $cat_name = $subterms->name; $amp = array('&', ' '); $and = array('and', '-'); $category_name = str_replace($amp, $and, strtolower($cat_name) ); $category_name = str_replace( ',', '', $cat_name); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568652 Share on other sites More sharing options...
Barand Posted July 24, 2019 Share Posted July 24, 2019 Describe to yourself (or someone else if that helps) what the last two lines of code are doing. Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568653 Share on other sites More sharing options...
the_toolman Posted July 24, 2019 Author Share Posted July 24, 2019 Sorry, that last line wasn't meant to be there... that we me trying to replace the comma with "nothing" to remove it. $category_name = str_replace( ',', '', $cat_name); Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568655 Share on other sites More sharing options...
ginerjm Posted July 24, 2019 Share Posted July 24, 2019 STOP creating NEW VARIABLES!! That is your problem here! Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568656 Share on other sites More sharing options...
ginerjm Posted July 24, 2019 Share Posted July 24, 2019 IF you don't understand what I said above, take a fresh look at the code I provided you earlier and read it carefully and try to understand what you are doing differently. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568659 Share on other sites More sharing options...
maxxd Posted July 24, 2019 Share Posted July 24, 2019 On 7/22/2019 at 8:21 AM, maxxd said: Try esc_attr() or get_post_field('post_title', $post, 'display') Either of these functions will do what you want, and without you having to recreate the wheel. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308995-syntax-help/#findComment-1568663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.