<?php
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
}
?>
Hi, I would like to know how to put all my foreach loop inside a div ? I want each $recent_posts to be inside a div, right now they're one after the other. (btw it's my first post here, excuse me if i'm not clear)
##EDIT##
Here's my html :
<h2>Maquette UX/UI</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/vignette_maquette-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 1</p>
<h2>Intégration Baniera</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/vignette_baniera_homepage-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 2</p>
<h2>Intégration PHP</h2> <img width="150" height="150" src="http://localhost/wordpress/wp-content/uploads/2021/04/order_homepage-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image"
alt="" loading="lazy" />
<p class="excerpt">Projet 3</p>
<p class="excerpt"></p>
</div>
And I want every "group" to be inside a div, right now they're all together
Ok it works now, I wrapped the content of foreach like so :
<?php
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<div class="foreach_wrapper">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
echo '</div>';
}
?>
You can echo a div around the foreach, a little side note, you are using a <li> element but forgot the <ul> wrapper please validate your HTML with a HTML validator.
<?php
echo '<div>'; /* I added this */
$recent_posts = wp_get_recent_posts(array('post_type'=>'projet'));
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
}
if (has_excerpt($recent["ID"]) ){
// echo get_the_excerpt($recent["ID"],'the_excerpt');
echo '<p class="excerpt">' . get_the_excerpt($recent["ID"],'the_excerpt') . '</p>';
}
}
echo '</div>'; /* I added this */
?>
Related
I assembled a Wordpress shortcode but it throws an error in the block editor: "Updating failed. The response is not a valid JSON response." Notwithstanding, the edits are saved. I've been told the reason I get the error is my "shortcode handler function is generating output. Such functions must collect all output into a variable which is returned."
Below are (1) the code that works but causes the error message and (2) my pseudo code to fix the problem by assigning the 'a href' to a variable $html, but doesn't.
(1)
function make_header() {
$args = array(
'posts_per_page' => 1,
'category_name' => 'headlines',
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<a href="<?php the_permalink() ?>">
<div><img src="<?php echo $featured_img_url; ?>" width="100%" /></div>
<h2>
<?php the_title(); ?>
</h2></a>
<?php
}
wp_reset_postdata();
}
}
add_shortcode('make_header', 'make_header');
(2)
$html = '
<a href="<?php the_permalink() ?>">
<div><img src="<?php echo $featured_img_url; ?>" width="100%" /></div>
<h2>
<?php
the_title(); ?> </h2></a>';
}
wp_reset_postdata();
return $html;
Thanks for your help.
Try below code
$html = ' <div><img src="'. echo $featured_img_url .'" width="100%" /></div> <h2>'. the_title().' </h2>';
the concatenation operator (‘.‘), which returns the concatenation of its right and left arguments.
You could to use concatenation like so:
$html = '<a href="' . esc_url(get_the_permalink()) . '">';
$html .= '<div>';
$html .= '<img src="' . $featured_img_url . '" width="100%" />';
$html .= '</div>';
$html .= '<h2>' . get_the_title() . '</h2></a>';
Also, use get_the_permalink() and get_the_title() as these functions are returning their result instead of outputting it.
The full code would then look something like this:
function make_header() {
$html = '';
$args = array(
'posts_per_page' => 1,
'category_name' => 'headlines',
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$featured_img_url = esc_url(get_the_post_thumbnail_url(get_the_ID(),'full'));
$html = '<a href="' . esc_url(get_the_permalink()) . '">';
$html .= '<div>';
$html .= '<img src="' . $featured_img_url . '" width="100%" />';
$html .= '</div>';
$html .= '<h2>' . get_the_title() . '</h2></a>';
}
wp_reset_postdata();
}
return $html;
}
add_shortcode('make_header', 'make_header');
I'm trying to modify my custom wp theme and add related post block. I want to add default thumbnail for posts which doesn't have it. Below code is working fine but i can't archive how to add default img.
$args = array( 'numberposts' => '4','post__not_in' => array($post->ID));
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ) {
if($recent['post_status']=="publish") {
if ( has_post_thumbnail($recent["ID"])) {
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></div> ';
} else {
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></div>';
}
}
}
In order to print the default thumbnail if the posts featured image is not found you have to print the default image that you have in your images folder.
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-thumb-img.png"
alt="<?php the_title(); ?>" />
<?php } ?>
What the above code does?
It checks whether the post has thumbnails, if not it assigns the default-thumb-img.png ( Change it to your image name) as per your requirement.
my solution is just hardcode absolute link to default thumbnail
$args = array( 'numberposts' => '4','post__not_in' => array($post->ID));
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
if($recent['post_status']=="publish") {
if ( has_post_thumbnail($recent["ID"])) {
echo '<div class="col-md-3 col-lg-3"><div class="recent-post-holder"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></div></div> ';
} else {
echo '<div class="col-md-3 col-lg-3"><div class="recent-post-holder"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . "<img src='/*add link here*/'>". $recent["post_title"].'</a></div></div>';
}
}
}
I have a table with a row called lugar that have the value 1 or 2.
When I do foreach, i want to filter the elements that have the value 1 in the row lugar and the elements that have the value 2.
$descuento->lugar is the variable for the row lugar.
Actually, my code is:
<ul id="slider">
<?php foreach ( $results['descuentos'] as $descuento ) {
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
?>
<li>
<?php if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) ) { ?>
<img src="<?php echo $imagePath?>" alt="<?php echo htmlspecialchars( $descuento->title )?>" />
<?php } else {?> <img src="http://lorempixel.com/400/300/sports/" alt="<?php echo htmlspecialchars( $descuento->title )?>" /> <?php } ?>
<div>
<strong><span><?php echo htmlspecialchars( $descuento->title )?></span></strong>
<p><?php echo $descuento->content ?></p>
</div>
</li>
<?php
}
?>
</ul>
Then you could try this:
foreach ( $results['descuentos'] as $descuento )
{
if( $descuento->lugar == 1)
{
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
echo '<li>';
if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) )
{
echo '<img src="'. $imagePath. '" alt="'. $titulo. '" />';
}
else
{
echo '<img src="http://lorempixel.com/400/300/sports/" alt="'. $titulo. '" />';
}
echo '<div>'.
'<strong><span>'. $titulo. '</span></strong>'.
'<p>'. $descuento->content. '</p>'.
'</div>'.
'</li>';
}
}
So I am using Wordpress and I have to have a specific logo on a specific page. From research I have to use conditional logic to swap the existing logo with another depending on the current page. Everything I have tried seems to just break the theme.. Any help on guiding me in the correct direction? So basically every page except page_id=79 would have the same logo in the header.
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo'])) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
<?php if ( is_page(79) ) { ?>
What to displayed on page 79.
<?php } else { ?>
What will be displayed everywhere else.
<?php } ?>
This should work.
Try using get_queried_object_id();
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo']) && get_queried_object_id() != 79) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
The url of your logo image is contained within $options['logo']. You should be able to modify this in the admin section of your WordPress installation (try looking in "Appearance -> Header").
I have difficulties in understanding how to manage single and double quotes inside a JavaScript event inside a php block.
This is the code:
<?php
$mainImagePath = '';
$galleryImages = $this->getGalleryImages();
if (count($galleryImages) > 0) {
$gallery = '<div class="more-views">';
$gallery .= '<h2>' . $this->__('More Views') . '</h2>';
$gallery .= '<ul>';
foreach ($galleryImages as $_image) {
if ($_image->getFile() == $_product->getData('small_image')) {
$mainImagePath = $this->getGalleryUrl($_image);
}
$gallery .= '<li>'
. '<a href="' . $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()) . '" '
. 'rel="popupWin:\'' . $this->getGalleryUrl($_image) . '\', useZoom: \'cloudZoom\', smallImage: \'' . $this->getCloudImage($this->getProduct(), $_image) . '\'" class="cloud-zoom-gallery" title="' . $this->htmlEscape($_image->getLabel()) . '" onmouseover="$(\'image\').src = "'.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'"; return false;">'
. '<img src="' . $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56) . '" width="56" height="56" alt="' . $this->htmlEscape($_image->getLabel()) . '" />'
. '</a></li>';
}
$gallery .= '</ul></div>';
}
?>
The problem is the onmouseover event, which has a .src method that expects the value to be inside double quotes, but putting double quotes inside that string breaks the rest.
I already tried putting the needed value in a variable and echoing the variable, but that didn't work either.
How can I correctly escape quotes there?
onmouseover="$(\'image\').src = "'.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'"; return false;">
use single quote
onmouseover="$(\'image\').src = \''.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'\'; return false;">
I would recommended writing out the html / javascript first and then simply echo the php vars into their correct places. You will not have to do any escaping and it makes for more maintainable code.
Your IDE will also be able to correctly apply syntax highlighting
<?php
$mainImagePath = '';
$galleryImages = $this->getGalleryImages();
?>
<?php if(count($galleryImages) > 0) { ?>
<div class="more-views">
<h2><?php echo $this->__('More Views'); ?></h2>
<ul>
<?php foreach ($galleryImages as $_image) { ?>
<?php
if ($_image->getFile() == $_product->getData('small_image')) {
$mainImagePath = $this->getGalleryUrl($_image);
}
?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" rel="popupWin:'<?php echo $this->getGalleryUrl($_image); ?>', useZoom: 'cloudZoom', smallImage: '<?php echo $this->getCloudImage($this->getProduct(), $_image); ?>'" class="cloud-zoom-gallery" title="<?php echo $this->htmlEscape($_image->getLabel()); ?>" onmouseover="$('image').src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256); ?>"; return false;">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56) ;?>" width="56" height="56" alt="<?php echo $this->htmlEscape($_image->getLabel()); ?>">
</a>
</li>
<?php } ?>
</ul>
</div>