Wordpress Post loop with query $args on search.php results? - php

I'm creating a search results page and I'm trying to exclude specific post formats. I found this query but I don't know how make the post loop works, I don't even know the code. And, I don't know how to put the 404 error not found on the search.php.
This is the code
<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-quote', 'post-format-video',
'operator' => 'NOT IN'
)
)
);
query_posts( $args );
?>
I first tried something and worked fine, but if i set "4 post per-page" it counts all the formats post, and if the standard posts on the search results should have been 3 and 1 quote-format post, it shows 3 standard post and an empty space.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_post_format() == 'quote' ) : ?>
<?php elseif( get_post_format() == 'link' ) : ?>
<?php elseif( get_post_format() == 'video' ) : ?>
<?php elseif( get_post_format() == 'image' ) : ?>
<?php else : ?>
post
<?php endif; ?>
<?php endwhile; else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>

This is the whole code of my search.php. This was the only way to not display other formats post on my search results. I just want the loop to not count "other formats post" on my results. I want to show 4 results per-page and has to be 4 standard posts format.
<?php
get_header(); ?>
<div id="content" class="site-content">
<div id="news-page">
<div class="right">
<?php get_sidebar(); ?>
</div>
<div class="left">
<div class="newstitle">RISULTATI</div>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_post_format() == 'quote' ) : ?>
<?php elseif( get_post_format() == 'link' ) : ?>
<?php elseif( get_post_format() == 'video' ) : ?>
<?php elseif( get_post_format() == 'image' ) : ?>
<?php else : ?>
<a href="<?php the_permalink(); ?>" title="Leggi">
<div class="post">
<div class="data">
<span class="giorno"><?php the_time('j M Y') ?></span> <span class="commenti"><?php comments_number('0 commenti', '1 commento', '% commenti'); ?></span>
</div>
<div class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'news-page' ); } ?></div>
<div class="thumb-full"><?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'news-page-full' ); } ?></div>
<div class="title"><?php echo get_the_title( $post_id ); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<div class="diss"></div>
<div style="position: absolute; z-index:999; bottom: 0; left: 0; font: normal 10px arial; padding: 3px;"> In <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></div>
</div>
</a>
<?php endif; ?>
<?php endwhile; else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
</div>
</div>
</div><!-- #primary -->
<?php get_footer(); ?>

Related

only displays certain category

I'm building my own website just for fun and for learning.
I want to display only the post with a certain category # my homepage.
I've tried a number of things but they don't seem to work
at my home.php
this is what te code looks like.
get_header(); ?>
<?php $blog_layout = get_theme_mod('blog_layout', 'small-images'); ?>
<?php if ( ($blog_layout == 'masonry') || ($blog_layout == 'fullwidth') ) {
$layout = 'fullwidth';
} else {
$layout = '';
} ?>
<?php if ( $blog_layout == 'masonry' ) {
$masonry = 'home-masonry';
} else {
$masonry = '';
} ?>
<div id="primary" class="content-area <?php echo $layout; ?>">
<main id="main" class="site-main" role="main">
<span class="diensten">ONZE DIENSTEN..</span>
<div class="diensten-content">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<div class="home-wrapper <?php echo $masonry; ?>">
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( $blog_layout == 'large-images' || $blog_layout == 'masonry' ) {
get_template_part( 'content', 'large' );
} else {
get_template_part( 'content', get_post_format() );
}
?>
<?php endwhile; ?>
</div>
<?php moesia_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!--diensten-content-->
<
</main><!-- #main -->
</div><!-- #primary -->
And at content.php
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
I've tried this..`
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
did not work. How do I fix this?`If It displays only the posts with a certain class if also good.
Thanks..

Flex-Flow Column Wrap - Icon with Header

I am trying to recreate a design using Advanced Custom Fields and Hard Code. I am using flex-box as CSS and would like to have the icon above text with a row of 6 columns.
Currently, my code is putting the icon on the left and the text beside it rather than reading it as a column and then a row.
Thank you for your help :)...
<div class="wrapper">
<section id="process">
<?php if ( have_rows( 'procces' ) ) : ?>
<?php while ( have_rows( 'procces' ) ) : the_row(); ?>
<h1><?php the_sub_field( 'process_header' ); ?></h1>
<p><?php the_sub_field( 'process_description' ); ?><p>
<?php if ( have_rows( 'icon' ) ) : ?>
<?php while ( have_rows( 'icon' ) ) : the_row(); ?>
<div id="process-icon" class="icon-item">
<?php $icon_image = get_sub_field( 'icon_image' ); ?>
<?php if ( $icon_image ) : ?>
<img src="<?php echo esc_url( $icon_image['url'] ); ?>" alt="<?php echo esc_attr( $icon_image['alt'] ); ?>" />
<?php endif; ?>
<h2><?php the_sub_field( 'icon_description' ); ?></h2>
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</section>
</div>
.icon-item {
display:flex;
flex-flow: column row;
}
Flex will work on the direct children of the item with the property so you just want to wrap all your .icon-item into a .icon-wrapper and apply the flex to that:
<?php if ( have_rows( 'icon' ) ) : ?>
<div class="icon-wrapper">
<?php while ( have_rows( 'icon' ) ) : the_row(); ?>
<div class="icon-item">
<?php $icon_image = get_sub_field( 'icon_image' ); ?>
<?php if ( $icon_image ) : ?>
<img src="<?php echo esc_url( $icon_image['url'] ); ?>" alt="<?php echo esc_attr( $icon_image['alt'] ); ?>" />
<?php endif; ?>
<h2><?php the_sub_field( 'icon_description' ); ?></h2>
</div><!-- close .icon-item-->
<?php endwhile; ?>
</div><!--- close .icon-wrapper-->
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
I'd remove that id="process-icon" on any multiple items too.. use classes for multiple & id for singular elements (I usually just use IDs for javascript reference)
Then your css would be:
.icon-wrapper {
display: flex;
}
.icon-item {
}

Infinite scrolling of WP categories doesn't load all elements

I set up a site for a photographer & she wanted to use her blog categories as her portfolio, which works fine, except for the fact that the infinite scrolling feature I'm using won't load all of the content for the excerpts.
The two items that it omits are the post thumbnail (image) and the horizontal row serving as a separator between the posts.
Any insight on this is appreciated - I'm using Paul Irish's infinite scroll plugin & the category excerpts are being called like so:
<?php get_header(); ?>
<section id="content" role="main">
<header class="header">
<h1 class="entry-title"><!-- <?php _e( 'Category Archives: ', 'themename' ); ?> --> <?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php get_template_part( 'entry' ); ?>
<hr />
<?php endwhile; endif; ?>
<?php get_template_part( 'nav', 'below' ); ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is the entry template:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<?php if ( is_singular() ) { echo '<h1 class="entry-title">'; } else { echo '<h2 class="entry-title">'; } ?><?php the_title(); ?><?php if ( is_singular() ) { echo '</h1>'; } else { echo '</h2>'; } ?><?php edit_post_link(); ?>
<?php if ( !is_search() ) get_template_part( 'entry', 'meta' ); ?>
</header>
<?php get_template_part( 'entry', ( is_archive() || is_search() ? 'summary' : 'content' ) ); ?>
<?php if ( !is_search() ) get_template_part( 'entry-footer' ); ?>
</article>
For reference, this is how I'm calling the excerpts for the blog, which have no issue:
<section class="entry-content">
<div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php the_excerpt(); ?>
<em><p>
<?php comments_number( '0 comments', '1 comment', '% comments' ); ?>.
</p></em>
<div class="entry-links"><?php wp_link_pages(); ?></div>
</section>
<hr />
Link to example
Move <div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div> and <hr> into the entry template.

WordPress pagination doens't work on home

I created a WordPress template file with latest posts and pagination. But when I set this page as the home page, the pagination wont work and it'll display the first page. As a standalone page it does work. Strange! Any thoughts about this?
<?php get_header(); ?>
<div id="primary" class="content-area clr">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article class="homepage-wrap clr">
<?php
/**
Post Content
**/ ?>
<?php if ( get_the_content() !== '' ) { ?>
<div id="homepage-content" class="entry clr">
<?php the_content(); ?>
</div><!-- .entry-content -->
<?php } ?>
<?php
/**
Features
**/
$wpex_query = new WP_Query(
array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'features',
'posts_per_page' => '-1',
'no_found_rows' => true,
)
);
if ( $wpex_query->posts ) { ?>
<div id="homepage-features" class="clr">
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-features', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-features -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Portfolio
**/
$display_count = get_theme_mod('wpex_home_portfolio_count', '8');
$wpex_query = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => $display_count,
'no_found_rows' => true,
'tax_query' => wpex_home_portfolio_taxonomy(),
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-portfolio" class="clr">
<h2 class="heading"><span><?php _e( 'Recent Work', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-portfolio', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
'posts_per_page' => 4
);
query_posts($args);
?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date"><?php _e('Posted on','wpex'); ?> <span class="meta-date-text"><?php echo get_the_date(); ?></span> </li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
<?php wp_pagenavi();?>
</div><!-- #homepage-portfolio -->
<?php wp_reset_query(); ?>
<?php endif; ?>
</article><!-- #post -->
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I might have found an answer for this myself, apparently it's different when you use pagination on the frontpage. I used this and it seems to be working!:
if( is_front_page() ){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
} else {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

Wordpress Blog page only showing one post

Im having an issue with a theme i've been customizing. My blog post only shows ( 1 ) post and i cant seem to fix the issue in Admin > Reading > Blog pages show at most. The Value only stays at "1" even after save. The code i have here is in the Loop.php
<?php ?>
<article class="primary-content">
<?php $firstClass='first-post' ; ?>
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article role="main" class="the-content">
<h1><?php _e( '404 - I'm sorry but the page can't be found' ); ?></h1>
<p>Please try searching again or head back to the homepage.</p>
</article>
<?php endif; ?>
<?php ?>
<?php if (is_home()): ?>
<h1>
<?php if ( is_day() ) : ?><?php printf( __( '<span>Daily Archive</span> %s' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : ?><?php printf( __( '<span>Monthly Archive</span> %s' ), get_the_date('F Y') ); ?>
<?php elseif ( is_year() ) : ?><?php printf( __( '<span>Yearly Archive</span> %s' ), get_the_date('Y') ); ?>
<?php elseif ( is_category() ) : ?><?php echo single_cat_title(); ?>
<?php elseif ( is_search() ) : ?><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php elseif ( is_home() ) : ?>Blog<?php else : ?>
<?php endif; ?>
</h1>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* How to display standard posts and search results */ ?>
<article class="article-archive <?php echo $firstClass; ?>" id="post-<?php the_ID(); ?>">
<?php $firstClass="" ; ?>
<?php ?>
<?php if (is_front_page()) { ?>
<div class="home-summary">
<?php } else { ?>
<div class="entry-summary">
<?php } ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_post_thumbnail( 'flozo-thumb');?>
</a>
<?php if (is_front_page()) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h2><?php the_title(); ?></h2>
<?php } ?>
<?php the_excerpt(); ?>
<?php if ( is_home() ) : ?>
<p class="entry-meta">
<time datetime="<?php the_time('l, F jS, Y') ?>" pubdate>
<?php the_time( 'l jS F Y') ?>
</time>
</p>
<?php endif; ?>
</div>
</article>
<?php /*?>
<?php comments_template( '', true ); ?>
<?php */?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="navigation">
<div class="nav-previous">
<?php next_posts_link( __( 'Older posts' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts' ) ); ?>
</div>
</div>
<!-- #nav-below -->
<?php endif; ?>
</article>
Any and all help with be greatly appreciated! Sorry if this was posted already.
L

Categories