I've Googled my fingers off but just can't find the solution! I'm using a Wordpress Child Theme (on top of Cherry) and simply want to change the length of the Title used and displayed in the Portfolio. It is the default portfolio, not a plug in.
Unfortunately I'm not allowed to provide links due to confidentiality! Any help appreciated.
Neil.
EDIT - Well I'm not a developer so apologies for my clear lack of detail. I don't know which part of functions.php controls this post. I can see it loads theme-portfoliometa.php which has what looks like some likely code but I can't read it well enough to know what's what.
I can manage CSS/HTML but not php. I can provide a link via PM if anyone has the patience to help.
So far, I inserted a php script I found on another post into my-functions.php (child theme) but that changed the excerpt length of the news (posts). I've tried looking for '35' in all php files also which is the current limit.
$the_title = esc_attr ( the_title_attribute ( 'echo=0' ) );
$max_length = 50;
$title_length = strlen ( $the_title );
if ( $title_length > $max_length ) {
$title_excerpt = substr ( $the_title, 0, $max_length ) . '...';
} else {
$title_excerpt = $the_title;
}
This will truncate the title to 50 characters if it is larger and append ...
If the title is <= 50 characters it will just display as normal.
Related
I am trying to create a breadcrumb style link back to the archive/feed page for each of my custom post types, for a magazine style website, preferably without having to make a separate template for each post type as there are a few. (I'm also hoping to be able to use the same breadcrumb style link on my main feed in a featured posts kind of way. EG above each H1 of each featured post, so that if somebody clicked fashion instead of the H1 it would take them through to that archive page, as another way of browsing the site as opposed to having to use the top level menu nav.)
Using the following:
$postType = get_post_type_object(get_post_type());
if ($postType) { echo ''; echo esc_html($postType->labels->name); echo ''; }
the_title('', '');
Stackoverflow has stripped the HTML parts from the above code, not sure what I'm doing wrong there
It is correctly pulling and displaying the name of the post type, eg 'Fashion', however the link returned is for the current post, not the feed page.
has_archive and hierarchical are both set to true when creating post type and I've updated permalinks. Anybody have a solution?
So I had a related problem while back and because I despise plugins I decided to make my own breadcrumb.
Instead of going full on php, I went the js road. The breadcrumb is automatically generated based on the user current url. Which is way more efficient than building it the other way around.
Container to add to your theme, wherever:
<ol id="bread"></ol>
Script to enqueue:
var pathname = location.pathname;
if (((bread = []), pathname.endsWith("/"))) var crumbs = pathname.split("/").slice(1, -1);
else crumbs = pathname.split("/").slice(1);
for (crumb = 0; crumb < crumbs.length; crumb++) {
var url = location.href.split(crumbs[crumb])[0];
if (crumbs[crumb].length > 35) var short = crumbs[crumb].replace(/-/g, " ").substring(0, 35) + "[...]";
else short = crumbs[crumb].replace(/-/g, " ");
bread.push('<li style="display:inline;padding:0 5px;">' + short + "</li>");
}
(document.getElementById("bread").innerHTML = bread.join(">")),
(document.getElementById("bread").lastChild.style.pointerEvents = "none"),
(document.getElementById("bread").lastChild.firstElementChild.style.textDecoration = "none"),
(document.getElementById("bread").lastChild.firstElementChild.style.color = "inherit");
The breadcrumb is unstyled beside display:inline;padding:0 5px; which display the list inline and can be easily modified.
You could probably also achieve the same behaviour in php.
The way I achieved with PHP was
$postType = get_post_type_object(get_post_type());
if($postType) {
esc_html($postType->labels->link); echo esc_html($postType->labels->name);
}
I created an extra label within custom post types replicating the slug in order to spit out the URL correctly. I'm not sure why, but when I tried to echo the slug this way I got a critical error.
When originally searching for the answer I found quite a few posts about this, so while I'm not sure its the perfect solution to this problem, I hope this is helpful for anybody else looking.
I know that people have asked before on here regarding woocommerce and getting the parent category. I've done quite a bit of research through Stack Overflow, and seen quite a lot of answers that don't fit the simplicity of what I'm trying to achieve.
Basically I have my single-product.php page, and we have a site that requires different headers / footer and design based on what category a product belongs in.
To achieve this we just need to access the name of the parent category so we can dictate what header / footer it should load.
I did find this on wordpress.stackexchange, but it's too complex for my needs, so I tried stripping some of the code out and placing directly in the single-product.php page like this:
global $post;
$descendant = get_the_terms( $post->ID, 'product_cat' );
$descendant = array_reverse($descendant);
$descendant = $descendant[0];
$descendant_id = $descendant->term_id;
$ancestors = array_reverse(get_ancestors($descendant_id, 'product_cat'));
$ac = count($ancestors);
$c = 1;
$origin_ancestor_term = get_term_by("id", $ancestors[0], "product_cat");
$origin_ancestor_term->name;
$headername = $origin_ancestor_term->name;
if ($headername == 'Events') {
$productparentcat = 'events';
} else {
$productparentcat = 'adventures';
}
get_header($productparentcat);
Strangely this did seem to work on my local version until I updated the version of wordpress and then it broke. I also get the following errors now:
Notice: Undefined offset: 0 (for $c = 1;)
Notice: Trying to get property of non-object (for the $origin_ancestor_term->name;)
Does anyone know an easy way to just retrieve the parent category name on the single-product template? I've looked a lot of standard wordpress functions to achieve this, but they don't work with woocommerce templates, I guess because they use a custom taxonomy...
Thank you!
After further researching the problem, I realised the answer was ridiculously simple and that the main issue was I was looking for a solution in the wrong place.
I'd become fixated with the idea of finding the products' category parent ID / name, but in fact all the products on the site were included under both child and parent categories.
So for example, a product called "Fiesta Special Edition" was under both "Fiesta" and "Ford", so if I had tried to search for a parent for the category "Fiesta" using the product ID it didn't work, since it was looking for a parent for "Ford" as well and that was the top most level category.
This was what caused my Error regarding "Notice: Undefined offset: 0 (for $c = 1;)".
In the end I used this:
if ( has_term('Ford', 'product_cat', null) ) {
get_header('ford');
} else {
get_header('other-makes');
}
$carclass = '';
if(has_term('Ford', 'product_cat', $post)) {
$carclass = 'ford';
} else {
$carclass = 'other-cars';
}
That allowed me to style the page and pull in the right header depending on if a specific category was ticked.
Is there any way to make the sidebar widget of wordpress to be dynamic? like for example, i would have a twitter widget and it will accept some variables like [ page ] or [ tags ] that will be all different according to the page.
This question is on the one hand too broad and on the other too narrow . To really answer your question a simple "yes" would do . Please try to put some actual code or actual question next time .
Having said that :
Yes, It is possible very Easy to do in the widget code using the conditional tags of wordpress.
Actually, This is the nature of the widgets - to be dynamic and easily customizable .
All you have to do is set your condition on the widget code ( function widget() inside the widget class ).
if ( is_front_page() ) {
$output = 'x'; // content only for front page
} else if ( is_single( array( 17, 19, 1, 11 ) ) ) {
$output = 'y'; // content only ID 17,19,1,11
} else {
$output = 'z'; // content only for all the rest not stated above
}
Refer to the codex page here for general and and to the widgets API
EDIT I after comment
It is up to you to decide which condition mechanism to use . The logic is still exactly the same . may it be if/else, switch , ternary or whatever. If you think about it - what you describe in words in your question, is a 'condition'. in computer logic ( and php ) it is done with conditional statements . and wordpress provides you with an easy mechanism for that . all you have to do is change the $output for whatever you want. tweets about cats, dogs, salmon fishes ,carrots or raspberry pie are exactly the same and you can filter them as such using the page slug for example .
But you will still need conditions. ( also filtering by page slug is a type of condition )
How to do that exactly will depend a lot on what mechanism you use in the specific case to retrieve tweets. Which brings me to my first point of you not posting any details or code .
I've been researching for the past few hours, trying to find a way to disable HTML in WordPress comments. So far this one consistently appeared on top of Google search results numerous times:
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
This code did not work with the latest version of WordPress. I also found many more codes that again, did not work. So how would one go about disabling HTML in WordPress 3.6 (the latest version) comments?
This removed the ability for users to post HTML (but not links for some strange reason) within comments:
add_filter( 'pre_comment_content', 'wp_specialchars' );
This removed the ability for users to post links within comments:
remove_filter('comment_text', 'make_clickable', 9);
To disable HTML tags in comments, put the following code into your theme's functions.php:
add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');
I've been struggling to figure this out for several days now and though I haven't got it to work yet, you folks here at stackoverflow have been the most helpful so far.
To date, I've made 1 custom thumbnail in my functions.php and have been trying to use my theme options code for the other size.
Custom Thumbnail:
add_image_size('br-thumb', 623, 380, true );
Theme Options Code:
<?php if ( $woo_options[ 'woo_post_content' ] != "content" ) woo_image( 'width='.$woo_options[ 'woo_thumb_w' ].'&height='.$woo_options[ 'woo_thumb_h'].'&class=thumbnail '.$woo_options[ 'woo_thumb_align' ]); ?>
The custom is the one that I want for the first two.
I've tried several different loops with different ways of making separate code for the first post, but they were all written for changing only 1 post. I used ones that had numbers in them, such as $i == 1 or $count == 1, so I could change them to == 2, but none of my attempts had any results.
I'm planning on using a category-ID.php file and stripping much of the other theme options from the loop, so have pretty much tried to build it from scratch. The only difference between the posts would be the thumbnails.
Any ideas, super smart people of stackoverflow who have unknowingly saved my skin time and time again by their solutions? :D
Thank you!!
while ( have_posts() ) : the_post();
switch ( $wp_query->current_post ) {
case 0 :
// First image code here
break;
case 1 :
// Second image code here
break;
default :
// All other image code here
}
endwhile;