Wrapping code in a function prevents it from working - function

I want to wrap my code in a function (and then put it in functions.php) so that I can call it elsewhere but my code fails as soon as I wrap it in a function.
I think this may be a scope issue, do I have to pass the the post number somehow to the function? If I get rid of the function that's wrapped around the query, the code works fine.
I'm guessing that the code is irrelevant really (although I may be wrong) - it's more to do with the fact that it's a loop and a function.
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>

You would have it something like this I think (not tested):
<?php function getGallery2() { ?>
$global post;
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php } ?>
Then call the function within any loop on any PHP page. Make sense? i.e. don't loop within the function. I don't understand why you don't just use a php include? i.e.
require('get-gallery.php');
Hope that helps :D

$post is not in the functions scope.
You can add global $post; to the top of the function or you can include it as a parameter like this:
function getGallery2($post){
// code
}
echo getGallery2($post)
Code inside a function can only see variables that were created within the same function or in global scope. Meaning the $post object is undefined.
//
On a slightly off topic note, you have lots of HTML comments within PHP. You could easily tidy things p by making it all PHP.
EDIT:
function getGallery2(){
global $post;
// 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page');
while ( have_posts() ) : the_post();
// 2. echo the test field -->
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true);
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true);
echo '<img src="'.$link.'" alt="echo $alt " />';
endwhile;
wp_reset_query();
}
getGallery2();

Related

How to add an anchor link with Advanced Custom Fields

Is it possible to add anchor links to Advanced Custom Fields?
I've created a page that shows images that when you click on the image a lightbox opens with the description. I have it all working without using Advanced Custom Fields but I'm trying to make it user friendly for the client so I added fields for them to just fill in the information and everything is already styled.
The problem is the tabs use an Anchor Link to link to the ID DIV that opens up the lightbox with the description - it works but not when I use the fields I created.
I created a url field for them to fill in the #div name for the anchor url and a field for the name of the #div for the anchor to link to. (Hope that makes sense!)
I'm also using repeater fields so they can add/delete flavors.
This is the code I am using -
<?php
// check if the repeater field has rows of data
if( have_rows('flavors') ):
// loop through the rows of data
while ( have_rows('flavors') ) : the_row();
// display a sub field value
?>
<li>
******** This is the anchor link *************
<?php
$link = get_sub_field('flavor_name');
if( $link ): ?>
<a href="<?php echo esc_url( $link ); ?>" class="wplightbox" data-width=900 data-height=800>
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
</a>
<?php endif; ?>
********* This is the div that needs to pick up the anchor name typed in as the Div ID name *****
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
<?php the_sub_field('flavor_name'); ?>
<?php endif; ?>
<div class="flavors">
<div class="flavorsleft">
<?php
$image = get_sub_field('beer_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="flavorsright">
<h3><?php if( get_sub_field('beer_title') ): ?>
<?php the_sub_field('beer_title'); ?>
<?php endif; ?></h3>
<div class="percent">
<?php if( get_sub_field('alcohol_percentage') ): ?>
<?php the_sub_field('alcohol_percentage'); ?>
<?php endif; ?>
</div>
<?php if( get_sub_field('beer_description') ): ?>
<?php the_sub_field('beer_description'); ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="clear"></div>
</li>
<?php
endwhile;
else :
// no rows found
endif;
?>
</ul>```
This is the dev site where it's at - https://headley.tfm-dev.com/beers
The page is working right now because it's currently built not using the ACF fields (I took them off for now and rebuilt it) but I would like to change it so the owner can make changes.
Thank you!
[![Screenshot of Advanced Custom Fields][1]][1]
[![Screenshot of Advanced Custom Fields Redone][2]][2]
[1]: https://i.stack.imgur.com/NeKYe.jpg
[2]: https://i.stack.imgur.com/uUN0G.png
On your dev site in the source code the anchor href is an id but you are using a page link field to match to the get_sub_field('flavor_bottom_section_name') so this would return a url so won't work.
Correct me if I'm wrong but you are using 2 repeaters, does that not reply on each of them to match up to each other? I would do this wiht one repeater field and you'd be able to avoid mistakes being made and would probably require less fields e.g you would only need one field for the anchor and one field for the div. You can have 1 repeater field but you can have 2 separate loops through the same repeater just getting what you want out of it on each loop.
ADDITIONAL
This line of code nothing is being ouput into the id=""
<div id="<?php if( get_sub_field('flavor_name') ): ?>" style="display: none;">
I think you need to echo the sub field like this maybe
<div id="<?php echo get_sub_field('flavor_name') ?>" style="display: none;">
Yon can see in the red boxes I've marked that the id attribute is empty on the div, even if I change it locally in the inspector it works, not sure why you got an error earlier but I also think that the value in your field might have # before it but the # is only only required in the href not the id.
Final Solution (Hopefully)
Ok I used your code and I see why you were getting the critical error this:
<a href="#<?php if( get_sub_field('beer_title') ): ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
<?php endif; ?>
Needs to change to this:
<a href="#<?php the_sub_field('beer_title'); ?>" class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
You have and if in there which isn't needed so removing that and echoing the sub field fixes it, we just needed to remove the endif also. I should have asked what the error was originally.
What you are saying here is
<a href="#
// IF THERE IS A SUB FIELD VALUE FOR BEER TITLE
<?php if( get_sub_field('beer_title') ): ?>"
// OUTPUT THIS
class="wplightbox" data-width=900 data-height=800 >
<?php the_sub_field('beer_title'); ?>
get_sub_field() doesn't output anything it has to be echoed or else use the_sub_field() instead.

is_single() function dose not show thumbnail image

I am using get_template_part('content'); in single.php. The the_post_thumbnail('large-thumbnail'); function shows the thumbnail image perfectly before. But it does not show with is_single() function in content.php.
content.php code:
<?php
if(is_single()){
the_post_thumbnail('large-thumbnail');
the_content();
}else{
the_post_thumbnail('small-thumbnail');
echo get_the_excerpt();
?>
Read moreĀ»
<?php } ?>
single.php:
<article>
<div class="single_post_content">
<h5><?php the_title(); ?></h5>
<?php
get_template_part('content');
?>
</div>
</article>
I think you forget to write wp loop in your single.php file. follow this code,
<?php while ( have_posts() ) : the_post();
get_template_part('content');
endwhile; wp_reset_postdata(); ?>
the_post_thumbnail() do not work out of wp loop, So call your get_template_part() function between the loop. May be now its will solve your problem.

Wordpress post grid last posts

how can i create a grid like image? for show last posts
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php $latest_post = get_posts( 'numberposts=4' ); ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?><br />
<?php the_content(); ?>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
One option is to use PHP to swap around the position of the image based on whether or not the increment is odd or even, for instance:
$i = 0;
while ( have_posts() ) : the_post();
if ($i % 2 == 0 ) {
// Display two columns with image on left
}
else {
// Display two columns with image on right
}
$i++;
endwhile;
If you're building your theme from scratch I'd suggest using a grid framework to handle your columns, otherwise see if the theme you're using has a grid framework already.
Edit:
There are also ways of doing this without actually having to change the markup of the page. For instance:
Swapping columns (left / right) on alternate rows
In this case you'd be able to generate your post markup without the if statement and just use CSS to swap the position of the image/video.
You can use the masonry technology or you can try this plugin
In your code above you fired two WordPress content loops. I am not sure why you had to fire two loops, although they both will work. First will print the recent posts depending on the number of Posts Per Page you selected via Settings->Reading tab from your WordPress Dashboard and the second one will again print the top 4 recent posts. I am using the first loop to tell you how you can create the attached image like grid.
Below is the PHP/HTML modifications that you have to make:
<?php $count = 1; if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="one-half <?php if( $count++ % 2 == 0 ): echo 'last-col'; endif; ?>">
<?php
// the function below will print the featured image attached to the post
the_post_thumbnail( 'your-featured-image-sizename' ); ?>
</div>
<!-- one-half -->
<div class="one-half">
<span class="post_stamp"><?php the_time( 'j m Y' ); ?></span>
<span class="post_cat"><?php the_category(', '); // This will print News if you filled the post under News Category ?></span>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); // Insteading of printing the whole content this function will print exceprt only ?>
</div>
<!-- one-half -->
<div class="clearfix"><!-- --></div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You would have to put the below given links to your style file:
<style>
.one-half{width: 50%; float: left;}
.last-col{float: right;}
.clearfix{clear: both; overflow: hidden;}
</style>
After making the above changes your posts will be displayed as the attached image. Good Luck (y)

How do I restart my loop with get_next_post()?

I'm looking for a succinct method of making get_next_post()
double back to the beginning once it hits the last post.
Currently, it stops once it hits the final post.
Here are a few lines of code from the codex
for context that are similar to what I'm using:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
Thanks in advance for your suggestion.
you can't do it with get_next_post - full stop.
Here's how I've done it...
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
Previous Project:<?php echo $prev_title; ?>
<?php
// if the current post is the first post
} else { ?>
Previous Project
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
Elements take from here Getting first & last post in custom post type outside of loop

Wordpress; image not appearing post loop

I have post about this before, but I didn't get any conclusive answer but I'm really hoping someone can help me. I have setup some custom post types, and with them, some custom fields using Wordpress 3's UI.
One of the fields I have set up is called banner_image, but in the loop, it doesn't output the image.
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
This simply outputs the ID number of the post. If I set the function to false, I get an array with this ID in and nothing else. How do I get the path to the image? I can't work this out and Googling reveals a sea of content not related to my problem, it's a real difficult one to search for so you're my only hope!
Many thanks,
Michael.
<?php
global $post;
$tmp_post = $post;
$args = array(
'post_status' => 'publish',
'post_type' => 'work',
'order' => 'DESC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php if( get_post_meta($post->ID, 'show_in_home_banner', true) == "yes" ) { ?>
<li class="slide">
<div class="slide-image">
<a href="<?php echo get_page_link($post->ID) ?>">
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
</a>
</div>
<div class="slide-content">
<h3 class="slide-header"><?php echo get_post_meta($post->ID, 'sub_title', true); ?></h3>
<p class="slide-title"><strong><?php echo the_title(); ?></strong></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
Try this
<?php echo get_post_meta($post->ID, 'banner_image', $single); ?>
Apparently, the custom field 'banner_image' doesn't have the right value. I guess it doesn't save the correct value first. You may want to install the Simple WP FirePHP plugin (http://wordpress.org/extend/plugins/simple-wp-firephp/) and check the value with the fb() function.