i've got a own wordpress template (still in progress). It includes of course search.php template which looks like this:
<?php
get_header(); ?>
<section class="row page_intro">
<div class="row m0 inner">
<div class="container">
<div class="row">
<h5><?php
/* translators: %s: search query. */
printf( esc_html__( 'Search Results for: %s', 'vetsandpets' ), '<span>' . get_search_query() . '</span>' );
?></h5>
<h1><?php _e('News and veterinary advices', 'vetsandpets'); ?></h1>
</div>
</div>
</div>
</section>
<section class="row breadcrumbRow">
<div class="container">
<div class="row inner m0">
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('
<p id="breadcrumbs">','</p>
');
}
?>
</div>
</div>
</section>
<section class="row content_section">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 blog_list">
<?php
global $post;
setup_postdata( $post );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'posts_per_page' => '6',
'paged' => $paged
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="row m0 blog blog2">
<div class="image_row row m0">
<?php the_post_thumbnail('looppostthumbnail', array( 'class' => "img-responsive loop-post-image")); ?>
</div>
<h3><?php echo get_the_title(); ?></h3>
<div class="row m0 meta"><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></div>
<p><?php echo excerpt(50); ?></p>
<?php _e('Read more', 'vetsandpets'); ?>
</div> <!--Single Post-->
<?php endwhile; ?>
<?php echo wpse247219_custom_pagination(); ?>
<?php else : ?>
<div class="center"><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></div>
<?php endif; wp_reset_postdata(); ?>
</div>
<div class="col-sm-12 col-md-4 sidebar">
<div class="row m0 widget categories">
<h5 class="widget_heading"><?php _e('Categories', 'vetsandpets'); ?></h5>
<ul class="list-unstyled">
<?php
$args = array(
'orderby' => 'count',
'depth' => 0,
'title_li' => '',
'use_desc_for_title' => '',
'order' => 'DESC',
'hide_empty' => 0
);
wp_list_categories($args);
?>
</ul>
</div>
<div class="row m0 widget recent_posts">
<h5 class="widget_heading"><?php _e('Recent posts', 'vetsandpets'); ?></h5>
<?php
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="media recent_post">
<div class="media-left">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('recentpostthumbnail', array( 'class' => "img-responsive recentpostimage")); ?>
</a>
</div>
<div class="media-body">
<h5><?php the_title(); ?></h5>
<p><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</section>
<?php
get_sidebar();
get_footer();
?>
And that's it. Then I have a search form which is included always in a navigation modal. Below you can check the php code:
<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div>
<input class="form-control" type="search" value="<?php get_search_query(); ?>" id="example-search-input" name="s" id="s" />
<button type="submit" class="btn searchbtn" id="searchsubmit"><?php _e('Submit', 'vetsandpets') ?></button>
</div>
</form>
but it doesnt work - meaning: it always displays all posts.. what im doing wrong? it is somehow linked to arguments in this code/loop?
The problem is that you reset the global $post object by the query_posts() call. As it is stated in the WordPress Docs: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.
So, you should delete these lines:
query_posts(array(
'post_type' => 'post',
'posts_per_page' => '6',
'paged' => $paged
));
The first while loop <?php while ( have_posts() ) : the_post(); ?> will already iterate over the search results.
Related
I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> »</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>
As per the image below - I have a blog archive page which shows a featured blog post as a large one and then shows all of the blog posts below that.
I would like to add something to the main blog loop to say:
IF the blog blog post is featured, don't display it here
So I can stop duplicate blog posts showing up (one featured and then the same one in the main blog loop).
I have two bits of code, one that pulls the featured post through and one that loops all of the posts through. Here
<!-- Featured Blog Item -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => 'yes'
)
);
?>
<div class="container">
<div class="row no-gutters">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-12">
<a href="<?php the_permalink(); ?>">
<div class="hero__overlay featured-grad-blog">
</div>
</a>
<div class="featured-blog-container">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
</div>
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div class="featured-blog-grid-image-container" style="background-image:url(<?php echo $feat_image; ?>);"></div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<!-- All Blog Items -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1
)
);
?>
<div class="container blog-page-container">
<div class="row blog-page-row">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-lg-4 col-sm-6 col-xs-12 blog-page-col">
<div class="blog-image" style="position: relative;">
<a href="<?php the_permalink(); ?>">
<div class="hero__overlay grad-blog-hover"></div>
</a>
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div class="blog-grid-image-container blog-page-image blog-post-image-div" style="background-image:url(<?php echo $feat_image; ?>);"></div>
</div>
<div class="blog-title">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<div class="blog-bars-outside-container">
<div class="blog-bars-inside-container">
<div class="blog-all-bar blog-bar1"></div>
<div class="blog-all-bar blog-bar2"></div>
<div class="blog-all-bar blog-bar3"></div>
<div class="blog-all-bar blog-bar4"></div>
<div class="blog-all-bar blog-bar5"></div>
</div>
</div>
<div class="blog-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="meta-details">
<p><?php the_author_meta( 'display_name', $author_id ); ?> - <?php echo get_the_date(); ?></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
I guess I would need to add the opposite of this maybe, into the array for the main blog posts:
'meta_key' => 'featured',
'meta_value' => 'yes'
But I can't get anything to work...
Any help would be greatly appreciated!
For the non-featured part, please try this:
<!-- All Blog Items -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => '!='
)
)
)
);
?>
This should select all posts where 'featured' is not equal '!=' 'yes'.
I have a problem with a custom wordpress template.
I've made a categories page. Works fine on the firs view.
But when I make click on 2nd page for older posts, the browser shows me a 404 error.
I think my query needs something more, but I don't know what is or if this is the problem.
Can somebody help, please?
This is my code:
<?php
/**
* The template for displaying Category pages
*/
?>
<?php get_header(); ?>
<?php get_template_part( 'partials/linea-content', 'page' ); ?>
<div class="index categories">
<div class="col-xs-12 pre-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-xs-12 categories-title">
<h2>Categoria: <span><?php echo get_category(get_query_var('cat'))->name; ?></span></h2>
</div>
<div id="posts" class="col-xs-12 col-sm-9">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// Cambiamos los argumentos para buscar desde la última
$args = array(
'posts_per_page' => 2,
'orderby' => 'date',
'order' => ASC,
'category__in' => get_category(get_query_var('cat'))->cat_ID,
'paged' => $paged
);
// '&orderby=date&order=ASC&category=' . get_category(get_query_var('cat'))->cat_ID;
// volvemos a crear la consulta principal
$aux = query_posts( $args );
$cont_items = 1;
?>
<?php if ( have_posts() ) : ?>
<div class="row items-row">
<div class="col-xs-12">
<?php while ( have_posts() ) : the_post(); ?>
<!-- post -->
<div id="post-id-<?php echo $post->ID; ?>" class="item col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12 text-center item-content">
<span class="meta-category">
<span class="meta-category-inner">
<?php
$cat = get_the_category($post->ID);
?>
<?php echo $cat[0]->name; ?>
</span>
</span>
<?php
global $wpdb;
$ppbv_tablename = $wpdb->prefix . 'popular_by_views';
$currentRow = $wpdb->get_row("SELECT * FROM {$ppbv_tablename} WHERE post_id = {$post->ID}");
$curView = 0;
if(isset($currentRow))
{
$curView = $currentRow->views;
}
?>
<div class="item-title-content">
<a href="<?php the_permalink(); ?>" title="Popsicase">
<h2 class="item-title col-xs-10 col-xs-offset-1"><?php the_title(); ?></h2>
</a>
</div>
<p class="date-cat">
<small class="col-xs-12">
<i class="fa fa-link"></i> <strong><?php the_author(); ?></strong> | <span><i class="fa fa-calendar"></i> <?php echo get_the_date();?> | <i class="fa fa-eye"></i> <?php echo $curView; ?></span>
</small>
</p>
<?php
$img_url = get_template_directory_uri() . '/assets/img/podcast.jpg';
if (get_the_post_thumbnail())
{
$img_url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
}
?>
<a href="<?php the_permalink(); ?>" title="Popsicase">
<div class="item-thumbnail" style="background:url(<?php echo $img_url; ?>) no-repeat center center;">
</div>
</a>
<div class="row">
<div class="col-xs-6 text-right">
<?php /*<span class="comment"> Comentarios: <?php comments_number( '0','1','%'); ?></span>*/ ?>
</div>
</div>
<div class="item-excerpt text-left">
<div class="col-xs-12">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
</div>
<?php if ($cont_items % 2 == 0) : ?>
</div>
</div>
<div class="row items-row">
<div class="col-xs-12">
<?php
endif;
$cont_items++;
?>
<?php endwhile; ?>
</div>
</div>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<?php
the_posts_pagination( array(
'mid_size' => 4,
'prev_text' => __( 'Artículos antiguos', 'textdomain' ),
'next_text' => __( 'Artículos nuevos', 'textdomain' ),
) );
?>
<?php /*
<div class="nav-previous alignleft"><?php next_posts_link( 'Artículos antiguos' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Artículos nuevos' ); ?></div>
*/ ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<div class="hidden-xs col-sm-3 sidebar">
<div class="row">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('main-sidebar')) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="col-xs-12 pos-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="clearfix"></div>
</div>
I'm trying to get 2 different styled posts to show up one after the other and then back around. So it should look like
DB
Uncategorized
DB
Uncategorized
etc
And to have them both styled differently. I'm not great with PHP and the best I got so far was all in one category and then all in the other.
<section class="home-middle">
<div class="container">
<div class="row">
<div class="col-sm-8">
<?php
$args = array('category_name' => 'designer backstage',
'post_status' => 'publish',
'posts_per_page' => '3' );
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="stylists-book">
<div class="image">
<div class="meta-designer">
<div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
<h3>Designer<hr></h3>
<p><?php the_author(); ?></p>
<span><?php the_date(); ?></span>
</div>
<img><?php the_post_thumbnail(); ?>
</div>
<?php
$args = array('category_name' => 'uncategorized',
'post_status' => 'publish',
'posts_per_page' => '3');
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="look" style="max-height: 200px">
<div class="row">
<div class="col-md-4">
<div class="team-modster">
<span><?php the_author(); ?></span>
<?php the_title(); ?>
</div>
</div>
<div class="col-md-8">
<a href="<?php the_permalink() ?>">
<img style="height:200px" src="<?php echo catch_that_image() ?>" />
</a>
</div>
</div>
</div>
<?php endwhile; else: >
Oops, there are no posts.
<?php endif; ?>
</div>
</div>
<?php get_sidebar(); ?>
</div>
</div>
</section>
A co-worker took a look at it and got it working. Figured I would post the solution.
<?php
$args = array('category_name' => 'designer-backstage',
'post_status' => 'publish',
'posts_per_page' => '5' );
$designer_posts = new WP_Query($args);
$args = array('category_name' => 'uncategorized',
'post_status' => 'publish',
'posts_per_page' => '5' );
$uncategorized_posts = new WP_Query($args);
if ($designer_posts->have_posts() && $uncategorized_posts->have_posts()) :
// If a new category is added, add it to this array
$category_posts = [$designer_posts, $uncategorized_posts];
$cat_posts_idx = 0;
$new_category_posts = [];
$post_count = 0;
$max_posts = 10;
// Alternate categories and store into a new posts array
while ($post_count < $max_posts) {
// Iterate the post
$category_posts[$cat_posts_idx]->the_post();
// get_the_category() returns an array with one item in this case
$category = get_the_category()[0];
if ($category->slug == 'designer-backstage') { ?>
<div class="stylists-book">
<div class="image">
<div class="meta-designer">
<div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
<h3>Designer<hr></h3>
<p><?php the_author(); ?></p>
<span><?php the_date(); ?></span>
</div>
<img><?php the_post_thumbnail(); ?>
</div>
<?php }
else if ($category->slug == 'uncategorized') { ?>
<div class="look">
<div class="row">
<div class="col-md-4">
<div class="team-m">
<span><?php the_author(); ?></span>
<?php the_title(); ?>
</div>
</div>
<div class="col-md-8" style="max-height: 225px; overflow: hidden;"><img style="" src="<?php echo catch_that_image() ?>" /></div>
</div>
</div>
</div>
<?php }
else:
?>
Oops, there are no posts.
<?php
endif;
?>
I'm working on a Wordpress site. I'm trying to make a category with the id 968 show up on the Wordpress site under the stories and successes div. However for some reason the stories and successes div is not having the content from the category inserted into it. Any information on how I can improve my query or PHP is much appreciated. If I need to update this post with more information please let me know. Below you will see the php template file I am using.
<?php
/**
* Template name: Random Template
*
*
*/
// Custom styles
wp_enqueue_style('stories-styles', get_bloginfo('stylesheet_directory') . '/assets/css/stories.css');
// TEMP -- Get the category from the slug
the_post();
$categorySlugs = array(
'religious-freedom' => 'religious-freedom',
'social-and-economic-justice' => 'social-and-economic-justice',
'civil-and-human-rights' => 'civil-and-human-rights'
);
// Get category
$categorySlugs = $categorySlugs[$post->post_name];
$storyQuery = new WP_Query(array(
'post_type' => array('stories', 'press-releases'),
'category_name' => $categorySlugs,
'posts_per_page' => 4,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
$successesQuery = new WP_Query(array(
'post_type' => 'stories',
'cat' => 968,
'posts_per_page' => 1,
'offset' => 5
));
// Remove yarpp
remove_filter('the_content', array($yarpp, 'the_content'), 1200);
?>
<?php get_header(); ?>
<div class="category-title">
<div class="container">
<h2><?php the_title() ?></h2>
</div>
</div>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
<article class="featured-issue-article">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('full', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
</div>
<?php endif; ?>
<div class="content">
<?php the_content() ?>
</div>
</article>
</div>
</div>
<hr class="large blue" />
<div class="row">
<div class="col-md-8">
<ul class="section-list">
<?php while ($storyQuery->have_posts()) : $storyQuery->the_post(); ?>
<li>
<h3><?php the_title() ?></h3>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
<?php endif; ?>
<?php if( get_field('custom_excerpt', $values_id)): ?>
<p><?php echo the_field('custom_excerpt'); ?></p>
<?php endif; ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
<?php
set_query_var('category_slug', $categorySlug);
get_template_part('issues-nav', 'bottom')
?>
<hr class="small blue" />
<div class="success-stories">
<h2>Stories & Successes</h2>
<ul>
<?php while ($successesQuery->have_posts()) : $successesQuery->the_post(); ?>
<li>
<h4>
<?php the_title() ?>
</h4>
<?= strip_tags($post->post_excerpt) ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>