How can I put a link around my excerpts in PHP? - 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>';

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!

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

Combine get_posts loop with another array and randomise sort by date

I have a page I am building in WordPress which includes tweets by the Twitter API and then a standard get_posts loop.
At the moment they are displaying one after the other but I wish to create a masonry style grid which includes both.
Is there a way to output both arrays in between each other by date so the items are broken up instead of it outputting tweets first then the posts?
Here is my code.
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo '<div class="funny-grid">';
echo '<div class="grid-sizer"></div>';
foreach($string as $items)
{
$string = $items['text'];
$regex = "#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#";
echo '<div class="grid-item tweet ">';
echo '<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>';
echo '<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' ."<br />";
echo '<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>';
echo '<div class="tweet-date">' . $items['created_at'] . '</div>' . "<br />";
echo '</div>';
}
//counts the amount of items in the array.
/*echo "Number of items:" . count($string);*/
?>
<!--<div style="clear: both;"></div>-->
<?php
query_posts('post_type=funny_wall &posts_per_page=8');
if(have_posts()):while(have_Posts()):the_post();
$img = wp_get_attachment_url(get_post_thumbnail_id($post->ID), "full");?>
<div class="grid-item image">
<div class="as">
<img src="<?php echo $img ; ?>">
<a href="<?php the_permalink();?>">
</a>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
</div>
</div>
Instead of echoing the results directly, you could put them into an array, and then use shuffle() to randomise before outputting it in one go.
e.g.:
tweets
$grid_items[] = '<div class="grid-item tweet ">
<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>
<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' .'<br />
<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>
<div class="tweet-date">' . $items['created_at'] . '</div>' . '<br />
</div>';
posts:
$grid_items[] = "<div class='grid-item image'>
<div class='as'>
<img src=".$img.">
<a href=".the_permalink().">
</a>
</div>
</div>";
then use:
shuffle($grid_items);
foreach($grid_items as $grid_item){
echo $grid_item;
}
when you want to output them.
http://php.net/manual/en/function.shuffle.php

Removing "echo" and replacing it with "return"?

I have the following code in place, however, I have to strip away all the styling (any css) and have it return rather than echo, otherwise it messes up with some other code I'm using.
The other code I'm using echo's a shortcode that outputs the following determined by if there is items in the cart or not.
Obviously I can't echo (on the echo shortcode end of things), so I must return. If the cart is empty, it returns just fine as per my commented code below. It is when the cart has items, I can't get it to return :( my attempt is below.
//Original code, if there's items
if(is_object($cart) && $cart->countItems()) {
?>
<div id="Cart66scCartContents" style="float:right; text-align: right;">
<a id="Cart66scCartLink" href='<?php echo get_permalink($cartPage->ID) ?>'>
<span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
<span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? ' items' : ' item' ?></span>
<span id="Cart66scCartCountDash">–</span>
<!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL .
number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
</span></a>
<a id="Cart66scViewCart" href='<?php echo get_permalink($cartPage->ID) ?>'>View Cart</a>
<span id="Cart66scLinkSeparator"> | </span>
<a id="Cart66scCheckout" href='<?php echo get_permalink($checkoutPage->ID) ?>'>Check out</a>
</div>
<?php
}
else {
//My code, if there's no items (which works perfectly as a return)
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}
My attempt on setting the "if there's items in the cart" to a return rather than echo..
if(is_object($cart) && $cart->countItems()) {
return "<a href='" . get_permalink($cartPage->ID) . "'>" . $cart->countItems(); . " " . $cart->countItems() > 1 ? ' items' : ' item' . "–" . number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); . "</a> <a href='" . get_permalink($cartPage->ID) . "'>View Cart</a> | <a href='" . get_permalink($checkoutPage->ID) . "'>Check out</a>";
}
else {
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}
The text "1 item – View Cart | Check out" is not showing up with my attempt.
What have I done wrong?
Thank you!!
You have a semicolon at . $cart->countItems();, remove that and you should be good to go
Try to use HEREDOC
For example:
if (is_object($cart) && $cart->countItems()) {
$items = $cart->countItems() > 1 ? ' items' : ' item';
return <<<HTML
<div id="Cart66scCartContents" style="float:right; text-align: right;">
<a id="Cart66scCartLink" href='{get_permalink($cartPage->ID)}'>
<span id="Cart66scCartCount">{$cart->countItems()}</span>
<span id="Cart66scCartCountText">{$items}</span>
<span id="Cart66scCartCountDash">–</span>
</span></a>
<a id="Cart66scViewCart" href='{get_permalink($cartPage->ID)}'>View Cart</a>
<span id="Cart66scLinkSeparator"> | </span>
<a id="Cart66scCheckout" href='{get_permalink($checkoutPage->ID)}'>Check out</a>
</div>
HTML;
}
else {
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}
Maybe
$html = <<<EOD
<tr>
<td>TEST</td>
</tr>
EOD;
Would help here?
Declare the string variable and concatenate all the HTML you trying to echo in that string and return the string variable .
Example $string = '

Categories