get_the_date with using get_adjacent_post wordpress - php

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

Related

How to make the first accordion tab open and the rest closed in wordpress

I am having an issue in incrementing the class="accordion-collapse collapse show so that the first accordion tab should open and the closed. This is how I have doneclass="accordion-collapse collapse '.if($i++){.'show'.}.'" . I am do it in a while loop.
if ( $the_query->have_posts() ) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading'.get_the_ID().'">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse'.get_the_ID().'" aria-expanded="true" aria-controls="panelsStayOpen-collapse'.get_the_ID().'">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse'.get_the_ID().'" class="accordion-collapse collapse '.($i++.'show'.).'" aria-labelledby="panelsStayOpen-heading'.get_the_ID().'">
<div class="accordion-body">
'
.the_content() . '
</div>
</div>';
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
I am expecting the first according to open and the rest closed
Try this :-
EDITED.
<?php
$flag = true;
if ($the_query->have_posts()) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ($the_query->have_posts()) {
$the_query->the_post();
$show_class = $flag == true ? 'show' : '';
$collapsed = $flag == true ? '' : 'collapsed';
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading' . get_the_ID() . '">
<button class="accordion-button '.$collapsed.'" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse' . get_the_ID() . '" aria-expanded="true" aria-controls="panelsStayOpen-collapse' . get_the_ID() . '">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse' . get_the_ID() . '" class="accordion-collapse collapse ' . $show_class . ' " aria-labelledby="panelsStayOpen-heading' . get_the_ID() . '">
<div class="accordion-body">
'
. the_content() . '
</div>
</div>';
$flag = false;
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}

jQuery Popup not working with output from PHP

I'm looking to correct this script form popup each image with is corresponding texts. Actually it popup with all text in MySQL. Why isn't it showing the respective text for each image?
<?php
while ( $alaune = mysqli_fetch_assoc( $resultat6 ) ) {
if ( ! empty( $alaune ) ) {
echo '<div class="single_iteam"><img alt="" src="changements/une/images/' . $alaune["alaunePic"] . '" class="alaunePic">';
echo '<div class="slider_article">';
echo '<h2><a class="slider_tittle" href="#">' . $alaune["alauneTitre"] . '</a></h2>';
echo '<p class="alaunetexte truncate">' . $alaune["alauneTexte"] . '</p>';
?>
Lire la suite...
<?php echo '</div>';
echo '</div>';
}
}
echo '</div><div class="modal" id="modal-name"><div class="modal-sandbox"></div><div class="modal-box"><div class="modal-header"><div class="close-modal">✖</div>';
foreach ( $resultat6 as $alaune ) {
echo '<h1>' . $alaune["alauneTitre"] . '</h1><p>' . $alaune["alauneTexte"] . '</p>';
}
echo '</div>';
echo '<div class="modal-body"><br /><button class="close-modal"> Fermer </button></div></div></div>';
?>
Please have a look. Best regards.
Your second loop cannot iterate through the mysqli_result object. You need either to iterate again through results using mysqli_fetch_assoc or a better solution is to temporary store output of all modals in the array during while iteration and then after while loop, output it. Also, use integer variable to increment it during while loop and assign it as a suffix to modal id, that would differentiate modal for each record:
<?php
$modals = [];
$key = 1;
while ( $alaune = mysqli_fetch_assoc( $resultat6 ) ) {
if ( ! empty( $alaune ) ) {
echo '<div class="single_iteam"><img alt="" src="changements/une/images/' . $alaune["alaunePic"] . '" class="alaunePic">';
echo '<div class="slider_article">';
echo '<h2><a class="slider_tittle" href="#">' . $alaune["alauneTitre"] . '</a></h2>';
echo '<p class="alaunetexte truncate">' . $alaune["alauneTexte"] . '</p>';
?>
Lire la suite...
<?php echo '</div>';
echo '</div>';
$tempModal = '<div class="modal" id="modal-name-' . $key . '"><div class="modal-sandbox"></div><div class="modal-box"><div class="modal-header"><div class="close-modal">✖</div>' .
'<h1>' . $alaune["alauneTitre"] . '</h1><p>' . $alaune["alauneTexte"] . '</p></div>' .
'<div class="modal-body"><br /><button class="close-modal"> Fermer </button></div></div></div>';
$modals[] = $tempModal;
$key++;
}
}
echo '</div>';
foreach($modals as $modal){
echo $modal;
}
?>

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>';

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>

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

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!

Categories