Wordpress: Continues(loop) page navigation with get_permalink() - arrays

I would like to turn my page navigation in to a loop. I'm quit close but my problem is that I'm not able to switch between the categories. At the moment I have a loop inside of the category but I'm not able to jump to the next category.
As information: I have different post categories.
My script:
<?php
$prev_post = get_adjacent_post( true, '', true );
$next_post = get_adjacent_post( true, '', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>
<?php
if(($prev_post_id === '') || ($prev_post_id === null)) :
$query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => '1', 'cat=4'));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$prev_post_id = get_the_ID();
endwhile;
endif;
wp_reset_postdata();
endif;
if(($next_post_id === '') || ($next_post_id === null) ) :
$query = new WP_Query(array('order' => 'ASC', 'posts_per_page' => '1', 'cat=4'));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$next_post_id = get_the_ID();
endwhile;
endif;
wp_reset_postdata();
endif;
?>
<div id="left">
</div>
<div id="right">
</div>
$query = new WP_Query(array('order' => 'ASC', 'posts_per_page' => '1', 'cat=4'));
I tried different usage with the cat=4 or cat=1,2,3,4 but it has no effect.

Related

wp_query same data on every page

In my wordpress site where I use ACF, I created an array of arrays with wp_query and imported them into the datatable. It also acts according to the posts_per_page wordpress settings. Currently wordpress is set to 10 impressions per page. I also wrote code in function.php to display the page under the table. 10 data comes into the table. Below is the correct number of pages. The problem starts here. Although the number of pages is correct, I always see the same 10 data when I click on the pages. Where did I make the mistake?
<?php
$show_logo = (!empty($date_or_logo) and $date_or_logo == 'logo') ? true : false;
$posts_per_page = (!empty($number) and intval($number)) ? $number : get_option( 'posts_per_page' );
$current = (!empty($offset) and intval($offset)) ? $offset : 1;
$offset = (!empty($offset) and intval($offset)) ? ( $offset * $posts_per_page ) - $posts_per_page : 0;
$args = array(
'post_type' => 'stm_ico_listing',
'posts_per_page' => $posts_per_page,
'offset' => $offset,
'meta_key' => 'end_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'compare' => '<',
'value' => time(),
),
array(
'key' => 'end_date',
'compare' => '!=',
'value' => '',
),
)
);
$args = (!empty($add_args)) ? wp_parse_args($add_args, $args) : $args;
$q = new WP_Query($args);
if ($q->have_posts()): ?>
<!-- the loop -->
<?php while ($q->have_posts()): $q->the_post();
?>
<tr>
<td width="10%">
.............
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- end of the loop -->
<ul class='page-numbers'>
<li><?php pagination_bar_venue($q); ?></li>
</ul>
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<script>
$(document).ready(function() {
$('#table').DataTable( {
"language": {
"lengthMenu": "Display _MENU_ records. per page",
"zeroRecords": "Nothing found - sorry",
"info": "Showing page _PAGE_ of _PAGES_",
"infoEmpty": "No records available",
"infoFiltered": "(filtered from _MAX_ total records)"
},
"lengthMenu": [[50, 100, -1], [50, 100, "All"]]
} );
} );
</script>
function.php
add_action( 'wp_enqueue_scripts', 'team_script' );
function pagination_bar_venue( $q ) {
$total_pages = $q->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
}

CAKEPHP 4: I cannot upload many files at the same time

Goodnight (or good morning),
I trying to upload multiple files at the same time. I am following the cookbook instructions to build the solution. I always got first file (not and array of files).
Here is my view code...
<?php
/**
* #var \App\View\AppView $this
* #var \App\Model\Entity\Upload $upload
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Uploads'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="uploads form content">
<?= $this->Form->create($upload, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Upload') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('document_type_id', ['options' => $documentTypes]);
echo $this->Form->control('period_id', ['options' => $periods]);
echo $this->Form->control('user_id', ['options' => $users]);
echo $this->Form->control('documents', ['type' => 'file', 'label' => __('Choose PDF Files'), 'accept' => 'application/pdf', 'multiple' => 'multiple']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
And here is my controller code...
public function add()
{
$upload = $this->Uploads->newEmptyEntity();
if ($this->request->is('post')) {
$upload = $this->Uploads->patchEntity($upload, $this->request->getData());
if ($this->Uploads->save($upload)) {
if (!is_dir($this->Parameters->findByName('document_directory')->first()->toArray()['value'] )) {
mkdir($this->Parameters->findByName('document_directory')->first()->toArray()['value'], 0776, true);
}
$documents = $this->request->getData('documents');
$this->loadModel('Dockets');
$dockets = $this->Dockets->findByDocketStateId(1);
$documents_ok = 0;
$documents_nok = 0;
$dockets_without_document = 0;
foreach($documents as $documents_key => $document_to_save)
{
foreach($dockets as $dockets_key => $docket)
{
$contents = file_get_contents($document_to_save);
$pattern = str_replace('-', '', $pattern);
$pattern = str_replace('', '', $pattern);
$pattern = preg_quote($docket->cuit, '/');
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
$documentsTable = $this->getTableLocator()->get('Documents');
$document = $documentsTable->newEmptyEntity();
$document->upload_id = $upload->id;
$document->document_type_id = $upload->document_type_id;
$document->period_id = $upload->period_id;
$document->docket_id = $docket->id;
$document->user_id = $this->getRequest()->getAttribute('identity')['id'];
if ($documentsTable->save($document)) {
if (!is_dir($this->Parameters->findByName('document_directory')->first()->toArray()['value'] )) {
mkdir($this->Parameters->findByName('document_directory')->first()->toArray()['value'], 0776, true);
}
$fileobject = $this->request->getData('document');
$destination = $this->Parameters->findByName('document_directory')->first()->toArray()['value'] . 'document_' . $document->id . '.pdf';
// Existing files with the same name will be replaced.
$fileobject->moveTo($destination);
}
$this->Flash->error(__('The document could not be saved. Please, try again.'));
$documents_ok = $documents_ok + 1;
unset($dockets[$dockets_key]);
unset($documents[$documents_key]);
break;
}
}
}
if(!empty($documents)){
//print_r($documents);
$documents_nok = count($documents);
unset($documents);
}
if(!empty($dockets)){
$dockets_without_document = count($dockets);
unset($dockets);
}
$message = __('There were processed ') . $documents_ok . __(' documents succesfully. ') . $documents_nok . __(' documents did not math with a docket. And ') . $dockets_without_document . __(' active dockets ddid not not have a document.');
$this->Flash->success(__('The upload has been saved. ') . $message);
return $this->redirect(['action' => 'view', $upload->id]);
}
$this->Flash->error(__('The upload could not be saved. Please, try again.'));
}
$documentTypes = $this->Uploads->DocumentTypes->find('list', ['keyField' => 'id',
'valueField' => 'document_type',
'limit' => 200]);
$periods = $this->Uploads->Periods->find('list', ['keyField' => 'id',
'valueField' => 'period',
'limit' => 200]);
$users = $this->Uploads->Users->find('list', ['keyField' => 'id',
'valueField' => 'full_name',
'conditions' => ['id' => $this->getRequest()->getAttribute('identity')['id']],
'limit' => 200]);
$this->set(compact('upload', 'documentTypes', 'periods', 'users'));
}
Can you help me to understand what I doing wrong?
Thanks,
Gonzalo
When using PHP, multi-file form inputs must have a name with [] appended, otherwise PHP isn't able to parse out multiple entries, they will all have the same name, and PHP will simply use the last occurrence of that name.
echo $this->Form->control('documents', [
'type' => 'file',
'label' => __('Choose PDF Files'),
'accept' => 'application/pdf',
'multiple' => 'multiple',
'name' => 'documents[]',
]);
Furthermore the following line:
$fileobject = $this->request->getData('document');
should probably be more like this, as there a) is no document field and b) even if there were, you most likely wouldn't want to process the same file over and over again:
$fileobject = $documents[$documents_key];
Also make sure that you have proper validation in place for the uploaded files, even if you don't seem to use the user provided file information, you should still make sure that you've received valid data!

cakephp: add filter to a report from dropdown list

I have a report to generate as a view and as an excel file. I am bale to show all its rows in both. The user needs to filter data based on a date field "end_date". In my view, I put this:
<?php
$options = array();
$options[0] = 'All';
$options[1] = 'Due last 6 months';
$options[2] = 'Due current month';
$options[3] = 'Due next 3 months';
$options[4] = 'Due next 6 months';
$options[5] = 'Due next 12 months';
?>
<div class="row">
<?= $this->Form->create() ?>
<fieldset>
<div class="col-sm-2"><?= $this->Form->input('select_period', array('options' => $options)); ?></div>
<div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button('Search', ['type' => 'submit', 'class' => 'btn btn-primary']); ?></div>
</fieldset>
<?= $this->Form->end() ?>
</div>
in my controller, I use this function:
public function reportReplacement()
{
$assetsAssignations = $this->AssetsAssignations->find()
->contain([
'AssetStatuses',
'Assets' => [
'AssetTypes'
],
'Clients' => [
'ClientTypes'
],
])
->order([
'AssetTypes.name' => 'asc',
]);
$filter = $this->Filter->prg($assetsAssignations);
$_filename = "xls_report_replacement_" . date('Ymd');
$this->set(compact('assetsAssignations', '_filename'));
}
I tried to put this in my controller intialize, but it did not work:
if ($this->request->param('action') == 'reportReplacement') {
$select_period = $this->request->query('select_period'); // I want to get the selected [0], [1], etc.. when I debug, I always get null.
I also failed to fin where I have to put the option as a condition in my queries like:
if option[1], then 'end_date' >= (TODAY-6 months) AND 'end_date' <= TODAY;
if option[2], then 'end_date' = TODAY(month);
if option[1], then 'end_date' >= TODAY AND 'end_date' <= (TODAY+3 months);
etc.
any help please ?
Thanks to Greg, I found the solution:
in the view:
<?php
$options = array();
$options[0] = 'Due last 6 months';
$options[1] = 'Due current month';
$options[2] = 'Due next 3 months';
$options[3] = 'Due next 6 months';
$options[4] = 'Due next 12 months';
?>
<div class="row">
<?= $this->Form->create() ?>
<fieldset>
<div class="row">
<div class="col-xs-3"><?= $this->Form->input('select_period_id', ['options' => $options, 'empty' => true, 'label' => __('Select Period')]) ?></div>
<div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button(__('Submit')) ?></div>
</div>
</fieldset>
<?= $this->Form->end() ?>
</div>
in the controller:
use Cake\I18n\Time;
and in the function:
$data = $this->request->data;
$select_period = $this->request->data('select_period_id');
$today = Time::now()->format('Y-m-d');
$second_date = Time::now();
$assetsAssignations = $this->AssetsAssignations->find()
->contain(['Assets']);
if($this->request->is(['patch', 'post', 'put']))
{
if ($select_period == 0) {
$second_date = $second_date->modify('-6 months');
$second_date = $second_date->format('Y-m-d');
$assetsAssignations->where([
'end_date >=' => $second_date,
'end_date <=' => $today
]);
} elseif ($select_period == 1) {
$second_date = $second_date->modify('1 month');
$second_date = $second_date->format('Y-m-d');
$assetsAssignations->where([
'end_date >=' => $today,
'end_date <=' => $second_date
]);
} elseif ($select_period == 2) {
$second_date = $second_date->modify('3 months');
$second_date = $second_date->format('Y-m-d');
$assetsAssignations->where([
'end_date >=' => $today,
'end_date <=' => $second_date
]);
} elseif ($select_period == 3) {
$second_date = $second_date->modify('6 months');
$second_date = $second_date->format('Y-m-d');
$assetsAssignations->where([
'end_date >=' => $today,
'end_date <=' => $second_date
]);
} elseif ($select_period == 4) {
$second_date = $second_date->modify('12 months');
$second_date = $second_date->format('Y-m-d');
$assetsAssignations->where([
'end_date >=' => $today,
'end_date <=' => $second_date
]);
}
}
$this->set(compact('assetsAssignations'));
}

How to do multiple pagination without page not found error in Cakephp

I have paginate 2 models (Income,Expanse) in the Student model view page .
I have really page not found problem with cakephp's pagination.
It have no any problem when the paginate result has only 1 page or the same, but It will error if one has more paginate result than other.
for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.
for example(not real one) , Student's view have income and expense items ,Both are display as pagination.
//this is how I config paginator in Student controller.
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'recursive'=>0,
)
);
<div class="paging">//this is how I config Income paginator in Student view
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
?>
</div>
//this is how I config Expanse paginator in Student view
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
?>
</div>
Please help me. sorry for my english
If you have any question , please ask me.
Thank you.
Yes It is possible.....
Step1 : Make paging.ctp in View/Elements/paging.ctp
<?php if (!isset($this->Paginator->params['paging'])) {
return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
return;
}
if (!isset($options)) {
$options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;
?>
<div class="paging">
<ul class="pagination pull-right">
<?php
echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
?>
</ul>
</div>
Step2: Now add this function to Controllers/AppController
public function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
$pageInUrl = isset($this->request->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->request->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Step3: Now in controller action do some conditions so that proper page should be call
if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0,
));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Expense' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0
));
Step 4: Now call the paging element in your student view
<?php echo $this->element('paging', array('model' => 'Income'));?>
<?php echo $this->element('paging', array('model' => 'Expense'));?>
Note: Please take care of brackets and semicolon.......and sorry to be late but will help others...Thanks..
I think if you use two different iframes to load different URLs for pagination, you can handle this.
OR you have to call ajax functions to paginate 2 models in one page without reloading the page....

Wordpress query_post array with Advanced Custom Fields (probably a simple solution)

Working on a Wordpress site and was wondering if anyone could point me in the right direction.
I have the following query_post which filters posts on my template and its working great.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, ) );
How would I append this to include a check for a specific value from Advanced Custom Fields?
Posts in this category have a radio button with three options 'primary' 'secondary' 'standard'
I want to be able to check against each value i.e if you belong in 'galoretv' and 'standard' do this.
I have the page executing and sorting with the parameters above, just not sure how to add the ACF value. I was able to get it to work using the sticky option but thats gonna work cause i need to have primary and secondar optionality. This is show i had it working with the sticky.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'post__in'=>get_option('sticky_posts')) );
The radio buttons field is called 'landing-grid-placement'
Any thoughts? Been checking the documentation, can't figure it out.
http://www.advancedcustomfields.com/docs/field-types/checkbox/
Thought this would work but it didn't
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'landing-grid-placement' => 'primary') );
Any help would be greatly appreciated. This is probably a simple syntax issue but its eluding me and causing me a lot of issues. Been searching for an answer but haven't got it right yet. Thank you advance to whoever reads this and a double thank you to anyone who contributes a solution.
APPENDED CODE PER NOTE BELOW
<?php
$args = array(
'post_type' => 'post',
'category-slug' => 'models-galore',
'showposts'=>1,
'meta_query' => array(
array(
'key' => 'grid_location',
'value' => 'primary',
'compare' => '=',
'type' => 'CHAR'
)
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h3>
<?php the_title(); ?>
</h3>
</div>
</li>
<?php
}
}
?>
You can do this with a meta_query
Here's the doc
And here's an example:
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_query' => array(
array(
'key' => 'landing-grid-placement',
'value' => 'primary',
'compare' => '=', //default
'type' => 'CHAR' //default
)
)
);
//This is the shoposts option (deprecated since 2.1, now use posts_per_page)
$args['posts_per_page'] = -1; //-1 = infinite
//to add taxonomy
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'galoretv'
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h2>
<?php the_title(); ?>
</h2>
</div>
</li>
<?php
}
}
Maybe there is a simpler solution for you :
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_key' => 'landing-grid-placement',
'meta_value' => 'primary',
'meta_compare' => '=' //default
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
//do what you would normally do in your loop
}
}

Resources