Customize search results by category - php

I have different categories where the post displayed are characterized by different elements, for this reason when I use the search in wordpress the results are not shown very well.
For this reason I would like to change the appearance of the post shown in the search results based on its category.
Example:
My search.php template look like this:
<?php get_header(); ?>
<div id="content">
<div class="padder">
<?php do_action( 'bp_before_blog_search' ); ?>
<div class="page" id="blog-search" role="main">
<?php if (have_posts()) : ?>
<?php bp_dtheme_content_nav( 'nav-above' ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php do_action( 'bp_before_blog_post' ); ?>
<div class="blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
<div class="clear"> </div>
<?php do_action( 'bp_after_blog_post' ); ?>
<?php endwhile; ?>
<?php bp_dtheme_content_nav( 'nav-below' ); ?>
<?php else : ?>
<h2 class="center"><?php _e( 'No posts found. Try a different search?', 'OneCommunity' ); ?></h2>
<?php endif; ?>
<div style="display:inline">
<center><?php wp_pagenavi(); ?></center>
</div>
</div>
<?php do_action( 'bp_after_blog_search' ); ?>
</div><!-- .padder -->
</div><!-- #content -->
<div id="sidebar">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-blog')) : ?><?php endif; ?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-ad-blog')) : ?><?php endif; ?>
</div><!--sidebar ends-->
<?php get_footer(); ?>
I tried to achieve the result of displaying based on the post category a different "template" for the post but without success.
I asked the same question on wordpress.stackexchange.com but without success, and there I pointed out a non working solution, hope it can help you in developing the code I need.
https://wordpress.stackexchange.com/questions/141856/different-layout-on-search-page-depending-on-category-post
Thank you very much for any help you will give, and excuse me for my english.

If I understand your question correctly, you could simply use the post_class() function:
<div <?php post_class( 'blog-post' );?> >
// Here is displayed the blog post style and features
</div><!--blog-post-->
This will render like:
<div class="post-3654 post type-post status-publish format-standard hentry category-buildings blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
for posts in the buildings category.
Then your CSS will take care of the rest:
.post.category-buildings {
border: 3px solid red;
}
But I think your setup/idea is only suitable for single category posts.
Ref: From the Codex:
The post_class may include one or more of the following values for the
class attribute, dependent upon the pageview.
.post-id
.post
.attachment
.sticky
.hentry (hAtom microformat pages)
.category-ID
.category-name
.tag-name
.format-name

Related

how to get full width in my wordpress page template?

I have to create a full-width page template in Wordpress. But I don't understand why, even if I only call my 'sidebar-bas' I also have the 'sidebar-droite' displayed :/
i hope someone could help me
<?php
/*
Template Name: Full Width
*/
?>
<?php get_header();
if(have_posts()): while(have_posts()): the_post();?>
<section id="pages" class="haut avecAside">
<section id="pages" class="haut">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile;
endif;
get_sidebar( 'sidebar-bas' );
get_footer();?>
Verify that your sidebar-bas template file does not include get_sidebar( 'sidebar-droite' ) in it.

HTML only print once in Wordpress Loop

I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!

3-column grid breaking after certain point

I have a WP blog with a theme based on F5 (Reverie). Currently using a child theme for handling the custom styles, etc.
In addition, I'm using Infinite Scroll (part of JetPack plugin) to manage pagination on home and archived pages.
The issue
It looks fine on the initial loading of the first set of posts (set to 6). When I click the button to trigger infinite scroll, six more load (still looks fine). Load another and the 3-column grid starts to break (lose the float). At first I thought it might be the images using for the post. I even tried deactivating some plugins I thought could be an issue; no luck.
I have also tried adjusting the css (height, min-max) of the posts to see if helps. No go.
home.php
<div class="row" id="content-home">
<div class="small-12 columns" id="content" role="main">
<h5 style="margin-left:15px;">Latest Posts</h5>
<?php query_posts('offset=1'); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; // end have_posts() check ?>
</div>
</div>
content.php
<article id="post-<?php the_ID(); ?>" <?php post_class('index-card small-12 large-4 columns'); ?>>
<header>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail('large'); } ?>
</header>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php bvblog_small_meta(); ?>
</div>
.row .small-12.columns#content article {
display: inline-block;
float: left !important;
min-height: 540px;
}

Custom Post Meta Search Template

I'm struggling with getting wordpress to search custom post meta from a page template. I've looked all over the internet and can't seem to find anything that will work. No plugins seem to work either.
On my posts, I have the custom meta: "rate" and the value: "10" - Wordpress delivers no result when searching any of these.
I'd be very appreciated if someone could write me a searchpage.php page template or point me in the right direction (I'm not good with php).
Here's my current PHP code:
<?php
/*
Template Name: Search Custom Meta
*/
?>
<?php get_header(); ?>
<div id="content">
<div class="container clearfix fullwidth">
<div id="left-area">
<?php query_posts('meta_value='.$s); ?>
<?php if (!empty($wp_query->posts)) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</div> <!-- end #left-area -->
</div> <!-- .container -->
</div> <!-- #content -->
<?php get_footer(); ?>
You checking incorrect variable in if so try removing that one,
query_posts('meta_value='.$s);
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

How to get custom categories template?

I have a categories page in Wordpress and it has its own template categories.php, which is actually a static page with boxes that link to specific categories. This is a shortened code:
<?php
/*
Template Name: Categories
*/
?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-categories">
<h5 class="inner_text_shadow">Categories & Tags</h5>
<div id="clear-box">
<div id="cc" class="c1"><h6>The Fall Locations</h6></div>
<div id="cc" class="c2"><h6>Info</h6></div>
<div id="cc" class="c3"><h6>Budget</h6></div>
</div> <!-- END main-content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
CSS doesn't matter.
My question is - which PHP file/template is used for those posts that are in certain category like www.mywebsite.com/category/budget? When I click any of the boxes I do get the posts that are in that category but there's no formatting, just the header, sidebar and footer. What is the actual code for "posts from category" and what PHP file do I input it to?
[Same question goes for tags. (www.mywebsite.com/tag/food)]
The category.php can handle all categories. See my example below:
<?php get_header(); ?>
<?php
//Get Category
$category = get_category(get_query_var('cat'));
?>
<!-- Start Loop -->
<?php query_posts('category_name=' . $category->cat_name . '&paged='. get_query_var('paged')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- Loop Code Here -->
<?php endwhile; else: ?>
<p>An Error Occurred</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
<!-- End Loop -->
<?php get_footer(); ?>
You can also have separate files for each category in the format category-slug.php. In your case you would have category-locations.php, category-info.php & category-budget.php
Hope this helps.

Categories