Wordpress MySQL Query for category pages - mysql

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

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>

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

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.

yii.validation is not defined error in yii2

I am opening a ActiveForm in modal window in yii2.
This error comes when I am opening a ActiveForm in Modal window . my index.php is
<?php
use yii\helpers\Html;
use yii\bootstrap\Modal;
$this->title = 'Roles';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="role-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Role', '#', [
'class' => 'btn btn-success',
'id' => 'create-role-model',
'data-toggle' => 'modal',
'data-target' => '#activity-create-modal',
]) ?>
</p>
</div>
<?php
Modal::begin([
'id' => 'activity-create-modal',
'header' => '<h2>Hello world</h2>',
'footer' => Html::button('Close', ['class' => 'btn btn-default', 'data-dismiss' => 'modal'])
. PHP_EOL . Html::button('Add', ['class' => 'btn btn-primary btn-modal-save']),
]);
$model= new \frontend\models\Role();
echo $this->renderAjax('_form',['model' => $model]);
Modal::end();
?>
and my _form.php is
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* #var $this yii\web\View */
/* #var $model frontend\models\Role */
/* #var $form yii\widgets\ActiveForm */
?>
<?php $form = ActiveForm::begin([
'id' => 'create-ro',
'enableClientValidation' => true,
]); ?>
<?= $form->field($model, 'id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'created_on')->textInput() ?>
<?= $form->field($model, 'updated_on')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
However when I place ActiveForm without render in index.php Yii Validation is working.Am I doing it an wrong manner . What have to be done so that modal validation start
It's echo $this->render('_form',['model' => $model]); you need, not $this->renderAjax().
The latter actually terminates the entire page and returns it without a layout around it (so including any javascript includes and so on).
Since you are still in another view that is definitely not what you need. So just use render(). It's a bit confusing because Controller::render() actually does close the page (and also adds javascripts), but in case of the View (Which $this is in that context) it does what renderPartial does for the controller.

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

How to add sidebars in footer of the wordpress(version 3.0) theme?

I want to add sidebars or widgets to wordpress theme, I tried many tuts online but they failed because they are to outdated. My website link is here
http://lifetothebrim.com
I want to add sidebar in three column layout.
Thank you
How to add a sidebar in the footer of a WordPress theme
Step 1.
Register your sidebars in functions.php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Footer Widgets Left',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Footer Widgets Center',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Footer Widgets Right',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
Step 2.
Create a template file and name it sidebar-footer.php and include the call to your sidebar
<div class="footer-left>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widgets Left') ) : ?><?php endif; ?>
</div>
<div class="footer-center">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widgets Center') ) : ?><?php endif; ?>
</div>
<div class="footer-right">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widgets Right') ) : ?><?php endif; ?>
</div>
Note: for styling purposes you should wrap the above function call in a div
I broke it into 3 widget areas for you with the css clases "footer-left", "footer-center", and "footer-right"
You will have to add the styles to display them in your css.
Example: clear any floated divs that come before this.
.footer-left {width:300px;float:left;} .footer-center {width:300px;float:left;} .footer-right {width:300px;float:left;}
make sure the next div clears:both
Step 3.
In your footer.php or at the bottom of any of your templates add
<?php get_sidebar('footer'); ?>
codex it's not outdated http://codex.wordpress.org/Function_Reference/get_sidebar, this might help you.