On a wordress site I'm building, for the first time, I'm trying to add custom loops.
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ):
$custom_loop->the_post();
echo '<li>' . get_the_title() . '
</li>';
endwhile;
wp_reset_query();
echo '</ul>';
endif;
?>
How do I correctly add this line if ( has_post_thumbnail() ) {
the_post_thumbnail();} inbetween the <li></li> tags?
I've tried just placing it in, but it shows up like normal text on the site.
If you want to put the_post_thumbnail() value between li tag, use below code instead of your echo '<li>.....';
echo '<li>' . get_the_title() . '';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
echo '</li>';
Try this:
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ($custom_loop->have_posts()) {
echo '<ul>';
while ($custom_loop->have_posts()) {
$custom_loop->the_post();
$thumb = (has_post_thumbnail()) ? the_post_thumbnail() : '';
printf('<li>%s%s</li>', get_permalink(), get_the_title(), $thumb);
}
wp_reset_query();
echo '</ul>';
}
?>
Related
I have a shortcode, which returns html created like this:
public function get_video_grid($the_query) {
$html = '';
if ( $the_query->have_posts() ) {
$count = 0;
while ( $the_query->have_posts()) : $the_query->the_post();
$attachment = get_attached_media( 'video' );
$post_ID = get_the_ID();
global $wpdb;
$video_url = wp_get_attachment_url($post_ID);
$video_attr = array(
'src' => $video_url
);
$item = $wpdb->get_row(
"
SELECT video_start_time
FROM " . $wpdb->prefix . "lc
WHERE attachment_id = $post_ID
",
ARRAY_A
);
$time = $item['video_start_time'];
$html .= '<div class="video-thumb-wrapper">';
$html .= '<h4 class="grid-title">' . get_the_title() . '</h4>';
$html .= '<span class="grid-timestamp">' . gmdate('d.m.Y H:i', strtotime($time)) . '</span>';
$html .= '<div class="aspect-ratio">';
$html .= wp_video_shortcode( $video_attr );
$html .= '</div>';
$html .= '</div>';
$count++;
endwhile;
}
return $html;
} // get_video_grid
However, if I am using the shortcode either in article or in do_shortcode(), the video elements are rendered outside of the containers on some pages. On some WP installations this works just fine. The version of WP does not seem to affect.
If I try to store the html string to variable like this:
<?php
/**
* Template Name: Modular Page
*/
get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post();
if ( post_password_required() ) {
echo get_the_password_form();
} else {
lobo_modular_content( $post );
}
endwhile;
$content = do_shortcode("[lilicast_list]");
get_footer(); ?>
the videos are still rendered. Notable here is that nothing should be echoed at this point, yet still I have the elements in my html. As far as I know, all I am asking is the wp to return me a string.
Why is the wp_video_shortcode() echoed before anything else? Why the behaviour is not consistent with just adding the shortcode inside of the post?
I have two loops running on my page one for getting one set of posts from a specific category and then further down getting posts from a custom post type but for some reason If I output both of them the second loop does not show and if I comment out the first the second one does show?
Im a bit confused as to why?
FIRST LOOP UPDATED
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
SECOND LOOP UPDATED
I didn't need one of the loops so I refined the code a bit further but still I am not having any luck with the second loop outputting.
<?php
$featureThumb = new WP_Query( array(
'post_type' => 'resources',
'meta_key' => 'file_upload',
'posts_per_page' => -1
));
while ($featureThumb->have_posts()) : $featureThumb->the_post();
echo '<div>';
if (has_post_thumbnail($post->ID)) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'homepage-thumb-thumb' );
echo '<img src="' . $thumb[0] . '" width="200" height="200" />' ;
};
echo '<p><a href="'. get_field('file_upload') .'" target="_blank" download>Click here to download as PDF</a></p>';
endwhile;
unset($featureThumb);
}
?>
It seems that you have three queries and 4 loops, not two.
Also, you have an extra } after the unset($featureThumb); that you should remove.
The three queries you posted in your question are:
$post_query, $loop and $featureThumb. At the last one your loop is incorect. It uses the object of the second one. Change at the third query in the loop, $loop with $featureThumb.
You need to use wp_reset_postdata() after the first two queries instead of wp_reset_query() as someone suggested and it should both work.
wp_reset_query() ensures that the main query has been reset to the original main query, while on the other hand wp_reset_postdata() makes sure that the global $post has been restored to the current post in the main query.
UPDATE:
If that does not work, if global $post object is not defined, try:
$post_query->reset_postdata();
$loop->reset_postdata();
UPDATE 2:
Your first query and loop should look like this:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
You have to use wp_reset_query(); at the end of the first loop as well. So the first loop should be:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
wp_reset_query();
}
?>
After scratching my head for almost 2 hours, I am unable to resolve the puzzle. Anyone who could help me , I will be highly thankful to him.
I have created a custom post type 'Properties' in wordpress. I am trying the following code to list the properties in a page. All of it is displaying fine except Images.
<?php
$conarg = array (
'post_type' => 'properties',
'order_by' => 'menu_order',
'order' => 'ASC'
);
$condo_query = new WP_Query( $conarg );
if ( $condo_query->have_posts() ) { ?>
<?php
while ( $condo_query->have_posts() ) {
$condo_query->the_post();
?>
<?php
$condo_images_max = 5;
$condo_items = array();
for($i=1; $i <= condo_images_max; $i++) {
//check if image exits
$img_attachment_id = get_field('gallery-image-'.$i);
if(!empty($img_attachment_id)) {
$condo_items[$i] = array(
'image' => wp_get_attachment_image_src($img_attachment_id, 'medium')
);
}
}
$condo_items_keys = array_keys($condo_items);
?>
<div class="box col-md-4 col-xs-12">
<div class="flexslider">
<ul class="slides">
<?php
for($i=0; $i < count($condo_items) ; $i++) {
$image = $condo_items[$condo_items_keys[$i]]['image'];
?>
<li>
<img src="<?php echo $image[0]; ?>" alt="Carousel Image" />
</li>
<?php
}
?>
</ul>
</div>
<?php echo '<h2 class="condo-title">' . get_the_title() . '</h2>';
echo'<ul class="condo-details">';
if(get_field('area_range')) {
echo '<li><span>SQ.FT RANGE:</span> ' . get_field('area_range') . '</li>';
}
if(get_field('room_view')) {
echo '<li><span>ROOM VIEW:</span> ' . get_field('room_view') . '</li>';
}
if(get_field('price_range')) {
echo '<li><span>PRICE RANGE:</span> ' . get_field('price_range') . '</li>';
}
?>
<a class="condo-btn" href="<?php the_permalink(); ?>"><?php _e('VIEW DETAILS'); ?></a>
<?php
echo'</ul></div>';
}
}
else {
echo'Sorry! No Condos are available at the moment!';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
As I mentioned in my comment it's probably worth switching to the PRO version and using a repeater field.
In your question you're overcomplicating this by unnecessarily building an array and then looping through your array using for instead of foreach.
Remove the code to build an array and try this instead of your current output code:
<ul class="slides">
<?php // Set maximum number of images to check.
$condo_images_max = 5;
for ( $i = 1; $i <= $condo_images_max; $i++ ) {
// Get image ID.
$image_id = get_field( 'gallery-image-' . $i );
// Check if image ID was found.
if ( $image_id ) {
// Output medium image in an li tag.
echo '<li>' . wp_get_attachment_image( $image_id, 'medium' ) . </li>';
}
} ?>
</ul>
If you need to be able to control the output of the image element and display it in the way you show in your question you can use:
...
if ( $image_id ) {
// Get image source.
$image = wp_get_attachment_image_src( $image_id, 'medium' );
// Check image source was found.
if ( $image ) {
echo '<li><img src="' . $image[0] . '" alt="Carousel Image" /></li>';
}
}
...
There is one issue in doing it this way to be aware of (and one of the reasons I suggested a repeater).
Let's say you have 6 image fields and a property is using all 6. The admin then decides to delete the first image so now the property has 5 images. The code will still work because we're checking an image was found before trying to output it but instead of showing all 5 images you'll only see 4 of them. Image #1 is empty and we've told it to stop looking after image #5.
I have recently installed Dynamic Featured Image plugin for wordpress. But I do not know how to link images. I'm trying to create me a gallery like this http://www.subcreative.com.au/#work - Scroll down to the projects and you will see .
I have put this code in functions.php
<?php
while ( have_posts() ) : the_post();
if( function_exists('dfi_get_featured_images') ) {
$featuredImages = dfi_get_featured_images();
//Now, loop through the image to display
}
endwhile;
?>
and used this to link the image.
echo ' <a class="fancybox" href="'. dfi_get_featured_images() .'" style="text-align:center">Take a look</a> '; ?>
But when I try to open the image, it becomes "/array"
Im not a wordpress dev but I've seen this on the wordpress website that I tried to fix.
so maybe you can try this one.
if( class_exists('Dynamic_Featured_Image') ):
global $dynamic_featured_image;
global $post;
$featured_images = $dynamic_featured_image->get_featured_images( $post->ID );
if ( $featured_images ):
?>
<?php foreach( $featured_images as $images ): ?>
<img src="<?php echo $images['full'] ?>" alt="">
<?php endforeach; ?>
<?php
endif;
endif;
this works in my case. I'm using DFI 3.1.13
This answer is only valid for plugin version 2.0.2 and below.
You need to loop throught the returned array and display the image manually. Try this:
<?php
if( function_exists('dfi_get_featured_images') ) {
$featuredImages = dfi_get_featured_images();
//Loop through the image to display your image
if( !is_null($featuredImages) ){
$links = array();
foreach($featuredImages as $images){
$thumb = $images['thumb'];
$fullImage = $images['full'];
$links[] = "<a href='{$fullImage}' class='dfiImageLink'><img src='{$thumb}' /></a>";
}
echo "<div class='dfiImages'>";
foreach($links as $link){
echo $link;
}
echo "</div>";
}
}
?>
try this inside of have posts loop
$img=dfi_get_featured_images();
$url=$img['full'];
echo ' <a class="fancybox" href="'. $full .'" style="text-align:center">Take a look</a> ';
If full doesn't work try thumb also.
This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?
When you use query_posts you have to call global $post to get the post_meta. If you're only calling one category why don't you just use an archive template?
Also if you're going to use query_posts make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..
global %post;
query_posts('cat=4');
// The Loop
//more stuff
endwhile;
wp_reset_query();
try replacing $post->ID with the_ID() in the inner pages. something like this
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));
$issetdate = get_post_meta(the_id(), 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';