How to adapt this wordpress loop query code to include pagination? - foreach

I have sourced the below code to query my posts in Wordpress and all works as intended.
I'd like to add pagination to this now. I don't want to alter the code too much now that I know it works, can anyone advise on the best way to adapt this to include the pagination?
I would like it to show a maximum of 18 posts per page and have next and prev links to other pages if they exist. This code is being used in custom category templates, but I have also setup a static page setup under the reading settings and the display of main posts uses home.php, I'd like to use the same or similar loop code to also paginate that. Any help is appreciated. Here is the code:
<div id="Items">
<ul>
<?php
// Grid sorted alphabetically
if (is_category('categoryName'))
{
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
$categoryNameposts = get_posts( $args );
}
foreach( $categoryNameposts as $post ) : setup_postdata($post);
?>
<li><?php get_template_part( 'content', get_post_format() ); ?></li>
<?php endforeach; ?>
</ul>
</div><!-- #ItemsEnd -->

You should let wordpress know how much post you want to show per page in the query_posts. Replace your $args with these two lines:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 18, 'paged' => $paged, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
Now to show Next-Previous link:
<div class="paginationClass">
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
</div>
Also in is_category function, use categorySlug, instead of categoryName.

Related

Octobercms disble fields in relation

I want to disable form fields in the update context, see the image:
I tried this, but it did not work
public function filterFields($fields, $context = null)
{
if($context == 'update') {
$fields->books->disabled = true;
$fields->user->disabled = true;
}
}
Seems you are trying to make relation manager field disable(read only)
But I am sure they are not following same pattern as normal widgets do.
they can not be disabled directly like that as I found another easy way.
you may have used partial for rendering this relational field ( book | user ) and your partial _books.htm is looking like this.
<?= $this->relationRender('comments', ['readOnly' => false]) ?>
You need to change it with this one
<?php if($this->widget->form->context == 'update'): ?>
<?= $this->relationRender('comments', ['readOnly' => true]) ?>
<?php else: ?>
<?= $this->relationRender('comments', ['readOnly' => false]) ?>
<?php endif; ?>
The magic config value is this readOnly property it will make list read-only or active working.
try this it will work , if not please comment.

How to Display Wordpress Posts by Custom Field

I am trying to create a page that displays all my wordpress posts that contain a certain custom field. I assumed that the best way to do this is to create a "custom page template" and use a new WP_query to list the posts in question. I created a new "page" and applied the new "page template" with my new WP_query, however no posts show up on the page. Can anyone tell me why this custom page template does not correctly list the posts in question? Any help is much appreciated!
<?php
/**
* Template Name: Recommended Incentives
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
get_header(); ?>
<div id="main-content" class="main-content">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// args
$args = array(
'meta_key' => 'Incentive ID',
'meta_value' => '20'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
Thanks Andy,
My code worked when I specified the post type in the $args variable. The code I used is
'meta_key' => 'Incentive ID',
'meta_value' => array(20,21),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'incentives'

Using ZfcUser and BjyAuthorize to show or hide views

I have recently installed ZfcUser and BjyAuthorize and would like to use them to show or hide various parts of the layout.phtml file.
I understand that BjyAuthorize is a firewall of sorts and the flowchart from git hub suggests it should be possible to get current permission status and to use that to hide or show a particular section of code.
So for instance:
<ul>
<li>Admin Menu item</li>
<li>Affiliate menu item</li>
<li>Guest Menu item</li>
</ul>
If an admin user is logged in, he will view all three items, the affiliate will only see Affiliate and guest and the guest will only see guest.
How I was thinking of doing this was something like this:
<?php
//Get array of permissions for current user *not certain how to do this*
$permissionArray = $this->GetBjyPermissions($current->user);
?>
<ul>
<?php if in_array('admin',$permissionArray) {?>
<li>Admin Menu item</li>
<?php } ?>
<?php if in_array('affiliate',$permissionArray) {?>
<li>Affiliate Menu item</li>
<?php } ?>
<li>Guest Menu item</li>
</ul>
Essentially this will allow me to hide the sections of code a user is not allowed to use.
If it is not possible to get the permissions via Bjy or Zfc I guess my option would be to simply query the Database and build a permissions array from that directly.
Has anyone else had to do something like this? Is this approach a good approach or is there another way of achieving this?
Many thanks for any input.
You can use the BjyAuthorize's IsAllowed view Helper. It knows the current user's identity, so you just have to check the rule. It works like:
$isMenuAdmin = $this->isAllowed( 'menu', 'menu_admin' );
$isMenuAffiliate = $this->isAllowed( 'menu', 'menu_affiliate' );
$isMenuGuest = $this->isAllowed( 'menu', 'menu_guest' );
menu is a resource and menu_* a rule. You have to define them in the bjyauthorize.global.php. I'd do it this way:
(...)
'resource_providers' => array(
'BjyAuthorize\Provider\Resource\Config' => array(
'menu' => array(),
),
),
'rule_providers' => array(
'BjyAuthorize\Provider\Rule\Config' => array(
'allow' => array(
/*
[0] -> role
[1] -> resource
[2] -> rule
*/
array( array( 'admin' ), 'menu', array( 'menu_admin' ) ),
array( array( 'affiliate' ), 'menu', array( 'menu_affiliate' ) ),
array( array( 'guest' ), 'menu', array( 'menu_guest' ) ),
),
),
),
(...)
BTW, it seems that you're trying to build a menu. I recommend you to check this post about integrating Zend Navigation with BjyAutorize.

ZF2 Form\Element\MultiCheckbox: how to get each item on a new line?

I've been Googling this to no avail. I have a multi-checkbox form element in one of my forms. Here's the code I used to create it:
$this->add(array (
'name' => 'thingyId',
'type' => 'MultiCheckbox',
'options' => array (
'value_options' => $thingyArray,
)
));
In my view script, I have this:
<?= $this->formRow($form->get('thingyId')); ?>
The form element shows up fine, but all of the checkboxes are on a single line. How do I get it so that each checkbox is on a new line?
If you view this link, you can see that the fourth argument is partial. So, you can use many ways to accomplish the task.
Method 1:
echo $this->formRow($element, null, null, 'template-file');
Now, create a template file named as template-file.phtml to render the element however you like.
//template-file.phtml
<span><?php echo $label; ?></span><br/>
<?php foreach ($element->getValueOptions() as $key => $value): ?>
<input type="checkbox" name="<?php echo $element->getName() ?>[]" value="<?php echo $value; ?>">
<span><?php echo $key; ?></span><br/>
<?php endforeach; ?>
Method 2
Create your own view helper by extending the default helper.
namespace Application\View\Helper;
class MyFormRow extends \Zend\Form\View\Helper\FormRow
{
/**
* #var string
*/
protected $partial = 'template-file';
}
Now, inform our application about our new helper in your module,
namespace Application;
class Module
{
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'myFormRow' => 'Application\View\Helper\MyFormRow'
)
);
}
}
Lastly use the helper:
echo $this->myFormRow($element);
I came across this question when I was having this issue myself. The code that was being used was the following:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
echo $this->formMultiCheckbox($oMultiCheckboxField);
?>
The only additional parameter you could pass to the formMultiCheckbox view helper was whether to append or prepend the label.
How I eventually chose to solve this is with the following code:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
$oMultiCheckboxViewHelper = new \Zend\Form\View\Helper\FormMultiCheckbox();
$oMultiCheckboxViewHelper->setSeparator('<hr>');
echo $oMultiCheckboxViewHelper->render($oMultiCheckboxField);
?>
From what I recall, ZF1 had the option to set the separator (I clearly remember this at least for radio buttons). Why there isn't a clearer way to do this in ZF2 is a bit puzzling. If there are better ways to do this, I would certainly like to know about it.

putting jquery buttons in clistview widget in yii

I am trying to put a button which will be displayed along with some data in the view file I specified in "itemView" field of the CListView widget, but instead of the styled button for every list item, I am just getting it or the first list item. My code in the _view file is:
<div id="listView">
<div class="thedata">
...some data
</div>
<div id="buttons">
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType'=>'button',
'name'=>'btnJobs',
'caption'=>'Manage Jobs',
'options'=>array('icons'=>'js:{primary:"ui-icon-wrench"}'),
'onclick'=>'js:function(){alert("Manage Jobs clicked."); this.blur(); return false;}',
));
?>
</div>
</div>
and the code for CListView widget is just the bare minimum:
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view'
));
any suggestions?
Try passing in a unique ID to the CJuiButton, like so:
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'id'=>'button'.$data->id, // add a unique ID here (could use $index instead of $data->id)
'buttonType'=>'button',
'name'=>'btnJobs',
'caption'=>'Manage Jobs',
'options'=>array('icons'=>'js:{primary:"ui-icon-wrench"}'),
'onclick'=>'js:function(){alert("Manage Jobs clicked."); this.blur(); return false;}',
));
?>
The problem is that since all of your buttons have the same 'name' (and therefore 'id') jQuery is only binding to the first one. Making sure each button has a unique ID should fix this, so jQuery will bind correctly.

Resources