Create a div each month in wordpress loop - html

I'm trying to create a list of posts that has a seperator displaying each month.
It's supposed to look something like this:
January 2018
- blog post
- blog post
Febuary 2018
- blog post
etc....
The code i'm using is a simple loop. I've tried to get the seperator working using the the_date() function and change its date format. But it still triggers for each day.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post();
?>
<?php if(!the_date('F Y','','',false) == ''){ ?>
<div class="seperator">
<?php the_time('F Y'); ?> //echo month and year
</div>
<?php } ?>
<div class="post"> //the post content </div>
<?php endwhile; endif; wp_reset_query(); ?>
I don't really know what code to use to make it only trigger each month.

You should try something like this:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post();
if ($lastMonth && the_date('mm') !== $lastMonth) {
// new month
}
$lastMonth = the_date('mm');
?>
This way, in each iteration you're setting the month of publication in the lastMonth variable. The next iteration, you're checking if the month of publication is equal to the previous one. If not, you're in a new loop and you can insert your seperator.

Related

Yii2: How to validate a form with multiple instances of the same model

In my form I update more start and end dates from the same model at once. See the simplified form:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $i=>$date): ?>
<?= $form->field($date,"[$i]start"); ?>
<?= $form->field($date,"[$i]end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
In the model I need to control, if the end date is after the start date:
public function rules() {
return [
[['end'], 'compare', 'compareAttribute' => 'start', 'operator' => '>', 'message' => '{attribute} have to be after {compareValue}.‌'],
];
}
I tried to change selectors similarly as described in: Yii2: Validation in form with two instances of same model, but I was not successful. I suppose I need to change the 'compareAttribute' from 'mymodel-start' to 'mymodel-0-start' in the validation JS:
{yii.validation.compare(value, messages, {"operator":">","type":"string","compareAttribute":"mymodel-start","skipOnEmpty":1,"message":"End have to be after start.‌"});}
So, I look for something like:
$form->field($date,"[$i]end", [
'selectors' => [
'compareAttribute' => 'mymodel-'.$i.'-start'
]
])
Solution
The solution is based on the answer of lucas.
In the model I override the formName() method, so for every date I have a unique form name (based on ID for the existing dates and based on random number for new dates):
use ReflectionClass;
...
public $randomNumber;
public function formName()
{
$this->randomNumber = $this->randomNumber ? $this->randomNumber : rand();
$number = $this->id ? $this->id : '0' . $this->randomNumber;
$reflector = new ReflectionClass($this);
return $reflector->getShortName() . '-' . $number;
}
The form then looks like this:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $date): ?>
<?= $form->field($date,"start"); ?>
<?= $form->field($date,"end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
Override the formName() method in your model class to make it unique. If you don't want to change your model class, create a subclass of it for the purpose of working for this controller action. After doing this, the html ID and name fields will automatically be unique.

Insert data inner join Cakephp 3.0

I'm new to CakePHP, I just finished the blog/article tutorial and now I am trying to add the comment system to it.
In my comments table I have, Id, article_Id, name, email, text and created. In my articles table I have Id, user_id, title, text, created.
I'm trying to add the comment according to the article ID by using Insert Inner join Query. So let's say a user views a specific article like /view/1, before he add comments, the article ID is already passed so he can comment accordingly to that specific article ID he is viewing. How can I do that? I hope I'm clear.
This is my add function for my comment:
public function leavemessage()
{
$comment = $this->Comments->newEntity();
$articles = $this->loadModel('Articles');
$leavemessage = $articles->find()
->hydrate(false)
->join([
'table' => 'comments',
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.article_id = articles.id',
])
->insert(['id'])
->where(['c.article_id = articles.id'])
->execute();
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request->data);
if ($this->Comments->save($comment)) {
$this->Flash->success('The comment has been saved.');
return $this->redirect([
'controller' => 'Articles',
'action' => 'index',
'c.article_id'
]);
} else {
$this->Flash->error('The comment could not be saved. Please, try again.');
}
}
$articles = $this->Comments->Articles->find('list', ['limit' => 200]);
$this->set(compact('comment', 'articles'));
$this->set('_serialize', ['comment']);
}
My add comments view
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
<legend><?= __('Add Comment') ?></legend>
<?php
echo $this->Form->input('article_id', ['value' => $articleId]);
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('text');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

Different sidebars for different pages on Wordpress

I'm trying to design a theme on WordPress (version 3.3.2) but I am having a few problems in having the sidebar display a different set of widgets on certain pages.
I have tried several online tutorials and this one in particular http://www.rvoodoo.com/projects/wordpress/wordpress-tip-different-sidebars-on-different-pages/ but unfortunately there is no change in the sidebar when I move to a different page
I registered two sidebars on my functions.php as shown below, one which I would consider as main, and the other as a custom sidebar, and I also added different widgets to these sidebars.
<?php register_sidebar( //register sidebar
array(
'name' => 'Right-side',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar( //register second sidebar
array(
'name' => 'Second-right',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
?>
Following that, I created the files sidebar.php and sidebar-second.php to be able to call them.
sidebar.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Right-side')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
sidebar-second.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Second-right')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
And then I added replace my <?php get_sidebar() ; ?> statement with following conditional
<?php if( is_page('225') ) : ?>
<?php dynamic_sidebar('second'); ?>
<?php else : ?>
<?php get_sidebar() ; ?>
<?php endif ; ?>
However only the widgets added to the sidebar.php are displayed on every page. Any help on how I could change this, or pointers on what I could be doing wrong. Thanks.
I looks like you are using <?php dynamic_sidebar('second'); ?> where you should be using <?php get-sidebar('second'); ?>
What get_sidebar('foo') does is look for a file named 'sidebar-foo.php' in the root directory of your theme and includes it. dynamic_sidebar('bar'), on the other hand, looks for a sidebar registered with:
<?php register_sidebar( array( 'name' => 'bar' ... ) ) ?>
Hope this helps!
Why don't you use the Custom Sidebar Plugin?
It's really easy to use and does not need any coding
You have two sidebars. when you want to use the sidebar-second file on a particular page, instead of calling
<?php get_sidebar('second') ; ?>
TRY
<?php get_sidebar('sidebar-second.php') ; ?>
this should do the trick

Loop for custom post types filtered by a taxonomy in url

I've created a custom post type and a custom taxonomy to go with it.
I can't figure out how to program the loop on the taxonomy-{taxonomy}.php template to make it only show the posts that correspond with the current taxonomy archive page someone is on. <domain>taxonomy/term
I have created a loop that does filter based on taxonomy, however, its not dynamic, the loop works for whichever term of the custom taxonomy I program it to.
I would love a way to have it know which taxonomy archive (based on url?) the user is on, and only show posts from that taxonomy without having to create a template for each taxonomy term.
Current Loop:
<?php
$args = array( 'post_type' => 'event', 'type' => 'party', 'post_status' => 'future', 'posts_per_page' => 50, 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="event-item">
<div class="event-meta gold">
<div class="event-date"><?php the_time('M d'); ?></div>
<div class="event-time"><?php the_time('g:i A'); ?></div>
</div>
<div class="event-title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?>
</a>
</div>
<div class="entrytext">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Summary: having party be replaced with something that can change it based on which page a user is on would be spectacular!
Try using get_query_var('tag'), where 'tag' is the name of your custom taxonomy and add there result of that query to your $args.
I use it to create a tags archive page that changes display based on what tag I'm currently in.

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.