WordPress: echoing just the first 60characters of the post title - php

I am styling a WordPress Theme and I would like to make sure that if the post title is longer than 60 characters it shows the first 6ß0 characters +
three points (...) at the end
In Native Php would like:
<?php
if (strlen($title) <= 60) {
echo %title
} else {
echo (substr($title, 60) . "..."
}
?>
My problem is that inside WordPress the syntax of variables is not $title but %title as you could see in the code:
<?php previous_post_link( '%link', '%title ' ); ?>
My questions are:
How would be the final IF inside WordPress
How would be in shorthand if/else (ternary) form?
Thanks

You achieve this by creating your custom post_nav function
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if ($prev_post)
{
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
if (strlen($prev_title) >= 60) //<-- here is your custom checking
{
$prev_title = (substr($prev_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title . '" class=" "><strong><<< "' . $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?php
$next_post = get_next_post();
if ($next_post)
{
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
if (strlen($next_title) >= 60) //<-- here is your custom checking
{
$next_title = (substr($next_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title . '" class=" "><strong>"' . $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>
Hope this helps!

Related

How to change text Home to icon Home on WordPress?

I want to edit the breadcrumbs of website (MH Themes). It only is change the 'text Home' (in echo line) to 'icon Home' (<i class="fa fa-home" aria-hidden="true"></i>). I have try a ten times but I can't.
<?php
/***** Breadcrumbs *****/
if (!function_exists('mh_magazine_breadcrumb')) {
function mh_magazine_breadcrumb() {
if (!is_home() && !is_front_page()) {
global $post;
$mh_magazine_options = mh_magazine_theme_options();
if ($mh_magazine_options['breadcrumbs'] == 'enable') {
$item_counter = 1;
$delimiter = '<span class="mh-breadcrumb-delimiter"><i class="fa fa-angle-right"></i></span>';
$before_link = '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">';
$before_title = '<span itemprop="name">';
$close_span = '</span>';
echo '<nav class="mh-breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">' . $before_link . '' . $before_title . esc_html__('Home', 'mh-magazine') . $close_span . '' . '<meta itemprop="position" content="' . $item_counter . '" />' . $close_span . $delimiter;
$item_counter++;
This is what's creating the Home text:
esc_html__('Home', 'mh-magazine')
That needs to be changed into the icon you want to display.

How to get the number of pages of current wordpress post?

I do split the $post_content using <!--nextpage--> as separator, but maybe there is another method to obtain the number of pages of a single post because I'm doing it many times on large posts.
The WP_Post class doesn't have a member like $post_pages or something.
Use this for a link to previous posts with directional:
<?php previous_post_link(); ?>
Or this for a link to previous posts that without "<<":
<?php previous_post_link('<strong>%link</strong>'); ?>
The code needs to be inside the loop.
Or
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>

get_the_date with using get_adjacent_post wordpress

I want to show post date in get_adjacent_post. But get_the_date($prevpost->ID) shows post_id.
It can show thumbnail and title.
<?php
$prevpost = get_adjacent_post(true, '', true);
$nextpost = get_adjacent_post(true, '', false);
if( $prevpost or $nextpost ){
?>
<div class="cat_paging">
<div class="row">
<?php
if ( $prevpost ) {
echo '<div class="col-sm-6">
<div>Before</div>
<a href="' . get_permalink($prevpost->ID) . '">' .
get_the_post_thumbnail($prevpost->ID, 'thumbnail') . '</a><p>' .
get_the_title($prevpost->ID) . '</p><p>' . get_the_date($prevpost->ID) . '</p>
</div>';
} else {
echo '<div class="col-sm-6">TOP
</div>';
}
if ( $nextpost ) {
echo '<div class="col-sm-6">
<div>Next</div>
<a href="' . get_permalink($nextpost->ID) . '">' .
get_the_post_thumbnail($nextpost->ID, 'thumbnail') . '</a><p>' .
get_the_title($nextpost->ID) . '</p><p>' . get_the_date($nextpost->ID) . '</p>
</div>';
} else {
echo '<div class="col-sm-6">TOP
</div>';
}
?>
</div>
</div>
<?php } ?>
Somebody knows any idea, please teach me.
get_the_date() accepts two arguments, a format string and a post.
You're passing the post ID in as the first argument instead of the second.
Correct usage:
get_the_date( '', $prevpost )
One other point to note is that I'm passing in the post object rather than the ID. You could pass in an ID however the function would then have to retrieve the post object anyway.
Documentation: https://codex.wordpress.org/Function_Reference/get_the_date
use get_post_meta() instead get_the_date($nextpost->ID).Read official documentation of this function from this link

How to add a "active" class to a carousel first element

I'm developing a News website in Wordpress, and I need to show in a bootstrap carousel the first three posts, my problem is that I need to add the "active" class only at the first of the three elements, but really don't know how to. Here's my code:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
I've already tried a answer found on this site (this one):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] . '</strong></a></div>';
$isFirst = false;
?>
but it just printed me the "active" words.
Thanks for your help
You need to set $i so that you can count how many times you have gone through the loop and do some logic with it, like in my example below. Instead of having two lines of code that are nearly identical like I have done below though, you should be able to do the if conditional right around the class active. I didn't do that so you could clearly see the conditional and the count of the loops through the array.
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
$i = 0;
foreach ($recent_posts as $recent) {
if ($i == 0) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
} else {
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
$i++;
}
?>

Why does my translation not work & how can I fix it?

I'm struggling with a translation in the footer navigation. the translation for the navigation works fine but I can't figure out how to set the translation for text_1 & text_2. I would appreciate any help a lot!
This is my PHP for the language:
<?php
class l
{
public static function lang($name)
{
$l = array();
// Footer
$l['text_1'] = "Hello";
$l['text_2'] = "World";
return $l[$name];
}
public static function __callStatic($name, $arguments)
{
return self::lang($name);
}
}
?>
And this is what I try to translate (text_1 & text_2):
<?php
class footer_nav
{
public function get_footer_nav()
{
$lang = language::getLang();
if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = 'home';
$ret = '
<a href="' . $lang . '/private" ';
if ($page == 'private')
$ret .= ('class="active"');
$ret .= '>' . l::confidence() . '</a>
<a href="' . $lang . '/rules" ';
if ($page == 'rules')
$ret .= ('class="active"');
$ret .= '>' . l::rules() . '</a>
<a href="' . $lang . '/reklama" ';
if ($page == 'reklama')
$ret .= ('class="active"');
$ret .= '>' . l::adv() . '</a>
<a href="' . $lang . '/contact" ';
if ($page == 'contact')
$ret .= ('class="active"');
$ret .= '>' . l::contacts() . '</a>
';
return $ret;
<div class="soc_box">
<p class="soc_box_txt">
' . l::text_1() . '<br />
' . l::text_2() . '
</p>
</div>
}
}
?>
You can take the divs out of the Php code like this:
?>
<div class="soc_box">
<p class="soc_box_txt">
' . l::text_1() . '<br />
' . l::text_2() . '
</p>
</div>
<?php
Just close php code with ?> add html code and start php code again with < ?php or:
echo '<div class="soc_box">';
echo '';
echo '';
echo '<p class="soc_box_txt">
\' . l::text_1() . \'<br />
\' . l::text_2() . \'
</p>';
echo ' </div>';

Categories