cakephp get the url of image from post - cakephp

I would get the image url: mysite/img/test.png but I get this link: mysite/posts/test.png
<?php echo $this->Html->link(
$this->Html->image($post['Post']['image']),
$post['Post']['title']),
array('escape' => false)
);
?>
I have to get another way the url?

Edit:
If you want link to your image, you can do in this way.
<?php echo $this->Html->link(
$this->Html->image($post['Post']['image']),
$this->Html->url('/img/'.$post['Post']['image']),
array('escape' => false)
);
?>
or
<a href="<?php echo $this->Html->url('/img/'.$post['Post']['image']); ?>">
<?php echo $this->Html->image($post['Post']['image']); ?>
</a>

Related

ACF Relationship Field posts per page

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; ?>

cakephp, how to link with two span

How to add two span tags to a link?
<?php echo $this->Html->tag('span’,
$this->Html->link($v['name']['lastname'],
$v['link’]
);
?>
//Output
<a class=« test" href="./#">
<span>name</span>
<span>lastname</span>
</a>
I'm not sure to be a good start, thank you for the help
Simply concatenate two span tags with the HtmlHelper in the first argument of the link() method.
echo $this->Html->link(
$this->Html->tag('span', $v['name']) .
$this->Html->tag('span', $v['lastname']),
$v['link'],
array('escape' => false)
);
I believe this should do it:
<?php echo $this->Html->link('<span>name</span><span>lastname</span>', '#', array('escape' => false)); ?>

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; ?>

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.

CakePHP $this->Form->end('button text'); on same line with something

I have a simple cakephp form and would like for the 'save translation' button to be to the LEFT of a link which cancels and returns the user to the index, but I can't seem to get it working. It always puts the cancel link below the button.
Code I have now:
<tr>
<td>
<?php echo $this->Form->end('Save Translation'); ?>
</td>
<td>
<?php echo $html->link('Cancel', array('action' => 'index')); ?>
</td>
</tr>
Code I've tried:
echo $this->Form->end('Save Translation',array('label'=>'Save Translation','div' => false));
Try this:
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->submit('Submit',
array('after' => $this->Html->link('Cancel', array('action' => 'index')))
);
echo $this->Form->end();
?>

Resources