ACF Relationship Field posts per page - relationship

I'm using Advanced Custom Fields to call my related posts, which is all working fine, but how can I adapt this code to only call 3 posts at random?
Thank you for any help in advance!
<?php
$posts = get_field('associate_adverts');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<img src="<?php the_field('advert'); ?>">
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

I associated it the opposite way in the end and this solution worked..
<?php
$adverts = get_posts(array(
'post_type' => 'adverts',
'posts_per_page' => '3',
'meta_query' => array(
array(
'key' => 'associate_adverts', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $adverts ): ?>
<?php foreach( $adverts as $advert ): ?>
<?php $photo = get_field('advert', $advert->ID); ?>
<img src="<?php echo $photo['url']; ?>"/>
<?php endforeach; ?>
<?php endif; ?>

Related

Advanced Custom Fields: checkbox in repeater field

I can’t seem to get checkboxes working in an Advanced Custom Fields repeater field. The goal is that a different image will be spit out based on what checkbox has been ticked off. The time/title/description part is working great... just need some assistance understanding how to work in the checkbox values.
<?php if( have_rows('agenda') ): ?>
<ul>
<?php while( have_rows('agenda') ): the_row();
$time = get_sub_field('time');
$title = get_sub_field('title');
$description = get_sub_field('description');
$image = get_sub_field('imagepicker');
?>
<li>
<h3><?php echo $time; ?><?php echo $title; ?></h3>
<?php if( $description ): ?>
<?php echo $description; ?>
<?php endif; ?>
<?php if ( 'option1' == get_sub_field('imagepicker') ): ?>
<img src="/option1.jpg" />
<?php elseif ( 'option2' == get_sub_field('imagepicker') ): ?>
<img src="/option2.jpg" />
<?php elseif ( 'option3' == get_sub_field('imagepicker') ): ?>
<img src="/option3.jpg" />
<?php else: ?>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
(and yes I have gone over the other similar S.O. answers but no luck)
finally figured it out... working snippet below in case anyone else runs into the same thing.
<?php if ($image[0] == 'option1') : ?>
<img src="/option1.jpg" />
<?php elseif ($image[0] == 'option2') : ?>
<img src="/option2.jpg" />
<?php elseif ($image[0] == 'option3') : ?>
<img src="/option3.jpg" />
<?php endif; ?>

Advance Custom Fields - All fields from one field group in one frontent array?

I must stres out that i’m not a developer so i might use some “unsupported” terms :).
Ok the issue, i have created custom post type called Firm. Also i have created field group with 7 fields (text fields mainly, including website URL field and Google map field) and i have made template that displays those fields on frontend page. Once it’s saved all data is saved in database and new post under post type Firm is created. So that all works great. The main problem/question is:
How can i display all new posts in that post type (Firm) on one page? I know i must create some loop, array for those posts, but as i said i’m not developer so i’m kind of stuck with this one.
If someone could give me a hint or some link, or any kind of pointers so i could figure out in what direction to go. Thanks in advance for your answers.
You can refer Wordpress Codex.
Check sample code given below
$args = array( 'post_type' => 'product', 'posts_per_page' => 10 ); // for more parameter check link http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
// Displays Advanced custom field value
the_field('field-name');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
Ok thanks so far, thank you #Arun, i have come up with this:
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
echo '<p>' . '<span>Official Wesite</span>' . '<span> : </span>' . get_field('web_site') . '</p>';
echo '</div>';
endwhile; ?>
<?php
$location = get_field('location');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
So im strugling now with how to incorporate this google map in loop. I do have title and website link for all posts but i need to have maps for each post as well.
Ok this is what i have at the end, and it's working.
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 15 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="boxy">
<div class="acf_company_name">
<h5>Company Name: </h5>
<p><?php the_field('company_name'); ?></p>
</div>
<div class="acf_full_name">
<h5>Full Name: </h5>
<?php the_field('full_name'); ?>
</div>
<div class="acf_vat">
<h5>VAT: </h5>
<?php the_field('vat'); ?>
</div>
<div class="acf_email">
<h5>E-mail: </h5>
<?php the_field('email'); ?>
</div>
<div class="acf_website">
<h5>Official website: </h5>
<?php the_field('website'); ?>
</div>
<?php if ( get_field('logo_company') ) : ?>
<div class="acf_logo_company"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php $image = get_field(logo_company); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a></div>
<?php endif; ?>
<div class="acf_company_location"><?php $location = get_field('company_location');?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" data-lng="<?php echo $location['address']; ?>"></div>
</div></div>
<div class="acf_company_location">
<h5>Company location: </h5>
<?php the_field('company_location'); ?>
</div>
</div>
<?php endwhile; ?>

output of goods ubercart3

please help to solve the problem.
ubercart3 by default displays the items through views, using the 2-cycle
<?php foreach ($rows as $row_number => $columns): ?>
<?php foreach ($columns as $column_number => $item): ?>
<?php print $item; ?>
<?php endforeach; ?>
<?php endforeach; ?>
I would like to bring goods through one cycle. help please

Saving current view id 14 in /localhost/topp/events/view/14 to comments table field

Trying to save the current event id '14' in the url '/localhost/topp/events/view/14' to a event_id field in a comments table. I am using the add function in the comments controller and an 'add comment' button on the events view.ctp page. Any ideas? I have been advised to use a hidden id field and $this->params['pass']['0'] but not sure how to go about this, any advice is welcome. Thanks in advance.
My current comments controller add action code:
public function add() {
//$this->loadModel('Event');
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//Commented below is the logic Iam trying to achieve but is wrong
//$this->request->data['Comment']['event_id'] = $this->request->data['Event']['id'];
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash(__('The comment has been saved'));
$this->redirect(array('controller' => 'events', 'action' => 'myevents'));
} else {
$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
}
}
}
//////////////////////////////////////// My Events View code with add comment link//////////////////////////
<div class="events view">
<?php echo $this->element('maintitle'); ?>
<?php echo $this->element('profile_area'); ?>
<h2><?php echo __('Event'); ?></h2>
<dl>
<td><?php echo $this->Html->image('/files/event/photo/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo2/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo2'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo3/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo3'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo4/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo4'], array("alt" => "No Event Image")); ?> </td>
<dt></dt>
<dd>
<br>
</dd>
<dt><?php echo __('Name'); ?></dt>
<dd>
<?php echo h($event['Event']['name']); ?>
</dd>
<dt><?php echo __('Date'); ?></dt>
<dd>
<?php echo h($event['Event']['date']); ?>
</dd>
</dl>
</div>
<div class="related">
<h3><?php echo __('Related Comments'); ?></h3>
<?php if (!empty($event['Comment'])): ?>
<?php echo ('Add a comment for this event') ?>
<?php
$i = 0;
foreach ($event['Comment'] as $comment): ?>
<tr>
<td><?php echo $comment['comment']; ?></td>
<td class="actions">
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
Let me see if I got this right.
The form you posted is in the event view
The link "New Comment" redirects to another view "add", in the comments controller
This add view has a form to actually write a comment (I'm guessing this part,
you didn't put that code here)
You want the add function to have the event_id from where this came from.
Right?
You then need to pass the id of the event from event_view.ctp (I'm inventing names here) to comment_add.ctp.
So two ways to do it
First way: pass the id of the event in the link and recieve it in the action like this
//event_view.ctp
//I'm going to assume you know the event id here
$this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add', $event_id)); ?>
That will create a link like comments/add/14, for example, where 14 is the event id. Now in the action, you receive the id
//pass it like parameter of the action
public function add($event_id = null) {
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//check that the event_id isn't null and that the event actually exists first
$this->request->data['Comment']['event_id'] = $event_id;
/*etc*/
}
}
You don't need to pass the event_id to the comment_add view, if the user doesn't need to handle it, there's no need to add it to the form.
Second way: If you don't want to pass the event_id inside the url, you need to pass is by POST. To do that, the link you now have to add new comments needs to change to a form with the event_id hidden.
echo $this->Form->create('Comment', array('url'=>array('controller' => 'comments', 'action' => 'add')));
echo $this->Form->input('event_id', array('type'=>'hidden', 'value'=>$event_id);
echo $this->Form->end('New comment');
//instead of <?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?>
And in the action of the comments, check if the post you receive contains event_id, but not user_id, for example. I find this way a bit messy because every request you receive for that action is going to be a post, so it's be harder to differentiate when you want to display the form to be filled, and when you want to save it.
I like the first way better, and I recommend doing that. But I felt the need to point out there are other ways to do it. Depends what you want.

Omit certain post from loop- Wordpress

I have two loops on custom page, I want from the second loop to omit the post with Post ID from the first loop (of course show all other posts from category 36).
Edit: this is the whole code in the php file:
<?php
global $udesign_options;
// construct an array of portfolio categories
$portfolio_categories_array = explode( ',', $udesign_options['portfolio_categories'] );
if ( $portfolio_categories_array != "" && post_is_in_category_or_descendants( $portfolio_categories_array ) ) :
// Test if this Post is assigned to the Portfolio category or any descendant and switch the single's template accordingly
include 'single-Portfolio.php';
else : // Continue with normal Loop (Blog category)
get_header();
$content_position = ( $udesign_options['blog_sidebar'] == 'left' ) ? 'grid_16 push_8' : 'grid_16';
if ( $udesign_options['remove_single_sidebar'] == 'yes' ) $content_position = 'grid_24';
?>
<div id="content-container" class="container_24">
<div id="main-content" class="<?php echo $content_position; ?>">
<div class="main-content-padding">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry" style="margin:-30px 0 00px 0;">
<?php // Post Image
if( $udesign_options['display_post_image_in_single_post'] == 'yes' ) display_post_image_fn( $post->ID, false );
the_content(__('<p class="serif">Read the rest of this entry »</p>', 'udesign'));
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
<?php
$args = array( 'category' => 36, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php echo do_shortcode('[divider_top]'); ?>
<?php endforeach; ?>
<div><?php comments_template();
endwhile; else: ?>
<p><?php esc_html_e("Sorry, no posts matched your criteria.", 'udesign'); ?></p>
<?php endif; ?></div>
</div><!-- end main-content-padding -->
</div><!-- end main-content -->
<?php
if( ( !$udesign_options['remove_single_sidebar'] == 'yes' ) && sidebar_exist('BlogSidebar') ) { get_sidebar('BlogSidebar'); }
?>
</div><!-- end content-container -->
<?php endif; // end normal Loop ?>
<div class="clear"></div>
<?php get_footer(); ?>
I figured it out, this code needs to be inserted $args = array('exclude' =>theExcludedID, 'category' => 36, 'post_type' => 'post' );
According to the question's update, it seems the first loop displays all categories in portfolio_categories from a custom query, most probably including those from category 36 also.
The only way I can think off to not repeat the posts in the second loop is to exclude all category 36 posts from the first loop.
I can't test the code, but here is an idea of how to do it:
Add 3 lines of code after the Loop, like this:
if (have_posts()) : while (have_posts()) : the_post(); // This is the Loop
$Category = get_the_category( $post->ID );
$CatID = $Category[0]->cat_ID ; // The 0 assumes the post has only one category. If there are more, the number must be changed accordingly.
if ($CatID == 36) continue;
Use the following at the top of your loop:
if(get_the_ID() == theExcludedID){
continue;
}
To skip any iteration.

Resources