Read More and Read Less button with PHP - php

I'm trying to put a read more button and of course excerpt.
this is my base code to call text <?php the_content();?>
I add something for read more but it doesn't work.
<?php if($excerpt = $desc->the_content):?>
<?php
$num_word = 15;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt,15,'...') . '</p>';
echo '<p class="property-content">' . wp_trim_words($excerpt,25,'...') . '</p>';
?>
<?php endif;?>

Problem is solved.
<?php if ($excerpt = the_excerpt()):
$num_word = 5;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt, $num_word, '...') . '</p>';
?>
<?php endif; ?>

Related

Wordpress array null

I have a checkout system, within my Wordpress that shows me the following message:
Notice: Undefined index: answers in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
Notice: Trying to access array offset on value of type null in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
The php code of that file is the following:
<?php
$valorAccesorio = $args['quote']['answers']['vehicleGncValue'] ?? '0';
$cuotaMensual = $args['quote']['answers']['planPremioMensual'] +
(isset($args['quote']['answers']['apPremioMensual']) ? $args['quote']['answers']['apPremioMensual'] : 0);
?>
<header id="header">
<div class="wrap menu-wrap">
<?php
$class = (theme_get_custom_logo()) ? 'image' : 'text';
echo '<h1 class="site-title ' . $class . '">';
echo '<a href="' . esc_url(home_url('/')) . '" rel="home">';
if (theme_get_custom_logo()) echo '<img src="' . theme_get_custom_logo() . '" class="site-logo" alt="' . htmlspecialchars(get_bloginfo('name')) . '" />';
echo '<strong>' . get_bloginfo('name') . '</strong>';
echo '</a>';
echo '</h1>';
?>
<div class="right-section">
<?php if ($args['quote']['product'] == 'seguro-de-motos' || $args['quote']['product'] == 'seguro-de-autos-y-pick-ups') : ?>
<h1>
Estás contratando un plan
<span><?php echo $args['quote']['answers']['planCobertura'] ?></span>,
para tu
<span>
<?php echo $args['quote']['answers']['vehicleBrand'] ?>
<?php if (isset($args['quote']['answers']['vehicleModel'])) {
echo $args['quote']['answers']['vehicleModel'];
} else {
echo $args['quote']['answers']['vehicleVersion'];
} ?>
<?php echo $args['quote']['answers']['vehicleYear'] ?>
</span>
</h1>
<p>
Suma asegurada: $<?php echo number_format(($args['quote']['answers']['vehicleValue'] + $valorAccesorio), 2, ',', '.') ?>
|
Cuota mensual: $<?php echo number_format(($cuotaMensual), 2, ',', '.') ?>
</p>
<?php endif; ?>
</div>
From what I understand I am generating an array with null value, but I do not understand how to generate it by another way
Some help? Thanks!

How can I put a link around my excerpts in PHP?

I'm trying to put a link around my excerpts like on my titles but i'm getting a parse-error on this line:
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
Here the whole code of my post-element:
<div class="post-header">
<h3 class="title">
<?php the_title(); ?>
</h3>
<span class="meta-author"><?php the_author_posts_link(); ?> </span>
<span class="meta-category"> | <?php the_category(', '); ?> </span>
<span class="meta-comment-count"> | <a href="<?php comments_link(); ?>">
<?php comments_number( esc_html__( 'No Comments','salient'), esc_html__( 'One Comment','salient'), '% '. esc_html__( 'Comments','salient') ); ?></a>
</span>
</div>
<?php
$excerpt_length = ( !empty( $nectar_options['blog_excerpt_length'] ) ) ?
intval( $nectar_options['blog_excerpt_length'] ) : 30;
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
<div class="meta-tags"> <?php the_tags(''); ?> </div>
<div class="tags-divider"></div>
In PHP, text strings are merged using the dot character. So if you want to connect them together, you should do this:
$a = "text1";
$b = "text2";
echo ($a . $b); // prints "text1text2"
Or in your case like this:
echo "text1" . function() . "text3"; // prints text1text2text3
And if you are using function like string, you don't use the ";" character at the end, because it will end the whole line of code.
echo "text1" . "text2"; . "text3"; // wrong
echo "text1" . "text2" . "text3"; // correct
echo "text1" . function(); . "text3"; // wrong
echo "text1" . function() . "text3"; // correct
So, just add the dots and remove the semicolon, and it should work.
echo '<div class="excerpt">' . '' . nectar_excerpt($excerpt_length) . '</div>';?>
Give this a try?
echo '<div class="excerpt">' . nectar_excerpt($excerpt_length) . '</div>';

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</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

php passing on output to a variable

This might be easy, but i could not get solution for it as is novice and learning on the php
I want to put the output into a variable called $list so that it becomes
<?php $list .= '<h3> <?php echo $this->escape($item->n_make); ?> <?php echo $item->n_model; ?>
</h3>
<p> <?php echo $item->n_month; ?> <?php echo $item->n_year; ?> </p>
<p> <?php echo $item->n_short_description; ?> </p>
<hr/>'
?>
But the syntax is incorrect as its like php within php
How to get value of $list capturing in details
and can then use in explode function
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
---- conditions
echo $listings;
?>
It's just simple concatenation:
$list .= '<h3> ' . $this->escape($item->n_make) . $item->n_model . '</h3>
<p> ' . $item->n_month . ' ' . $item->n_year . '</p>
<p> ' . $item->n_short_description . ' </p>
<hr/>';

Categories