How do i loop to display post category title? - categories

Please, pardon me for unending questions. I am just a newbie as far as wordpress theme development is concerned. I am just cracking my brain to get something done. I have successfully worked on my code and it gives one category title. The category title of the post is not appropriate. I beg someone out there to help look into my code below and help me correct where necessary. I am just tired over this issue.
I want it to display appropriate category title above the post as you can see about. The category title is outside the loop and not within. It is equally not in category page rather at the front page.
`<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'building',
)
)
);
$q = new WP_Query( $args );
$leadingcount = 3;
$cats = get_terms( array( 'taxonomy' => 'product_cat' ) );
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h6 class='mt-5 text-center'>".$cat->name."</h6>"; ?>
<?php if (have_posts()) : while ( $q->have_posts() ) : $q->the_post(); ?>
<article class="item col-lg-<?php echo $leadingcount; ?>">
<div class="card rounded-0">
<div class="card-body">
<a href="<?php the_permalink(); ?>">
<h5><?php the_title(); ?></h5>
</a>
<?php the_excerpt(); ?>
<?php the_category(); ?>
</div>
</div>
</article>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
`

Related

Showing custom fields of custom post type in a date ordered list on a page

I'm struggling with this. I add multiple custom field values: in this case event dates with their title and location to all my projects (custom post type) and want to display them in a table together on a page called Calendar. I tried to query the posts but then the custom fields will show as groups together, and not as a date ordered list.
The event titles match the post title in this case.
My question is: what would be the best solution? Creating all the event dates as custom fields within the posts and then show them all together on a single page called calendar, or create them all in a calendar page and then filter those on title to show only the relevant ones in a single post that match the page title?
The event name is taken from a post object, which seemed the best solution for the scenario where all events are created on the same page, but other wise I might use just the post title.
Hope there is someone to help!
My code for the calendar (the part with upcoming dates):
<div class="Rtable upcoming Rtable--collapse">
<?php $outs = array(); if( have_rows('events') ): ?>
<?php while ( have_rows('events') ) : the_row(); ob_start();
// Get sub field values.
$date = get_sub_field('date');
$location = get_sub_field('location');
$website = get_sub_field('website');
$title = get_sub_field('title');
$premiere = get_sub_field('premiere');
$event = get_sub_field('date', false, false);
$today = strtotime(date('Ymd'));
$upcoming = strtotime($event);
$datefin = new DateTime($event); ?>
<?php if ($upcoming >=$today): ?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php if( get_sub_field('date') ): ?><?php the_sub_field('date'); ?><?php endif; ?>
</div>
<div class="Rtable-cell table-project-title"><?php if( get_sub_field('premiere') == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif; ?>
<?php if( $title ): ?><?php foreach( $title as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?><?php the_title(); ?> <?php endforeach; ?> <?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?> <?php endif; ?>
</div>
<div class="Rtable-cell"><?php if( get_sub_field('location') ): ?><?php the_sub_field('location'); ?><?php endif; ?></div>
<div class="Rtable-cell Rtable-cell--foot"><?php if( get_sub_field('website') ): ?><a class="button" href="<?php echo esc_url( $website ); ?>" target="_blank">Tickets</a> <?php endif; ?></div>
<?php endif; ?>
<?php $outs[] = ob_get_clean(); endwhile; ?>
<?php
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>
</div><!-- UPCOMING END -->
I found the solution, to query by the custom field date. Result here:
<?php
/**
* Template Name: Calendar
*/
get_header();
?>
<?php get_header();
?>
<div class="content-header yellow">
<h1><?php the_title();
?></h1>
</div>
<!-- Kalender -->
<div class="yellow content">
<div class="Rtable upcoming Rtable--collapse">
<?php
// Create an empty array for storage
$post_data = array();
// Set arguments for your query
$args = array(
'post_type' => 'projects',
'posts_per_page' => -1
);
// Do our query with any arguments from above
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Setup our repeater
if ( have_rows( 'events' ) ):
while ( have_rows( 'events' ) ) : the_row();
// Optional - Only get posts that match a true false within our repeater
$event = get_sub_field( 'date', false, false );
$today = strtotime( date( 'Ymd' ) );
$upcoming = strtotime( $event );
$datefin = new DateTime( $event );
if ( $upcoming >= $today ) {
// Build our array with the data we need using key => value
$post_data[] = array(
'url' => get_the_permalink(),
'title' => get_the_title(),
'date' => get_sub_field( 'date' ),
'location' => get_sub_field( 'location' ),
'website' => get_sub_field( 'website' ),
);
}
endwhile;
endif;
}
} ?>
<?php // Custom function to use in our sort to list the items by date
function subval_sort( $a, $b ) {
if ( $a['date'] == $b['date'] )
return 0;
return $a['date'] < $b['date'] ? -1 : 1;
}
// Sort our multidimensional array by sub array value
usort( $post_data, 'subval_sort' );
// We can now work our data normally through easy to access methods
foreach ( $post_data as $post ) {
?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php $format_in = 'm/d/Y';
// the format your value is saved in ( set in the field options )
$format_out = 'd/m/Y';
// the format you want to end up with
$date = DateTime::createFromFormat( $format_in, $post['date'] );
echo $date->format( $format_out ) . ' ' ;
?>
</div>
<div class="Rtable-cell table-project-title"><?php if ( get_sub_field( 'premiere' ) == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif;
?>
<a href="<?php echo $post['url']; ?> ">
<?php
echo $post['title'] . ' ' ;
?>
</a>
</div>
<div class="Rtable-cell">
<?php echo $post['location'] . ' ' ;
?>
</div>
<div class="Rtable-cell Rtable-cell--foot">
<a class="button" href="<?php
echo $post['website']; ?> ">
tickets
</a>
</div>
<?php }
?>
</div>
</div>
<?php get_footer();
?>

Yii2 use form tags

When I submit my form i got some errors,
This is my form script which contains posted fields.
<?php $form = ActiveForm::begin([
'action'=>'userpermission/create',
]); ?>
<form method="post" action="<?php echo Yii::$app->getUrlManager()->createUrl('admin/userpermission/create')?>">
<ul class="list-unstyled">
<li>
<?= $form->field($model, 'idPermission')->checkboxList(ArrayHelper::map(Permission::find()->all(),"idPermission", "libelle", [
'onclick' => "$(this).val( $('input:checkbox:checked').val());",
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='ckbox ckbox-primary col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}' tabindex='3'>{$label}</label>";
}
])) ?>
</li><br>
</ul>
<div class="form-group">
<?php Html::submitButton($model->isNewRecord ? 'Valider' : 'Create' ,['class' => $model->isNewRecord ? 'btn btn-primary','value'=>'Create', 'name'=>'submit']) ?>
</div>
<?php ActiveForm::end(); ?>
and my create function looks like but i got the error undefined variable model!
public function actionCreate()
{
$model = new Userpermission();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
print_r(Yii::$app->request->post());
exit;
return $this->redirect(['index', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
1st off, you dont need that <form> tag.
<?php $form = ActiveForm::begin([
'action'=>'userpermission/create',
]); ?>
creates and initializes the form for you with corresponding client-validations.
the possible issue is due to unclosed </form> which anyways is unnecessary.
suggesting to remove the <form> tag entirely. and try again and if any issue please let me know the error.
also bring the print_r(Yii::$app->request->post()); before if condition.
enable error reporting in your function
error_reporting(E_ALL);
please give the filename to code block. it would be easier to understand that way.

wp shortcode breaks get_post_thumbnail

I'm having some trouble with short code's breaking a featured image I have set on my home page in Wordpress (4.0). For some reason, when I paste in my shortcode in the wp-admin > page > edit > content for the home page, suddenly my featured image stops working. It's working until I paste in the shortcode and update the page, but then the image disappears and the_post_thumbnail() returns false. I've also tried get_the_post_thumbnail() without success.
Here's my code excerpt from "front-page.php":
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php if (has_post_thumbnail()) {the_post_thumbnail();} ?>
</div>
And here's the shortcode function from "functions.php":
// [random_testimonial]
function short_code_get_random_testimonial(){
$args = array( 'post_type' => 'testimonial', 'posts_per_page' => 1, 'orderby' => 'rand' );
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$random_testimonial = get_the_content();
$author = get_the_title();
$rt = '<p class="quote">' . $random_testimonial;
$rt .= '</p><p class="right">– ' . $author . '</p>';
endwhile;
return $rt;
}
// Register the shortcodes.
add_shortcode( 'random_testimonial', 'short_code_get_random_testimonial' );
// Allow text widgets to contain shortcodes.
add_filter('widget_text', 'do_shortcode');
add_filter('the_content', 'do_shortcode');
Any ideas would be greatly appreciated.
I think I may have solved this one. It seems to have something to do with the fact that I was calling a custom post type with the shortcode, so by the time the_post_thumbnail() was called, the query had changed to refer to the custom post type included in the shortcode ("testimonial") rather than the parent post (in this case the home page). I revised my code as follows to catch the image before the shortcode is called, and all is working again:
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<?php
// go ahead and get the post image in case [random_testimonial]
// shortcode is used in page content which will break the query.
if (has_post_thumbnail()) {$image = get_the_post_thumbnail();} ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php
// Echo post image from above.
if(isset($image)){echo $image;}
?>
</div>

select menu default values not rendering in symfony

I am using Symfony 1.4, and am using embedded forms to place multiple similar forms into one form for a configuration page. I am having success showing the form, but the default values of the sfWidgetFormChoice widgets are not being rendered, i.e. the selected="selected" attribute is gone from the HTML.
Incidentally, the default values show up if I don't use embedded forms. The problem with avoiding embedded forms is that each form has identical inputs and therefore overwrites itself.
The action code is as such, some code omitted for brevity:
$serviceFormArray = array();
$this->fullForm = new ConfigForm();
foreach($this->serviceArray as $net => $service)
{
$this->partialForm = new ConfigForm();
foreach($service as $typeId => $val)
{
$typeObj = Doctrine::getTable('Type')->find($typeId);
$typeField = new sfWidgetFormChoice(array(
'default' => $val,
'choices' => array('1' => 'on', '0' => 'off'),
'label' => $typeObj->name)
);
$typeField->setDefault($val);
$serviceFormArray[$typeObj->name] = $typeField;
}
$netObj = Doctrine::getTable('Network')->find($net);
$this->partialForm->setWidgets($serviceFormArray);
$this->fullForm->embedForm($netObj->name,$this->partialForm);
}
and the template looks like this, some code omitted for brevity:
<div class="sectionBox">
<?php echo $fullForm->renderFormTag('/configure/submitconfig') ?>
<?php foreach ($fullForm->getVisibleFields() as $part => $field): ?>
<div class="settingsField">
<?php echo $field->renderLabel() ?>
<?php echo $field->render() ?>
<input type="hidden" name="plug" value="<?php echo $plugName; ?>"/>
</div>
<?php endforeach; ?>
<div id="submitConfig"><input type="submit" value="Save"/></div>
</form>
</div>
Try setting default value via $form->setDefault($name, $default).
$this->partialForm->setDefault($typeObj->name, $val);

Zend_Paginator: no page number on links on a page with no page number specified by the URL

Without sticking /1 onto a url is there away to change Zend_Paginator to approach a URL? Currently the user goes to /aaron/studio. Then the user should click through the paging and start accessing URLS such as : /aaron/studio/2
I have this rule:
$router->addRoute('studios/page', new Zend_Controller_Router_Route(':id/studio/:page',array('module' => 'default', 'controller' => 'studio', 'action' => 'view')));
If I go-to /aaron/studio/2, the Paginator links correctly to other pages, if i goto /aaron/studio it doesn't link to other pages, just the page its on.
What I need todo somehow it make Paginator aware that of its location even without a page in the URL.
Heres my controller code if it helps:
$page = $this->_getParam('page', 1);
$from = ($page * $this->clips_per_page) - $this->clips_per_page;
$count = Model_Clip::load_by_type(array('type' => 'studio_id', 'values' => $object->id, 'to' => 0, 'to' => COUNT_HIGH, 'count' => 1, 'order' => 'd.views DESC'));
$paginator = Zend_Paginator::factory($count);
$paginator->setItemCountPerPage($this->clips_per_page);
$paginator->setCurrentPageNumber($page);
$paginator->setPageRange(25);
$this->view->paginator = $paginator;
Edit, heres my view as requested:
<?php if (count($this->paginator) && $this->paginator->count() > 1): ?>
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml', array('translate' => $this->translate)); ?>
<?php endif; ?>
and the partial
<div class="pagination-control" style="width: auto; text-align: center; margin: 0 auto">
<div style="width: auto;">
<!-- First page link -->
<?php if (isset($this->previous)): ?>
Start |
<?php else: ?>
<!-- <span class="disabled">Start |</span> -->
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
Previous |
<?php else: ?>
<!-- <span class="disabled"> Previous |</span> -->
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?php echo $page; ?>
<?php else: ?>
<span class="chosen"><?php echo $page; ?></span>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
| Next |
<?php else: ?>
<!-- <span class="disabled">| Next |</span> -->
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
End
<?php else: ?>
<!-- <span class="disabled">End</span> -->
<?php endif; ?>
<p>
Page <?php echo $this->current; ?> of <?php echo $this->last; ?>
</p>
</div>
<p class="clear"></p>
</div>
I had the same problem, and I just figured out that the problem was in the route.
First I had specified the route like this:
$router->addRoute(
'projects',
new Zend_Controller_Router_Route('/:lang/projects/:category/',
array('lang' => 'en',
'module' => 'index',
'controller' =>'projects',
'action' =>'index'
)
)
);
so the paginator was not printing the page numbers when generating the links, when I changed the route and put the :page var the paginator worked. The final route is:
$router->addRoute(
'projects',
new Zend_Controller_Router_Route('/:lang/projects/:category/:page',
array('lang' => 'en',
'module' => 'index',
'controller' =>'projects',
'action' =>'index',
'page' => 1
)
)
);
Try providing a default value of 1 for :page in the router
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard
(scroll down to Variable Defaults)
$router->addRoute('studios/page',
new Zend_Controller_Router_Route(':id/studio/:page',
array('module' => 'default', 'controller' => 'studio', 'action' => 'view'),
array('page' => 1)));