How can I display only two posts from each category in wordpress? - html

I have to display 2 posts from each category. The code below gets all the posts belong to each category. My structure is tab based. I am attaching the image of my design. My tabs contain "all (shows all posts)", "symptoms (shows posts related to symptoms only" etc. ,clicking on each tabs shows relevant posts.
<?php $recent = new WP_Query("post_type=post&posts_per_page=-1&orderby=date&order=DESC");
$count=0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
while($recent->have_posts()) : $recent->the_post(); ?>
<?php $c = get_the_category();
$cat = $c[0]->cat_name;
$slug = $c[0]->category_nicename;
?>
<div class="element-item transition <?php echo $slug;?>" data-category="<?php echo $slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
[![enter image description here][2]][2]
Any help would be appreciated

Try like this
<?php
$cat_args = array(
'orderby' => 'date',
'order' => 'DESC',
'child_of' => 0,
'parent' => '',
'type' => 'post',
'hide_empty' => true,
'taxonomy' => 'category',
);
$categories = get_categories( $cat_args );
foreach ( $categories as $category ) {
$query_args = array(
'post_type' => 'post',
'category_name' => $category->slug,
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC'
);
$recent = new WP_Query($query_args);
while( $recent->have_posts() ) :
$recent->the_post();
?>
<div class="element-item transition <?php echo $category->slug;?>" data-category="<?php echo $category->slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile;
}
wp_reset_postdata();
?>
First you list all the categories, then in foreach loop through each, and query only 2 most recent ones.
Note that you can list any taxonomy this way. I just put
'taxonomy' => 'category',
but this can be a taxonomy assigned to your custom post type for instance.
List of arguments looks kinda like this:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
Hope this helps.

Related

Custom Post Type adds extra html tags

I have a custom post type called services but when displaying them, it is adding two extra <p> tags in the code for each post. I have no idea why.
This is the registration of the post type:
function services_post_type() {
$args = array(
'labels' => array(
'name' => __( 'Services', 'services' ),
'singular_name' => __( 'Service', 'service' ),
'menu_name' => 'Services',
),
'description' => 'Add a service for your website.',
'supports' => array( 'title', 'editor', 'thumbnail' ),
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-text-page',
'has_archive' => true,
'rewrite' => array('slug' => 'services'),
);
register_post_type( 'services', $args );
}
add_action( 'init', 'services_post_type' );
This is the display of the post type:
<div class="row">
<?php
// The Query
$query = new WP_Query(array('post_type' => 'services', 'order' => 'ASC'));
query_posts( $query );
// The Loop
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="col-6">
<h3 class="service-item-title"><?php the_title(); ?></h3>
<p class="service-item-content"><?php the_content(); ?></p>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
</div>
And this is what inspect shows:
Inspect Screenshot
the_content() directly echoes all contents, including its HTML tags (which are often p tags), so you shouldn't put it into a p tag. If you need to add a class, use a DIV tag as a container for it:
<h3 class="service-item-title"><?php the_title(); ?></h3>
<div class="service-item-content">
<?php the_content(); ?>
</div>

yii2 - two modal in one active form

I have 2 modals in one active form. Let's say first one is modal-ticket and second one is contract-ticket. First time, I click button to show modal-ticket, and it was fine, but when I click button to show modal-contract, it also call script modal-ticket or otherwise. why?
this is my code.
<?php $form = ActiveForm::begin(); ?>
<div id="BtnSearchTicket">
<?= Html::button('Search Ticket', [
'value' => Url::to('../ticket-timbangan/list'),
'class' => 'btn btn-primary',
'id' => 'BtnModalTicketList',
'data-toggle'=>"modal",
'data-target'=>"#modalTicketList",
]) ?>
</div>
<div id="BtnSearchContract">
<?= Html::button('Search Contract', [
'value' => Url::to('../contract/list'),
'class' => 'btn btn-primary',
'id' => 'BtnModalContractList',
'data-toggle'=>"modal",
'data-target'=>"#modalContractList",
]) ?>
</div>
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'header' => 'Ticket List',
'id' => 'modalTicketList',
'size' => 'modal-lg',
'class' => 'style=width:auto'
]);
echo "<div id='modalContentTicket'></div>";
Modal::end();
?>
<?php
Modal::begin([
'header' => 'Contract List',
'id' => 'modalContractList',
'size' => 'modal-lg',
'class' => 'style=width:auto'
]);
echo "<div id='modalContentContract'></div>";
Modal::end();
?>
<?php
$script = <<< JS
$('#BtnModalTicketList').click(function(e){
e.preventDefault();
$('#modalTicketList').modal('show')
.find('#modalContentTicket')
.load($(this).attr('value'));
return false;
});
$('#BtnModalContractList').click(function(e){
e.preventDefault();
$('#modalContractList').modal('show')
.find('#modalContentContract')
.load($(this).attr('value'));
return false;
});
JS;
$this->registerJs($script);
?>
this are the error found in console web browser
GET http://localhost/pks/web/ticket-timbangan/get-ticket?id=1 404 (Not Found)
GET http://localhost/pks/web/contract/get-contract?id=2 404 (Not Found)
please help.
Try This: using one modal on your form:
<?php $form = ActiveForm::begin(); ?>
<div id="BtnSearchTicket">
<?= Html::button('Search Ticket', [
'value' => Url::to('../ticket-timbangan/list'),
'class' => 'btn btn-primary showModal',
'id' => 'BtnModalTicketList',
'title' => 'Search Ticket',
]) ?>
</div>
<div id="BtnSearchContract">
<?= Html::button('Search Contract', [
'value' => Url::to('../contract/list'),
'class' => 'btn btn-primary showModal',
'title' => 'Search Contract',
'id' => 'BtnModalContractList',
]) ?>
</div>
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'headerOptions' => ['id' => 'modalHeader'],
'id' => 'modal',
'size' => 'modal-lg',
'closeButton' => [
'id'=>'close-button',
'class'=>'close',
'data-dismiss' =>'modal',
],
'class' => 'style=width:auto',
'clientOptions' => [
'backdrop' => false, 'keyboard' => true
]
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
<?php
$script = <<< JS
$(document).on('click', '.showModal', function(){
if ($('#modal').hasClass('in')) {
$('#modal').find('#modalContent')
.load($(this).attr('value'));
document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
} else {
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
}
});
JS;
$this->registerJs($script);
?>
From viewing your code what calls my attention is how you use the Url::to() function. Not sure why you use ../. I have fiddled with using more than one modal in a form and the code is similar to what your are doing. I suggest you try changing your Url::to() function as follows:
Url::to('/ticket-timbangan/list')
and
Url::to('/contract/list')
Another suggestion would be to try with
Yii::$app->urlManager->createUrl('/ticket-timbangan/list')
and
Yii::$app->urlManager->createUrl('/contract/list')
Let me know if this helps.
First, many thanks to kalu who has helped me 2 days to solve my problem. Two modals which contains grid need to define different classes to prevent execute previous script.
'value' => Url::to('../ticket-timbangan/list') and 'value' => Url::to('../contract/list'), both has two gridview and same select-row class. This class is root cause of the problem, because it's same class and execute both script.
Thanks Kalu, you save my day.

Remove line break from CakePHP form submit and link buttons

I am modifying my login form in CakePHP, and want to add Register Button after Login button. However CakePHP creates line break between these two elements:
My login.ctp file:
<?php
/**
* Copyright 2010 - 2011, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* #copyright Copyright 2010 - 2011, Cake Development Corporation (http://cakedc.com)
* #license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<div class="row-fluid">
<div class="span4"></div>
<div class="span4">
<fieldset>
<?php
echo $this->Form->create($model, array(
'plugin' => 'users', 'controller' => 'users',
'language' => $this->Session->read('Config.language'),
'id' => 'LoginForm', 'class' => 'well'));
?>
<div class="row-fluid">
<?php
if ($_SERVER['HTTP_HOST'] == Configure::read('webakis.touchscreen_host')) {
echo $this->Form->input('name', array(
'label' => __('Name'), 'class' => 'span12'));
echo $this->Form->input('surname', array(
'label' => __('Surname'), 'class' => 'span12'));
} else {
echo $this->Form->input('email', array(
'label' => __('Email or name surname'), 'type' => 'text', 'class' => 'span12'));
}
echo $this->Form->input('password', array(
'label' => __('Password'), 'class' => 'span12'));
// echo '<p>' . $this->Form->checkbox('remember_me') . __( 'Remember Me') . '</p>';
//echo '<p>' . $this->Html->link(__( 'I forgot my password'), array('action' => 'reset_password')) . '</p>';
echo $this->Form->hidden('User.return_to', array(
'value' => $return_to));
// echo $this->Form->end(__( 'Sing in') );
?>
<div class="row-fluid">
<?php
echo $this->Form->submit(__('Sign in'), array('class' => 'btn span6'));
echo $this->Html->link(__('Create an Account'), array('plugin' => 'users', 'controller' => 'users', 'action' => 'add'), array('class' => 'btn span6'));
?>
</div>
<p><?php
if ($_SERVER['HTTP_HOST'] !== Configure::read('webakis.touchscreen_host')) {
echo $this->Html->link(__('Forgot your password?'), array('action' => 'reset_password'));
}
?>
</p>
<div class="btn btn-facebook"><i class="fa fa-facebook"></i>
<?php echo $this->Html->link('Connect with Facebook', $fb_login_url);?>
</div></br></br>
</div><?php echo $this->Form->end(); ?>
</form>
</fieldset>
</div>
<div class="span4"></div>
</div>
I have tried to use Bootstrap "row-fluid" class to put them in one line, but it doesn't work.
It's not a linebreak as in <br>, it's that the button is being wrapped in a block element (div by default).
Either disable it
echo $this->Form->submit(__('Sign in'), array(
'class' => 'btn span6',
'div' => false
));
or use the after option to inject your other button/link into the wrapping element:
$link = $this->Html->link(__('Create an Account'), array(
'plugin' => 'users',
'controller' => 'users',
'action' => 'add'
),
array('class' => 'btn span6'));
echo $this->Form->submit(__('Sign in'), array(
'class' => 'btn span6',
'after' => $link
));
See also http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options

Wordpress MySQL Query for category pages

I am using this query on my wordpress index.php to display posts and sort them by date (oldest first ASC). I use this database query because query_posts didn't work for me for some reason.
http://pastebin.com/e7vVyKP9
Now I want to use the exact same query on my category pages. I somehow have to add some lines in order to only show posts of the category which is active.
Does anyone have a solution?
Ok so based on your information that you posted you could use something like this:
$args = array( 'post_status' => array( 'publish', 'future' ), 'post_type' => 'post','orderby' => 'date', 'order' => 'ASC' );
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
the_time('l, F jS, Y');
the_content();
the_category();
endwhile;
wp_reset_postdata();
UPDATED WITH REQUEST
get_header(); ?>
<section id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="archive-header">
<h1 class="archive-title"><?php printf( __( 'Category Archives: %s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h1>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</header><!-- .archive-header -->
<?php
$args = array( 'post_status' => array( 'publish', 'future' ), 'post_type' => 'post','orderby' => 'date', 'order' => 'ASC' );
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
the_time('l, F jS, Y');
the_content();
the_category();
endwhile;
wp_reset_postdata();
?>
<?php else : ?>
<?php endif; ?>
</div><!-- #content -->
</section><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is a twentytweleve category page that displays:
this page

cakePHP & Boostrap insert html code into form - radio - option

Is it possible to insert custom html code into a options name with Bootstraps formhelper?
<?php
echo $this->BootstrapForm->input('where', array(
'options' => array('1' => 'foo'),
'type' => 'radio'
));
?>
I would like it to be
<?php
echo $this->BootstrapForm->input('where', array(
'options' => array('1' => '<span class="italic">f</span>oo'),
'type' => 'radio'
));
?>
But when I insert the code it's being stripped from the output.
Im not sure which BoostrapForm helper you are using, but have you considered putting a flag escape as false ?
<?php
echo $this->BootstrapForm->input('where', array(
'options' => array('1' => '<span class="italic">f</span>oo'),
'type' => 'radio',
'escape' => false,
));
?>