Wordpress - the_title() limit character in standard widget - wordpress

I copy standard Recent Post widget to function.php, I unregister it and register my new class. In widget I see this function that is responsible for showing title of recent posts in A tag:
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php if ( get_the_title() ) {
$t = the_title();
$t = substr($t, 0, 40); /// this doesn't work
}else{
the_ID();
}
?>
</a>
...
...
But this substr doesn't work - title is always display all. What I do wrong?

You can alsp use mb_substr(), It works almost the same way as substr but the difference is that you can add a new parameter to specify the encoding type, whether is UTF-8 or a different encoding.
Try this:
$t = mb_substr($t, 0, 40, 'UTF-8');
LATER EDIT: change
$t = the_title();
with
$t = get_the_title();
You are using the_title instead of get_the_title to give it to an specific variable. And be sure to echo $t after all of this.

this one should work
echo mb_strimwidth(get_the_title(), 0, 40, '...');

Related

Assign an image to a taxonomy in wordpress?

I am trying to get a different icon to appear next to an event in Wordpress. I am use the "The Event Calendar" plugin and the events are stored as taxonomies. I have three taxonomies. "Tax1", "Tax2" and "Tax3".
I cannot seem to figure out how to call to check what the taxonomies are and assign an icon to each one. Here is what I have so far.
" title="" rel="bookmark">
<?php $taxonomy_exist = taxonomy_exists( 'Tax2' ); ?>
<?php if(is_tax($taxonomy_exist)) { ?>
<p>working!!!!!!!!!!</p>
<?php } else { ?>
<img class="cat-icon" src="/wp-content/uploads/2017/11/Heart.png" />
<?php } ?>
<?php $title = get_the_title($event); ?>
<?php if (strlen($title) > 30) { ?>
<?php echo mb_strimwidth($title, 0, 30, '...'); ?>
<?php } else { ?>
<?php echo $title ?>
<?php }?></a></span><!--.event-title-->
So it is getting to the if statement and failing, showing the heart icon. How can I see if it is part of the "Tax2" taxonomy?
Problem 1:
is_tax() accepts taxonomy names as a first argument. But you put $taxonomy_exist parameter which is boolean. (true/false). Of course it should not work and always return zero. Because there is not any taxonomy called "true" or "false".
Problem 2: Are you sure that tax1, tax2 and tax3 are taxnomies? may be they are terms? if they are terms then i will add a solution code as an update to this answer. If they are taxonomies, then i don't see any logic there. Because taxonomies are not objects to be related to posts. Taxonomy is name of categorization, terms are member elements of those taxonomies.

Target the next ID inside the Loop (WP)

Is it possible to target the next section inside the loop? so that it will scroll down to the next section area.
Here is my code:
<section id ="<?php echo $postid; ?>" class="subpage-wrapper fullscreen background parallax" style="background-image: url('<?php echo $image[0]; ?>')" data-img-width="1400" data-img-height="717" data-diff="100" data-oriz-pos="100%">
<a href="#<?php echo $postid; ?>" class="btn-scroll" data-scroll></a>
</section>
Remember that this is inside the WP_Query Loop
Thanks!
I'm not keeping any selections within your displaying of posts in mind, but I figure you can get 'round by using get_next_post(). Something like:
global $post;
$post = get_post($post_id);
$next = get_next_post();
$next_id = $next->id;
What about using your own counter?
$nextAnchorId = 0;
while ( have_posts() ) { //the loop
$nextAnchorId++; //make this actually be a reference to the *next* post
the_post();
if ($nextAnchorId < $post_count) //don't link to the next post if it's the last one
{
echo "<a href='#post-$nextAnchorId'>next post</a>";
}
}

How to make wordpress title wrap to second line

I'm using a custom theme and I need to find a way to make the title wrap to the second line. The theme has a carousel and the title is displayed below. At the moment, if the title is too long it has "..." after it. How can I stop this and make it wrap to the second line instead?
This is the code from the theme
<div class="home-carousel">
<ul>
<?php
$carimgheight = 120;
if ( get_option('storefront_carousel_img_height' )) {$carimgheight = get_option('storefront_carousel_img_height' ); }
$carimgwidth = 200;
if ( get_option('storefront_carousel_img_number' ) == 2 ) {$carimgwidth = 428;}
if ( get_option('storefront_carousel_img_number' ) == 3 ) {$carimgwidth = 276;}
if ( get_option('storefront_carousel_img_number' ) == 5 ) {$carimgwidth = 154;}
if ( get_option('storefront_carousel_img_number' ) == 6 ) {$carimgwidth = 124;}
//BEGIN Slider LOOP
$loopcarousel = new WP_Query( array( 'post_type' => 'carousel' ) );
while ( $loopcarousel->have_posts() ) : $loopcarousel->the_post();
$buttonlink = get_post_meta($post->ID, "link", true);
if(has_post_thumbnail()) {
$carthumb = get_post_thumbnail_id();
$carimage = vt_resize( $carthumb, '', $carimgwidth, $carimgheight, true );
}
?>
<li>
<img alt="<?php the_title(); ?>" title="<?php the_title(); ?>" src="<?php echo $carimage['url']; ?>" width="<?php echo $carimage['width']; ?>" height="<?php echo $carimage['height']; ?>" /><?php if ( get_option('storefront_carousel_image_only') == "false" ) { ?><p><span class="carousel-title"><?php echo the_title(); ?></span></p><?php } ?>
</li>
<?php endwhile; //END SLIDER LOOP ?>
</ul>
The ellipsis (...) could be added either client side or server side. Chances are pretty it is added using CSS text-overflow: ellipsis;. View the HTML source of the page (the output, not the template), if the full title text is present you can assume the ellipsis is added client side. If you see the ellipsis, you need to track down a custom filter in WordPress that has added it.
Presuming .carousel-title is the selector to which the style is applied, you can comment out or remove the text-overflow: ellipsis; rule altogether. There may be additional vendor prefixed properties which will need to be removed as well, eg. -o-text-overflow: ellipsis. There may also be a -moz-binding: property which is used to add ellipsis support for Firefox versions prior to 4, you can remove that as well.

Wordpress - Add comma to all but last item, remove underscore

I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>

Wordpress Custom Value If/ElseIf - Doesn't Work

I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>
Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>

Resources