I'm having trouble getting images shown with a repeater in Wordpress.
For some reason it only shows the alt text instead of the image itself?
example
the upper images are my thumbnail posts and are showing correctly.
Does anyone know whats causing this?
my code
<div class="row">
<div class="col-sm-6 left"><h1>COMMERCIAL</h1><br>
<div class="page-header">
<?php $mymovies = new WP_Query(array(
'post_type' => 'my_movies'
)); ?>
<?php while($mymovies->have_posts()) : $mymovies->the_post(); ?>
<div class="movie-colums">
<div class="thumbtitle group">
<div class="preview">
<?php the_post_thumbnail('thumbnail'); ?>
<h1><?php the_title(); ?><br>
<?php the_field('year'); ?>
<?php
// check if the repeater field has rows of data
if( have_rows('images_rep') ):
// loop through the rows of data
while ( have_rows('images_rep') ) : the_row();
// display a sub field value
the_sub_field('image');
endwhile;
else :
// no rows found
endif;
?>
</h1>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
thanks in advance
Your PHP bit for the repeater stuff should be:
<?php if( have_rows('images_rep') ): ?>
<?php while( have_rows('images_rep') ): the_row();
$image = get_sub_field('image');
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>">
<?php endwhile; ?>
<?php endif; ?>
It looks like your example is outputting the image object so just wrap the image URL in an image tag.
Ref: https://www.advancedcustomfields.com/resources/image/
Related
I am using Advanced Custom Fields plugin in Wordpress and have managed (with very limited PHP knowledge) to use their repeater fields to create an accordion.
Everything is working really well except the image field (company_logo). So long as the user selects an image for this custom field it displays fine but if they do not select an image I get some strange text instead.
Using my current code I'm trying to add in an 'if' statement so if they do not select an image it displays a default image instead. I've stride lots of variations but cannot get it to work.
Can anyone help/point me in the right direction please? Also, if theres a way I can clean this up as I seem to use a lot of
<div id="accordion">
<?php if( have_rows('exhibitor') ): ?>
<?php while( have_rows('exhibitor') ): the_row(); ?>
<h4 class="accordion-toggle"><?php the_sub_field('exhibitor_type'); ?></h4>
<div class="accordion-content">
<?php while( have_rows('table') ): the_row(); ?>
<div class="exhibitor-single">
<p class="table-field">Table <?php the_sub_field('table_no'); ?></p>
<p><?php the_sub_field('company_name'); ?></p>
<?php $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<p><?php the_sub_field('company_website'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
You should be able to just wrap the image 'section' in this:
<?php
if(get_sub_field('company_logo')) :
$image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo');
?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<?php
else :
?>
<!-- Your default image here -->
<img src="" alt="" />
<?php
endif; ?>
I am pretty new in WordPress development and I am trying to implement this custom theme that handle the so called featured posts: http://lnx.asper-eritrea.com/
As you can see in the posts area of the homepage I have the Articoli in evidenza sub area that contains my featured posts and under it the Ultimi Articoli subarea that contains the latest posts.
To implment this I use the posts tag and in the futured posts area I show the posts having the tag=featured condition.
So this is my code:
<section id="blog-posts">
<header class="header-sezione">
<h2>Articoli in evidenza</h2>
</header>
<?php query_posts('tag=featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<header class="header-sezione">
<h2>Ultimi Articoli</h2>
</header>
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
</section>
As you can see first I show the posts having a tag featured by the use of query-posts() function:
<?php query_posts('tag=featured');?>
Now my problem is that if a post have the featured tag I don't want that it is shown in the latest post area (at this time it is shown). So I tried to use this code:
<header class="header-sezione">
<h2>Ultimi Articoli NOT FEATURED</h2>
</header>
<?php query_posts('tag != featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
But don't work and the featured post still shown in the homepage. As you can se I tried so specify that, to be shown, a post can't have the featured tag:
<?php query_posts('tag != featured');?>
Why don't work? What am I missing? Can you help me to fix this issue?
Tnx
To return posts that do not contain a particular tag, you should use pass the ID of the term into the tag__not_in argument.
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id ) );
is_front_page()
You should try <?php is_front_page(); ?> or vice versa.
http://codex.wordpress.org/Function_Reference/is_front_page
<?php if (is_front_page()) { ?>
<!-- Do something -->
<?php } else { ?>
<!-- Add else only if you need it! -->
<?php } ?>
I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks
I am working on a WordPress project, using the Advanced Custom Fields plugin.
However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.
<?php get_header(); ?>
<?php
// If the loop has posts
if (have_posts()) :
// For each post this page has
while (have_posts()) : the_post();
// Display the Flexible Content
while(has_sub_field('add_row')):
// If the content type is Tagline
if(get_row_layout() == 'tagline'): ?>
<div class="row paddingBottom paddingTop">
<div class="col-xs-12">
<h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
</div>
</div>
<?php
// If the content type is a Featured Image
/*elseif ( get_row_layout() == 'featured_image' ): ?>
<div class="col-xs-12 textCenter">
<?php
$attachment_id = get_sub_field('featured_image');
$size = "full-width"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</div>
<?php*/
// If the content type is a horizontal rule
elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
<div class="row">
<div class="col-xs-12">
<hr />
</div>
</div>
<?php
// If the content type is Columns
elseif ( get_row_layout() == 'columns' ):
// If there is at least one column section
if(get_sub_field('column')):
// For each column section
while(has_sub_field('column')): ?>
<div class="row paddingTop paddingBottom">
<?php
if(get_sub_field('column_content_type')):
$count_rows = 0;
// Counting
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
// Loop
while(has_sub_field('column_content_type')): ?>
<div class="
<?php /*
if ( $count_rows == 1 ) :
echo "col-xs-12";
elseif ( $count_rows == 2 ) :
echo "col-xs-6";
elseif ( $count_rows == 3 ) :
echo "col-xs-6 col-sm-4";
elseif ( $count_rows == 4 ) :
echo "col-xs-6 col-sm-6 col-md-4";
endif; */
?>
"> <?php
// Title
if ( get_sub_field('title') ) : ?>
<h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
endif;
// Text Area
if ( get_sub_field('text') ) :
the_sub_field('text');
endif;
// Link
if ( get_sub_field('link') ) : ?>
<?php // eventually need to make it so you can change the link text ?>
<a class="textCenter" href="<?php the_sub_field('link'); ?>">
More info
</a> <?php
endif; ?>
</div> <?php
endwhile;
endif;
?>
</div>
<?php
endwhile; // end for each column section
endif; // end checking for at least one column section
endif; // end checking content type is columns
endwhile; // end flexible content
endwhile; // end for each post
endif; // end checking for at least one post
?>
<?php get_footer(); ?>
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.
I think the mistake is
// Display the Flexible Content
while(has_sub_field('add_row')):
i think you need to use if(){
// Code
}
instead of "while"
I'm displaying a custom field like this:
<div class="blog_text">
<img src="<?php the_field('imagen_prensa'); ?>" />
</div>
I would like to make the img appear only if the user has uploaded an image, because right now if there is no image then a broken image symbol appears.
I have tried the following, but it did not work:
<?php if (get_field('imagen_prensa') != ''): ?>
<img src="<?php the_field('imagen_prensa'); ?>" />
<?php endif; ?>
Any can this be achieved?
EDIT:
this is the code now using the answers below but now it gives just a blank page:
<div id="blog_interior">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<div class="article_date"><?php the_time('d/m/Y'); ?></div>
<h2><?php echo get_the_title(); ?></h2>
<div class="blog_text">
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
<div class="video_prensa"><?php the_field('video_prensa'); ?></div>
<?php the_content() ?>
</div>
</article>
<?php endwhile; ?>
<div class="back">
</div>
</div>
empty empty — Determine whether a variable is empty
if (!empty(get_field('imagen_prensa'))) {
// Display image
}
You forgot the echo to actually print the result of the_field('imagen_prensa').
Use this:
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
I've edited your code, to something which seems a bit more logical also added echo's where they were missing. I'm not used to these constructions:
<?php while ( have_posts() ) : the_post(); ?>
So I'm not actually sure if it's working properly or not, but you could check what these variables output by surrounding them with a var_dump like var_dump(have_posts());
Your edited code:
<div id="blog_interior">
<?php /* Start the Loop */ ?>
<?php foreach( $posts as $post ): ?>
<div class="article_date"><?php echo the_time('d/m/Y'); ?></div>
<h2><?php echo get_the_title(); ?></h2>
<div class="blog_text">
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
<div class="video_prensa"><?php the_field('video_prensa'); ?></div>
<?php echo the_content() ?>
</div>
<?php endforeach; ?>
<div class="back">
</div>
</div>
Try using empty() instead of comparing with an empty string.
<?php if (empty(get_field('imagen_prensa')) === false): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
empty() also checks for values like null or false which may be returned. You may also trim() the string if the field may contain only whitespaces.
As per your requirement you have set default image as no image in user.
If user have uploaded image then display its own image.
See this link may be help you. PHP: How to display a default Image if the specified one doesn't exists?