I'm having a problem with a relationship loop with Advanced Custom Fields in Wordpress.
The first post is shown perfect, but it should execute one more post, but it doesn't.
Can anyone see what's wrong with my code?
<?php $posts = get_field('produkter'); if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="produkt">
<?php the_title(); ?>
<?php the_content(); ?>
<?php $posts = get_field('fil'); if( $posts ): ?>
<div id="filer">
<div id="topsection" class="laddahem"><div class="topborder"></div>
<h2>Ladda hem</h2>
</div><!-- #top -->
<div class="filhuvud">
<div class="filtyp">Filtyp</div>
<div class="fildatum">Datum</div>
<div class="filstorlek">Filstorlek</div>
</div><!-- .filhuvud -->
<div class="filholder">
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<?php $attachment_id = get_field('filen');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// hämta filstorleken
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 1);
$filetype = strtolower(pathinfo($url, PATHINFO_EXTENSION)); ?>
<div class="fil <?php echo $filetype; ?>">
<div class="filtyp"><a target="_blank" href="<?php echo $url; ?>" ><?php the_title(); ?></a></div>
<div class="fildatum"><?php the_time('Y-m-d'); ?></div>
<div class="filstorlek"><?php echo $filesize; ?></div>
</div><!-- .fil -->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div><!-- .filholder -->
</div><!-- #filer -->
<?php endif; ?>
</div><!-- .produkt -->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your issue is that you're overwriting the $posts variable within the loop, after you've defined it outside of the loop.
The solution is to rename your second $posts variable to something else, so that your overarching loops look like this:
<?php $posts = get_field('produkter'); ?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php $files = get_field('fil'); //RENAMED THIS VARIABLE?>
<?php if( $files ): ?>
<?php foreach( $files as $file): ?>
<?php //Main body of markup and other fun stuff ?>
<?php endforeach; //end $files foreach ?>
<?php endif; //end $files if ?>
<?php endforeach; //end $posts foreach ?>
<?php endif; //end $posts if ?>
The solution was to change the other foreach loop to this:
<?php $files = get_field('fil'); if( $files ): ?>
<?php foreach( $files as $post): ?>
<?php setup_postdata($post); ?>
That made it work exactly how I wanted it.
Related
So I am having an issue with the following code:
<?php
$posts = get_field('story_character');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
By <?php the_title(); ?>
<span><?php the_field('story_character'); ?></span>
<?php endforeach;wp_reset_postdata(); ?>
<?php endif; ?>
<?php
$posts = get_field('story_character');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
of <?php the_field('char_tribe'); ?>
<span><?php the_field('story_character'); ?></span>
<?php endforeach; ?>
<?php endif; ?>
Everything after the first query does not show up and I cannot figure out why...What am I missing?
So I have a function in functions.php and I want to call it in other parts of my wordpress site with shortcode, but the problem is that I don't know how to save results of my function into a variable. If anyone can help me I would be very grateful. This is the php code:
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
echo $posts;
if( $posts ): ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('poslovna_darila') ): ?>
<?php while( have_rows('poslovna_darila') ): the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif;
}
add_shortcode ('slick_slider' , 'get_slider'); ?>
The code returns array, but I need to get a value out of it or if it's possible the slider.
You can use an output buffer to return the string in your shortcode.
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
//echo $posts;
ob_start(); // start the output buffer
if ( $posts ) : ?>
<div class="custom-posts-grid">
<?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<?php if ( have_rows('poslovna_darila') ): ?>
<?php while ( have_rows('poslovna_darila') ) : the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
// return the contents of the output buffer as a string
return ob_get_clean();
}
add_shortcode( 'slick_slider' , 'get_slider' ); ?>
I have a page which echos about 10 items. I would like them all to have a different background image on hover.
I'm guessing I would need to have my loop in my <head> tag so that I can apply a background image.
Here is my loop:
<?php
// get background images for the services page
$post = get_field('related_services');
if( $posts ): ?>
<style>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
.sector-box[style]::after {
background-image: url("<?php echo get_template_directory_uri(); ?>/images/creative-bg-hover.jpg");
color:#<?php the_field( 'colour' ); ?>;
}
<?php endforeach; ?>
</style>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I can't get it to echo a style for each..
Would I also need to apply an ID to each box? So that it knows which box to apply the background image to?
here is my html:
<?php
$posts = get_field('related_services');
if( $posts ): ?>
<div class="container">
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="sector-box reveal" style="background-color:<?php the_field( 'colour' ); ?>;">
<div class="sector-title">
<?php the_title( '' ); ?>
</div>
</div>
<!--Close Single Service box-->
<?php endforeach; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!--Close Service Sector Boxes-->
Use the following:
<?php echo get_permalink($post->ID); ?>
<?php echo get_the_title( $post->ID ); ?>
<?php echo get_field('colour', $post->ID ); ?>
and remove function
<?php setup_postdata($post); ?>
<?php wp_reset_postdata(); ?>
I want to create blog page with recent posts and pagination. Code below shows recent posts but pagination doesn't want to work.
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$postslist = get_posts('numberposts=-1&posts_per_page=5&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<div class="recent-post-thumbnail">
<?php echo the_post_thumbnail($recent->ID, 'thumbnail'); ?>
</div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<h4>More ></h4>
</div>
<?php endforeach; ?>
</div> <!-- end content -->
<div class="kreska-pion"></div>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
numberposts=6 replace this with post_per_page and let me know if It worked .
<?php
$postlist = get_posts( 'numberposts=-1&posts_per_page=5' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<?php
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->
I tried
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$args = array( 'post_per_page' = -1 );
$query= new WP_Query( $args );
var_dump( $query );
// The 2nd Loop
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title( $query->post->ID ) . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
?>
<div class="vertical"></div>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
</div>
<?php wp_pagenavi(); ?>
<?php get_footer(); ?>
But it shows a blank page.
EDIT:
Here's what I did:
<?php get_header(); ?>
<div class="container clearfix">
<div id="content" class="clearfix">
<?php
$args = array( 'post_per_page' = -1 );
$query= new WP_Query( $args );
var_dump( $query );
wp_reset_postdata();
?>
<div class="vertical"></div>
<div class="sidebar">
<?php get_sidebar(); ?></div>
</div>
<?php wp_pagenavi(); ?>
<?php get_footer(); ?>
And it shows a blank page.
I'm still pretty new to PHP, and I'm having trouble getting this to work. What I want to do, is make the slide linked (if link is available). Otherwise, print the post thumbnail without the
Here's my code so far:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
I've done this before using the WP Custom fields, but I'm not sure how to apply it to my custom post type (called slider). Here's what I did for my Custom Field script:
<?php $slider_url = get_post_meta($post->ID, 'Slider_URL', true);
if ($slider_url) { ?>
LINKED SLIDE HERE
<?php } else { ?>
UNLINKED SLIDE HERE
<?php } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
Here's what I tried (when combining the two), but there's an error somewhere:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<?php $slide_url = get_post_meta($post->ID, 'Slide_URL', true);
if ($slide_url) { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } else { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
If I'm correct in thinking, you just want to check if the link is there, before outputting, otherwise, just show the image. Try the following:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<!-- Get a link -->
<?php $theLink = get_post_meta($post->ID, "_location", true); ?>
<li>
<!-- Check for a link -->
<?php if($theLink != ''): ?>
<a href="<?php echo $theLink; ?>" title="More Info">
<?php endif; ?>
<?php the_post_thumbnail('full'); ?>
<div class="caption">
<p class="captiontitle">
<?php the_title(); ?>
</p>
<p class="caption">
<?php the_content(); ?>
</p>
</div>
<!-- Close the link -->
<?php if($theLink != ''): ?>
</a>
<?php endif; ?>
</li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>