How to get custom categories template? - php

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.

Related

WordPress Tag archive not loading content

I working on a custom theme and the Tag archive (tag.php) doesn't work. The page loads the header and footer but the content section is blank. I know the template is correct because when I put general text above or below the loop I can see it on the page. The index.php, archive.php, and category.php work correctly.
Here's the code in my tag.php (this code loads posts on the other templates)
<?php
if (!defined('ABSPATH')) exit;
get_header();
?>
<div class="container py-5">
<div class="row">
<div class="col">
<?php if( have_posts()) : while (have_posts()) : the_post() ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_sidebar();?>
</div>
</div>
<?php get_footer();?>
The permalink structure for the tag archive is the default - www.website.com/tag/tag-name
Any help is greatly appreciated.

Blog Main Header Showing Recent Post Title Instead

My Wordpress blog works fine on the main page that shows all posts, however, when I click a category the most recent post's title becomes the main header and that post doesn't even show up in the blog post list of that category.
https://ibb.co/ebReRV
I spied on other people with the same theme as me and they have the same problem so I believe this is a problem with the original code. My theme's creator seems to have disappeared and hasn't responded to any of my messages.
archive.php
<section id="wp-main-content" class="clearfix main-page title-layout-standard">
<?php do_action( 'naturalfood_before_page_content' ); ?>
<div class="container">
<div class="main-page-content row">
<!-- Main content -->
<div class="content-page <?php echo esc_attr($main_content_config['class']); ?>">
<div id="wp-content" class="wp-content">
<?php get_template_part('templates/layout/archive') ?>
</div>
</div>
You Need to Create Archive Page
Give Archive Page name is "archive.php".
Archive Page Structure Like this:
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
/* Custom Archives Functions Go Below this line */
/* Custom Archives Functions Go Above this line */
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Customize search results by category

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

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;

Logged out sidebar weirdness

When I'm logged in to my site, viewing an individual post, the right sidebar displays perfectly, but when I log out on the same page (via the sidebar widget), the right sidebar suddenly ends up below my comments. This only happens with I'm viewing individual posts. Any ideas what causing this?
I've double checked the CSS, and as far as I can tell being logged out doesn't add or change any class attributes. Also - the theme I'm working with doesn't have a posts.php file...
Post: http://www.wespeakfashion.com/cool-sunglasses
page.php...
<?php include (TEMPLATEPATH . '/header.php'); ?>
<div id="content">
<?php include(TEMPLATEPATH."/l_sidebar.php");?>
<div id="contentleft">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read more'));?><div style="clear:both;"></div>
<!--
<?php trackback_rdf(); ?>
-->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
<?php posts_nav_link(' — ', __('« go back'), __('keep looking »')); ?>
</div>
<?php include(TEMPLATEPATH."/r_sidebar.php");?>
</div>
<!-- The main column ends -->
<?php get_footer(); ?>
The right sidebar appears to be in the main content div. This is suspicious because the left sidebar is a sibling of the main content div. Try taking the right sidebar out of there, and putting it as a sibling of the left sidebar and main content div.

Categories