I use Business Directory plugin for directory website. I want to restrict title to 100 characters or fewer.
I tried in CSS to restrict, but this doesn't work:
case 'title':
$value = sprintf( '<a href="%s" target="%s" >%s</a>',
get_permalink( $post_id ),
wpbdp_get_option( 'listing-link-in-new-tab' ) ? '_blank' : '_self',
get_the_title( $post_id ) );
break;
Use substr()
substr('get_the_title( $post_id )', 0, 100);
Related
I recently bought a new Wordpress theme but all my custom excerpts are being cut-off based on character count rather than word count. I've found the excerpt.php file that the theme is using and I think the issue down comes down to the use of substr, but I can't figure out how to change it to fix it. I tried replacing it with wp_trim_words but then I just saw a number rather than text in my excerpts.
Would be grateful if someone can point me in the right direction!
The code:
$excerpt = get_the_excerpt();
if ( ! empty( $excerpt ) ) {
$excerpt_length = healthfirst_get_search_page_excerpt_length();
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
?>
<p itemprop="description" class="qodef-e-excerpt">
<?php echo esc_html( strip_tags( strip_shortcodes( $new_excerpt ) ) ); ?>
</p>
<?php }
You can replace this line..
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
.. with.
$new_excerpt = wp_trim_words( $excerpt, 10 );
You do not need the condition since you are trying to override the excerpt length. You can actually remove this line afterwards.
$excerpt_length = healthfirst_get_search_page_excerpt_length();
Unless it is still used somewhere else in that file or function.
Im using the searchWP plugin in my wordpress site but the results of the keyword is showing html tags in the description. (see picture)
Is it possible to use a filter to remove those tags from showing up but still show the text without the tags?
The tags are inside the wordpress wysiwyg editor.
Update.
The search-form uses vue.
So if it is possible to trim the html tags from the results using vue i would prefer that.
I'm not tested try this.
function wpse159789_posts_search( $search, $query ) {
global $wpdb;
if ( ! preg_match( '/' . $wpdb->posts . '\.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
return $search;
}
$search_str = stripslashes( $matches[1][0] );
// Cater for closed angle pairs embedded in the search string.
for ( $i = 0, $len = mb_strlen( $search_str ); $i < $len; $i++ ) {
$q_searches[] = '(<[^>]*>)?' . preg_quote( mb_substr( $search_str, $i, 1 ) );
}
$q_search = implode( '', $q_searches );
$regexs[] = '^[^<]*' . $q_search; // Before any angle bracket.
$regexs[] = '(<[^>]*>)[^<]*' . $q_search; // After any closed angle bracket pair.
array_unshift( $regexs, implode( ' OR ', array_fill( 0, count( $regexs ), $wpdb->posts . '.post_content RLIKE %s' ) ) );
$search_replace = call_user_func_array( array( $wpdb, 'prepare' ), $regexs );
$search = substr( $search, 0, $matches[0][1] ) . $search_replace . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
return $search;
}
i've read many Q/A's here during the last few days, but unfortunately none of them solved my issue.
I'm trying to fetch product attributes and display them on the frontend with a shortcode. I have managed to display ALL available attributes and display them in a list, but i need to select only one or two of them in different locations (thats why using shortcodes). For example like [shortcode_attribute name="brand"].
Any help is highly appreciated!
my code so far:
function tutsplus_list_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div><li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li></div>' );
}
}
add_action( 'woocommerce_product_meta_end', 'tutsplus_list_attributes' );
add_shortcode('display_attributes', 'tutsplus_list_attributes');
While I am still not 100% clear what you're after, what I'm going to create is a shortcode that creates a list of all terms in all attributes. To make it more flexible I'll add support for a shortcode parameter so you can create a list of specific attribute terms.
One major thing you need to alter from your code, is that shortcodes need to RETURN a string and not ECHO out html. Echoing out shortcodes can result in unexpected weirdness.
/**
* Attributes shortcode callback.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
Usage: This would be used in 1 of 2 ways.
First, to display specific attributes use:
[display_attributes attributes="color|material"]
Second, to display all attributes use:
[display_attributes]
Edit
Here's an always single display edition:
/**
* Attribute shortcode callback.
*/
function so_39394127_singular_attribute_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attribute' => ''
), $atts );
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( $args['attribute'] ){
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $args['attribute'], 'pa_' ) === false ? wc_attribute_taxonomy_name( $args['attribute'] ) : $args['attribute'];
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, $attribute_label . ': ' , ', ', '' );
}
}
return $html;
}
add_shortcode( 'display_attribute', 'so_39394127_singular_attribute_shortcode' );
This removes all HTML markup, and so you'd need to provide your own... which would happen if you are making a custom list.
<ul>
<li class="arrow">Static List Item</li>
<li class="arrow">[display_attribute attribute="color"]</li>
<ul>
EDIT: This code can be added to your theme's functions.php, but preferably either in a site-specific plugin or via the Code Snippets plugin.
If anyone else just wants to simply output the attribute value as text, replace the $html .= line with this:
$html .= strip_tags(get_the_term_list($product->get_id(), $taxonomy));
#helgatheviking There must be a more elegant way than this! :)
Needed this recently - found https://wordpress.org/plugins/wcpas-product-attributes-shortcode/ does the trick.
With this plugin you can set an "attribute" parameter to match one of the product attributes you have setup.
For example if you have a brand attribute you can use:
[wcpas_product_attributes attribute="brand"]
This will output a <ul> list containing all brands.
There are also a number of parameters you can use alongside the attribute including:
orderby (order the list by an orderby value)
order (order asc, desc, rand, etc)
hide_empty (hide empty terms)
show_counts (show the total number of products in the brand)
archive_links (use an archive based URL instead of a filter based URL)
min_price (When using archive_links = 0 you can have a price filter based URL off a minimum price)
max_price (When using archive_links = 0 you can have a price filter based URL off a maximum price)
I have a problem with no result page. I have a search box, if you find something then u can see "Search result : ", but if u didnt find then u can see "No Search result : ". I want to remove this NO before search result, but I dont find any solution for this. I tried already many different functions and css, but nothing .. I want to remove this NO, or whole this line (No search result : ).
This is what I have atm in functions.php
add_filter( 'tc_search_results_title' , 'my_search_results_title');
function my_search_results_title(){
$my_search_results_title = __('Search result :', 'customizr-child');
return $my_search_results_title;
}
add_filter('tc_breadcrumb_trail_items', 'search_results_breadcrumb');
function search_results_breadcrumb( $trail ){
if ( ! is_search() )
return $trail;
$_last = sizeof($trail) - 1;
$_search_string = __('Search result: ', 'customizr-child');
/* or you an use the function used for that other snippet to have the same title, in this case remove the comment of the line below */
// $_search_string = my_search_results_title();
if ( is_paged() )
$trail[$_last] = '' . sprintf( '%2$s "%1$s"' , esc_attr( get_search_query() ), $_search_string ) . '';
else
$trail[$_last] = sprintf( '%2$s "%1$s"', esc_attr( get_search_query() ), $_search_string );
return $trail;
}
add_filter( 'tc_no_result_content', 'my_no_result_content');
function my_no_result_content() {
return '<div class="tc-content span12"><h1>Didnt find anything, try again</h1></div>';
}
I use Customizr child theme in Wordpress
Maybe somebody can help me with this.
Thanks !
You're using the wrong filter. You'll have to use tc_search_results_header_content instead and replace the No string. You can also use to original part of code and remove the unwanted part, something like:
function my_search_results_header_content ($content) {
return sprintf( '<div class="row-fluid"><div class="%1$s"><h1 class="%2$s">%3$s%4$s %5$s </h1></div><div class="%6$s">%7$s</div></div>',
apply_filters( 'tc_search_result_header_title_class', 'span8' ),
apply_filters( 'tc_archive_icon', 'format-icon' ),
'', // Original uses: have_posts() ? '' : __( 'No' , 'customizr' ).' ' ,
apply_filters( 'tc_search_results_title' , __( 'Search Results for :' , 'customizr' ) ),
'<span>' . get_search_query() . '</span>',
apply_filters( 'tc_search_result_header_form_class', 'span4' ),
have_posts() ? get_search_form(false) : ''
);
}
add_filter('tc_search_results_header_content', 'my_search_results_header_content');
Hope it helps.
We have Wordpress site in the root of our domain. A translation plugin we use appends the domain with (in our case for Czech) /cs - this means we can run more than one translation but use the same database and wp-content as the main English website.
However, the added /cs causes the logout function to fail as it tries to use the current directory as the basis for where the actual site content is being pulled from.
The actual code being used is <?php echo wp_logout_url( $redirect ); ?>. We have tried a simple HTML href but the logout link is dynamic and requires a unique nonce value to validate the command.
Do you have any ideas for how we can have a logout button that uses the actual site address (mywebsite.com) rather than with the added 'directory' (mywebsite.com/cs). I have thus far been unable to edit the wp_logout_url to add a / before it. Any ideas?
Example links:
Correct:
http://www.mywebsite.com/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a
Incorrect, resulting in 404 ERROR:
http://www.mywebsite.com/cs/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a
Actual code being used (relevant logout code is the #bawlogout# part):
add_filter( 'wp_setup_nav_menu_item', 'bawllm_setup_nav_menu_item' );
function bawllm_setup_nav_menu_item( $item )
{
global $pagenow;
if( $pagenow!='nav-menus.php' && !defined('DOING_AJAX') && isset( $item->url ) && strstr( $item->url, '#baw' ) != '' ){
$item_url = substr( $item->url, 0, strpos( $item->url, '#', 1 ) ) . '#';
$item_redirect = str_replace( $item_url, '', $item->url );
switch( $item_url ) {
case '#bawloginout#' :
$item_redirect = explode( '|', $item_redirect );
if( count( $item_redirect ) != 2 )
$item_redirect[1] = $item_redirect[0];
for( $i = 0; $i <= 1; $i++ ):
if( $item_redirect[$i] == '%actualpage%')
$item_redirect[$i] = $_SERVER['REQUEST_URI'];
endfor;
$item->url = is_user_logged_in() ? wp_logout_url( $item_redirect[1] ) : wp_login_url( $item_redirect[0] );
$item->title = bawllm_loginout_title( $item->title ) ; break;
case '#bawlogin#' : $item->url = wp_login_url( $item_redirect ); break;
case '#bawlogout#' : $item->url = wp_logout_url( $item_redirect ); break;
case '#bawregister#' : if( is_user_logged_in() ) $item->title = '#bawregister#'; else $item->url = site_url( '/wp-login.php?action=register', 'login' ); break;
}
$item->url = esc_url( $item->url );
}
return $item;
}
I would put the URL into a variable so that you can perform a regular expression on it, for example:
<?php
$url = wp_logout_url( $redirect );
$fixed_url = preg_replace("/stuff_to_find/", "stuff_to_replace", $url);
?>
In the end I took the easy way out and use a Redirect 301 in .htaccess to redirect that logout link with /cs/ to /