I'm trying to add <br /> code to widget titles in Wordpress, as most of my widget's titles are on two lines, and I would like to format the text so it makes more sense to the reader.
I've tried a few different suggested solutions, including adding various code to the Functions file:
add_filter('widget_title', 'change_widget_title', 10, 3);
function change_widget_title($title, $instance, $wid){
return $title = str_replace('Widget Title', '<span style="color: red">Custom</span>', $title);
}
That didn't work for me unfortunately.
Nor did #4 on this page:
http://www.rvamedia.com/wordpress/customize-wordpress-widget-title
I don't even need the title to link to anything, I just want to put in a <br /> so it'll show properly on two lines.
Any help would be greatly appreciated.
I know its a little late for the answer but may be it will help others
Its very simple, You can get help from shortcode by
add_filter('widget_title', 'do_shortcode');
function line_break() {
return '<br />';
}
add_shortcode('br', 'line_break');
Put this in your functions.php file at the end than use <br/> as shortcode i.e. br
I figured out an alternative to this issue.
Instead of putting a br in the widget title, I put the first line of the title as the title, and then inserted a div with the same background color and text color at the very top of the text widget that had the second line.
You can then style this div to have the same qualities as the widget title e.g. no underline, no text transform, etc. to make it look the exact same as the widget title.
Related
I use a product category description in every products category in woocommerce, but i don't know how to show the full description. After a few words it just stops and shows "..."
https://i.stack.imgur.com/BaOx5.jpg
There are 2 general ways I'm aware the text would show ... at the end. Either via code or by CSS text-overflow: ellipsis; If it's using the CSS method, then you got to inspect the element (try right-click on element and choose Inspect, or something similar to that), see which html tag has the style set and then remove it or override it in the stylesheet.
(BTW: If you notice that the full description is there when inspecting the element, then text-overflow CSS is most likely involved.)
Read more here: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow
If it's not CSS but done by code, then it's necessary to get more details on the code used for this to know how to change it. It can be done either by JavaScript or PHP and might be dependent on the WordPress Theme in use.
A quick solution is to observer the source code for the page. See if the full text is in use anywhere else on the page, and then use JavaScript to rewrite the text in the Description area based on it. Read more http://api.jquery.com/html/
Otherwise, you will have to find out if there are any API Hooks that can intercept the Description area to change the text. (Maybe https://codex.wordpress.org/Function_Reference/category_description or http://hookr.io/actions/woocommerce_archive_description/)
Again it might be dependent on the theme. If you are not getting through, add the Theme name to your question, to try get more help.
Here's an example of how to use woocommerce_archive_description
add_action( 'woocommerce_archive_description', 'woo_category_description' );
function woo_category_description() {
if ( is_product_category() ) {
global $wp_query;
$cat_id = $wp_query->get_queried_object_id();
$cat_desc = term_description( $cat_id, 'product_cat' );
$description = '<div class="description">'.$cat_desc.'</div>';
echo $description;
}
}
I have a shortcode [table id=table /]
I want to do something like this <?php echo do_shortcode('[table id="'.$post->post_name.'"/]'); ?>
I want the slug to appear as the table id.
What am I doing wrong?
Given that you mentioned in the comments that you're trying to use this shortcode in a widget - widgets don't parse shortcodes by default.
To make most widgets parse shortcodes (most being those with text fields that apply the widget_text filter), you can add the following to your theme's functions.php:
add_filter("widget_text", "do_shortcode");
You can then refer to this shortcode directly in your widget text like you would expect you can:
[table id=... /]
EDIT:
If you have any trouble running shortcodes that are on their own line, it may be because they get wrapped in <p></p> tags automatically by Wordpress. To stop this happening, add this filter before the one added above:
add_filter("widget_text", "shortcode_unautop");
The shortcode_unautop() function simply stops shortcodes from being wrapped in paragraph tags like is Wordpress' default behaviour in most text fields.
I have a word document, with all my posts already write on it.
So I just have to copy/paste all these posts on my tinyMCE advanced Editor in my Wordpress 4.1
The only problem is that it doesn't take care about line break when I echo them in my blog page.. Whereas I can see these line break on tinyMCE visual preview. And if I check in the tinyMCE text mode, I don't see any <br /> or any <p> tags.
Does anyone had this problem ?
I have created my own template. And it is my first time on Wordpress so I could have miss something, but everyone seems to have a problem with remove_filter ('the_content', 'wpautop'); in their function.php But I don't have this function and I try with add_filter ('the_content', 'wpautop'); but it doesn't change anything.
Thank you in advance!
In your themes template where you output the_content try this:
$getPost = get_the_content();
$postformatted = wpautop( $getPost );
echo $postformatted;
You may want to remove the filter you added if it does not initially work.
This is so crazy that I can't think of what could possibly be happening.
I've got a shortcode defined in functions.php which looks like this:
function company($atts, $content = null) {
extract(shortcode_atts(array(
"linkto" => 'http://url.com',
"status" => 'none',
"size" => 'normal'
), $atts));
return '<div class="company block size-'.$size.'">'.$content.'<span class="status-'.$status.'"></span></div>';
}
add_shortcode("company", "company");
This is followed by the usual, documented way of registering the shortcode, and adding buttons to TinyMCE.
The critical part there is that it returns an anchor enclosing a div (which encloses some content). However, when WordPress renders the page, the HTML output is this:
<p></p>
<div class="company block size-normal"><img class="aligncompanylogo size-full wp-image-60" alt="logo" src="http://path/to/image.png" width="256" height="60"><span class="status-acquired"></span></div>
<a href="http://url.com/">
</a>
<p></p>
You're reading that right: WordPress outputs a p with an anchor first, then the div, followed by the anchor, followed by the content, followed by third anchor, and then a fourth anchor enclosed in p tags. What.
Any ideas on why WordPress outputs HTML completely differently than the way it's supposed to?
EDIT #2: when I try to reproduce your problem, I find the following: Wordpress outputs the HTML as expected but the browser rearranges the DOM. I believe the browser is doing this because your shortcode adds a div element inside a p element which is invalid HTML.
In addition to the workarounds posted below you could prevent Wordpress from turning line breaks into p tags by adding the following to functions.php:
remove_filter( 'the_content', 'wpautop' );
Putting a div within an anchor tag is invalid. Is it possible something somewhere is attempting to fix the invalid HTML?
Something in Wordpress is considering a div nested withing an anchor tag invalid and trying to fix it. Two workarounds,
Change your div to a span.
Change your div to another HTML element and make sure TinyMCE considers it valid by adding a hook to functions.php - in this example, I'm changing the div to article and adding article as a valid child of anchor:
function modify_valid_children($settings){
$settings['valid_children']="+a[article]";
return $settings;
}
add_filter('tiny_mce_before_init', 'modify_valid_children');
I want to display certain text in all my posts except if it's in this one category. How do I do that? Oh yea I almost forgot I want to include the title of the post in the text. So I think I need to use echo, cat='-5', and or something?? I don't know how to form it though. Thanks!
You could use the Wordpress function in_category(). When you use it inside the loop, it returns true if the current post is a member of the category you passed it.
<?php
if ( in_category('my-category'))
{
// don't output text
} else {
// do output text
}
?>
Do you require the text to be completely locked away, or just hidden from view? If you only need it hidden from view (but accessible to anyone who chooses to pry) then you may be able to do it very quickly using css.
If you have coded your theme - or are using someone else's - that adds helpful styles into the header, you may have a lot to work with already. For example, this is a body declaration generated by the Thematic theme:
<body class="wordpress y2011 m01 d31 h12 archive category category-orthopaedics">
Say you have a chunk of content to hide:
<div class="text_to_hide">This is what gets hidden.</div>
Then you declare the CSS as:
.category-orthopaedics .text_to_hide { display: none; }