substring - add ... in the end if text is cut - php

I have a code on wordpress website (front-page.php), it's cut text and add "..." on title if text is over 35 characters.
Cut is ok but the points is always visible. Is it possible to add only if text is cut?
Thank you!
<div class="bxpd">
<h2><span><?= substr($post_title,0,35); ?></span>
<a href="<?= get_post_permalink($post_id); ?>">
<?= substr($postData->post_title, 0, 35)."..."; ?>
</a>
</h2>
<p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150))."..."; ?> </p>
</div>

<div class="bxpd">
<h2><span><?= substr($post_title,0,35); ?></span>
<a href="<?= get_post_permalink($post_id); ?>">
<?=
substr($postData->post_title, 0, 35). (strlen($postData->post_title) > 35 ? "..." : "");
?>
</a>
</h2>
<p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150))."..."; ?> </p>

You can adjust your code and put ... only if they are needed:
<div class="bxpd">
<h2><span><?= substr($post_title,0,35); ?></span>
<a href="<?= get_post_permalink($post_id); ?>">
<?= substr($postData->post_title, 0, 35)."..."; ?>
</a>
</h2>
<p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150)).
(strlen($postData->post_content)>150?"...":"");
?> </p>
</div>

Related

How to recover the users I published randomly when I click on my profile to display them again when I return php

Hello I posted the users randomly on my site but when I click on a user to see his profile I would like when I return I fall back on the same users. PLEASE HELP ME
This is the code I typed for displaying users and now I would like to repost all the users I've already posted randomly
// This query will be used to display all users randomly
$listsAllUsers = $db->prepare("SELECT users1.id, username, sex, country, years, city,
regionOuProvince, titleOuSlogan, about,
orientation, looking_for, civil_status, img_original FROM users1
LEFT JOIN users2 ON users1.id = users2.user_id
LEFT JOIN pictures ON users1.id = pictures.user_id
WHERE pictures.active_profile = ?
ORDER BY RAND() $limit");
$listsAllUsers->execute(['2']);
$listAllUsers = $listsAllUsers->fetchAll(PDO::FETCH_OBJ);
<?php if ($data->display_user_fetch == '1'): ?>
<div class="container">
<ul class="usersFetch">
<center>
<?php if (count($listAllUsers) != 0): ?>
<?php foreach ($listAllUsers as $listAllUser): ?>
<li style="text-align:center;">
<div class="userFetchBloc">
<a href="<?= WEBSITE_NAME_URL ?>/profile/<?= strtolower($listAllUser->username) ?>">
<img src="/<?= $listAllUser->img_original ?>" class="img_user_fetch_bloc" style="width: 100%;" alt="Meeting <?= $listAllUser->username ?> On <?= WEBSITE_NAME ?>">
</a>
<div class="fetchInfosUserStandardProfile">
<span class="fetchInfosUserText">
<span class="infosUserText2"><?= $listAllUser->username ?>, </span><?= date('Y') - $listAllUser->years ?> old <br>
<?= $listAllUser->city ?><span class="infosUserText">, <?= $listAllUser->regionOuProvince ?></span>
<span class="infosUserText2"><br> <?= NbrePictures($listAllUser->id) ?> photo<?= NbrePictures($listAllUser->id) > 1 ? 's' : ''; ?> </span>
</span>
</div>
</div>
<div class="all_necessary_infos">
<div>
<b><?= $listAllUser->titleOuSlogan ?></b>
</div>
<em><?= $listAllUser->civil_status ?> <?= $listAllUser->sex ?> of <?= date('Y') - $listAllUser->years ?> years looking for <?= verify_looking_for($listAllUser->id, $listAllUser->sex) ?> for <?= $listAllUser->orientation ?></em>
<div class="wideDiv">
<?= $listAllUser->about ?>
</div>
<strong>Meet</strong> <a style="color: #be0210;" href="<?= WEBSITE_NAME_URL ?>/meet-<?= lcfirst($listAllUser->sex) ?>-<?= suppr_accents($listAllUser->city) ?>" style="color:#be0210;"><?= $listAllUser->city ?></a>,
<a style="color: #be0210;" href="<?= WEBSITE_NAME_URL ?>/meet-<?= lcfirst($listAllUser->sex) ?>-<?= suppr_accents($listAllUser->regionOuProvince) ?>" style="color:#be0210;"><?= $listAllUser->regionOuProvince ?></a>,
<a style="color: #be0210;" href="<?= WEBSITE_NAME_URL ?>/meet-<?= lcfirst($listAllUser->sex) ?>-<?= lcfirst($listAllUser->country) ?>" style="color:#be0210;"><?= $listAllUser->country ?></a>
</div>
</li>
<?php endforeach ?>
<?php else: ?>
<p style="text-align: center;">No users on <?= WEBSITE_NAME ?></p>
<?php endif ?>
</center>
</ul>
<?php if (count($listAllUsers) != 0): ?>
<div id="pagination" style="position: relative; top: 10px; bottom: 5px;"><?= $pagination ?></div>
<?php endif ?>
</div>
<?php else: ?>
<?php if (count($listAllUsers) != 0): ?>
<?php foreach ($listAllUsers as $key => $listAllUser): ?>
<div id="user_container_testimony">
<div class="userTestimony">
<span class="img_message_testimony">
<a href="<?= WEBSITE_NAME_URL ?>/profile/<?= strtolower($listAllUser->username) ?>">
<img src="/<?= $listAllUser->img_original ?>" class="img_testimony" alt="This image shows <?= $listAllUser->username ?> meeting on <?= WEBSITE_NAME ?>.">
</a>
</span>
<div class="fetchInfosUserStandardProfileStyle">
<span class="fetchInfosUserText">
<span class="infosUserText2"><?= $listAllUser->username ?>, </span><?= date('Y') - $listAllUser->years ?> old <br>
<?= $listAllUser->city ?><span class="infosUserText">, <?= $listAllUser->regionOuProvince ?></span>
<span class="infosUserText2"><br> <?= NbrePictures($listAllUser->id) ?> photo<?= (NbrePictures($listAllUser->id) > 1) ? 's' : '' ?> </span>
</span>
</div>
<div class="allTestimony">
<div class="titleTestimony">
<strong><u><?= $listAllUser->titleOuSlogan ?></u></strong>
</div>
<div class="subtitleTestimony">
<span class="describeSubtitleTestimony">
<u><?= $listAllUser->username ?></u> <?= $listAllUser->civil_status ?> <?= $listAllUser->sex ?> of <?= date('Y') - $listAllUser->years ?> years looking for <?= verify_looking_for($listAllUser->id, $listAllUser->sex) ?> for <?= $listAllUser->orientation ?>
</span>
</div>
<div class="testimony">
<span class="describeTestimony">
<?= mb_strlen($listAllUser->about) > 350 ? substr($listAllUser->about, 0, 350).' More Read...' : $listAllUser->about ?>
</span>
</div>
</div>
<div class="meetCityRegion">
<strong>Meet</strong> <u style="color: #be0210;"><?= $listAllUser->city ?></u>,
<u style="color: #be0210;"><?= $listAllUser->regionOuProvince ?></u>,
<u style="color: #be0210;"><?= $listAllUser->country ?></u>
</div>
</div>
</div>
<?php endforeach ?>
<div id="pagination" style="position: relative; top: 10px; bottom: 5px;"><?= $pagination ?></div>
<?php else: ?>
<p style="text-align: center;">You have not traded anything with this user yet.</p>
<?php endif ?>
<?php endif ?>
I would like you to help me take the users I've already posted randomly
Currently here is no simple way to "lock" the same users set when you use RAND() statement for ORDER because RAND here generate narnodm order every you call SQL query ( = on every page load).
You can use 3 ways:
Generate random keys to separate column in table and order by it. That can crate random order, bud it fixed during time – it make repeatable views to list.
Select random list of users as currently and cache that list somewhere and load it later when you back to that page.
Maybe you car help allo browser cache page with list of user with sending right HTTP header in request.
Question is why you want to sort it randomly?

I want to apply css class as per below condition

I want to apply css class as per below condition but after applying this code each content is repeated by 8 times and if i complete for loop before div then css classes are not getting applied.
<?php $size=count($Spacategories);
for($i=0;$i<8;$i++){
if($i==0 || $i==5){ ?>
<li class="valign">
<?php } elseif($i==1 || $i==7) { ?>
<li class="lalign">
<?php } elseif($i==2 || $i==6) { ?>
<li class="talign">
<?php } else { ?>
<li class="ralign">
<?php } ?>
<div class="image">
<img src="<?php echo $spacategory['thumb']; ?>" title="<?php echo $spacategory['name']; ?>" alt="<?php echo $spacategory['name']; ?>" />
</div>
<div class="text">
<h3><?php echo $spacategory['name']; ?></h3>
<p><?php echo substr($spacategory['description'],0,150); ?></p>
<?php echo $text_view_more; ?>
</div>
</li>
<?php } } ?>
Change all instances of
$spacategory
to
$Spacategories[$i]
This will fix it.
Also, I don't see what's populating $text_view_more. You should probably change that to some data point from $Spacategories?
Finally, it seems you need to be looping based on the number of categories, so change
for($i=0;$i<8;$i++){
to
for($i=0;$i<$size;$i++){
In your code, $spacategory is not an item from $spacategories with the index $i in each iteration. You should have started the for:
$spacategory = $spacategories[$i];
or use a foreach instead:
<?php
$classesIndexMap = array(
0 => 'valign',
1 => 'lalign',
2 => 'talign',
5 => 'valign',
6 => 'talign',
7 => 'lalign',
);
$html_view_more = htmlspecialchars($text_view_more, ENT_NOQUOTES, 'UTF-8');
?>
<?php foreach($spacategories as $i => $spacategory) : ?>
<li class="<?= isset($spacategory[$i]) ? $spacategory[$i] : 'ralign' ?>">
<div class="image">
<img src="<?= htmlspecialchars($spacategory['thumb'], ENT_QUOTES, 'UTF-8') ?>"
title="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>"
alt="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>" />
</div>
<div class="text">
<h3><?= htmlspecialchars($spacategory['name'], ENT_NOQUOTES, 'UTF-8') ?></h3>
<p><?= substr($spacategory['description'],0,150); ?></p>
<a href="<?= htmlspecialchars($spacategory['href'], ENT_QUOTES, 'UTF-8'); ?>"
class="learmmore"><?= $html_view_more ?></a>
</div>
</li>
<?php endforeach; ?>

Use of undefined constant

Hi I have this error in my website
my website is wordpress 4
i use post format
in post format i create this php to if page has quote post then dont load title and image and etc from quote post just show quote content is meta box (wordpress)
Notice: Use of undefined constant quote - assumed 'quote' in C:\xampp\htdocs\personal\wp-content\themes\personal\warp\systems\wordpress\layouts\_post.php on line 4
and
Notice: Use of undefined constant quote - assumed 'quote' in C:\xampp\htdocs\personal\wp-content\themes\personal\warp\systems\wordpress\layouts\_post.php on line 14
this is _post.php code in bellow
<?php $format = get_post_format(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('group'); ?>>
<?php
if ($format != quote ) {
$css_class = "b-box";
} else {
$css_class = "q-box";
}
echo "<div class=\"$css_class\">";
?>
<?php get_template_part('inc/post-formats'); ?>
<?php if ($format != quote ) : ?>
<div class="post-content">
<div class="blog-img-box">
<img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" />
</div>
<div class="post-deco blog-icon-box">
<div class="hex hex-small">
<i class="fa blog-icon"></i>
<?php if ( $format != false ) :?><?php endif; ?>
</div>
</div><!--/.post-deco-->
<h2 class="blog-title">
<?php the_title(); ?>
</h2><!--/.post-title-->
<div class="blog-details row">
<div class="col-md-4"><i class="fa fa-calendar blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php the_time('j M, Y'); ?></p>
</div>
<div class="col-md-4">
<a href="<?php comments_link(); ?>">
<i class="fa fa-comment blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php comments_number( '0', '1', '%' ); ?> Comments</p></a>
</div>
<div class="col-md-4"><i class="fa fa-eye blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php echo getPostViews(get_the_ID()); ?></p>
</div>
<div class="likes"><i class="fa fa-heart blog-details-icons"></i>
<p class="blog-details-icons-txt">18</p>
</div>
</div>
<div class="blog-txt">
<?php the_excerpt(); ?>
</div>
<div class="readmore-box"><p class="readmore-txt"><?php _e('Read More','anew'); ?></p></div>
</div><!--/.post-content-->
<?php endif ?>
</div><!--/.post-inner-->
</article><!--/.post-->
please check and say to me what i wrong
thank you !
If you want to use the quote as a literal string (line 4), not a variable name, you must enclose it into single or double quote characters (' or "). Otherwise, PHP thinks that you're referencing a PHP constant.
For reasons of backward compatibility, PHP falls back on using the string as literal, but it does throw an E_NOTICE error – the one you're experiencing.
verify that all variables have this sign $
correct variable $test
incorrect variable test

Multilingual slideshow

I have this code for a slideshow in my Drupal 7, Nexus 7.x-1.3 theme.
My website is multilingual and I need to use a different image for every language, eg. I would like to change slide-image-1.jpg (en language) with other image-name for (gr language).
My code:
<?php if ($is_front): ?>
<?php if (theme_get_setting('slideshow_display','nexus')): ?>
<?php
$slide1_head = check_plain(theme_get_setting('slide1_head','nexus')); $slide1_desc = check_markup(theme_get_setting('slide1_desc','nexus'), 'full_html'); $slide1_url = check_plain(theme_get_setting('slide1_url','nexus'));
$slide2_head = check_plain(theme_get_setting('slide2_head','nexus')); $slide2_desc = check_markup(theme_get_setting('slide2_desc','nexus'), 'full_html'); $slide2_url = check_plain(theme_get_setting('slide2_url','nexus'));
$slide3_head = check_plain(theme_get_setting('slide3_head','nexus')); $slide3_desc = check_markup(theme_get_setting('slide3_desc','nexus'), 'full_html'); $slide3_url = check_plain(theme_get_setting('slide3_url','nexus'));
?>
<div id="slidebox" class="flexslider">
<ul class="slides">
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-1.jpg'; ?>"/>
<?php if($slide1_head || $slide1_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide1_head; ?></h2><?php print $slide1_desc; ?>
<a class="frmore" href="<?php print url($slide1_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-2.jpg'; ?>"/>
<?php if($slide2_head || $slide2_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide2_head; ?></h2><?php print $slide2_desc; ?>
<a class="frmore" href="<?php print url($slide2_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-3.jpg'; ?>"/>
<?php if($slide3_head || $slide3_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide3_head; ?></h2><?php print $slide3_desc; ?>
<a class="frmore" href="<?php print url($slide3_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
</ul><!-- /slides -->
<div class="doverlay"></div>
</div>
<?php endif; ?>
<?php endif; ?>
Is there any solution for that? Thank you.
There is a Drupal connector from Smartling that may be of help. Here is a video showing how the connector works.
Many sites running on Smartling do language/country-specific image replacement, which is explained here.
Hope this helps,
Nataly
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<SCRIPT>
function english(){
$(#image).attr('src','http://link.to.your/english/image.png')
}
function greek(){
$(#image).attr('src','http://link.to.your/greek/image.png')
}
</SCRIPT>
<IMG id="image" src="http://link.to.your/greek/image.png">
Change to English<BR>
Change to Greek
Finally I fixed it in page.tpl with php code. One if/else on your page language to set you picture path.
Regards, HashKey

substr not working with $item->getTitle

I'm making a website with Joomla and am using RokSprocket to display some news. Now, some Titles are too long, so I wanted to shorten it after a certain character number, but it's not working. Am a total php noob, my apologies.
Here's the whole code:
<li data-lists-item>
<h4 class="sprocket-lists-title ">
<?php if ($item->custom_can_have_link): ?><a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>"><?php endif; ?>
<?php if(strlen($item->getTitle)>10)
echo substr($item->getTitle,0,10) . ' ...';
else
echo $item->getTitle();?></a>
<div class="date"><?php echo date('d.M. Y', strtotime($item->getDate()));?></div>
</h4>
<span class="sprocket-lists-item" data-lists-content>
<span class="sprocket-padding">
<?php if ($item->getPrimaryImage()) :?>
<img src="<?php echo $item->getPrimaryImage()->getSource(); ?>" class="sprocket-lists-image" />
<?php endif; ?>
<?php echo $item->getText(); ?>
<?php if ($item->getPrimaryLink()) : ?>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
</span>
</span>
</li>
I'm hoping it's something very obvious and I'm just being stupid. Thanks a bunch already!
$item->getTitle is probably a method not a property, so try:
$item->getTitle(); // <-- parenthesis
Change to:
<?php if(strlen($item->getTitle())>10)
echo substr($item->getTitle(),0,10) . ' ...';

Categories