I have looked everywhere and I cannot figure this out.
I am wanting my category name as a H1 tag to be above the product listings on the category page.
I found a solution. By adding the code below to the file
plugins/woocommerce/templates/archive-product.php
<?php if ( is_product_category() || is_product() ) : ?>
<div class="page-header-image generate-page-header category-header">
<div class="grid-container"><h1><?php single_term_title(); ?></h1></div>
</div>
<?php endif; ?>
Related
I'm building my own template for Wordpress and I'm bumping into an issue when displaying the content of a post.
I have already built one page template for the home page and it works fine. The loop outputs what I want to display. Now I'm building the template to display an article but the loop doesn't return anything.
Here is the code of the page template:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
This results in displaying the "sorry, no posts matched your criteria" message when i click on one of the links on the homepage. The strange thing is that the page really exists (it has ID=26 as you'll see here below):
<?php
$post = get_post(26);
$title = $post->post_title;
echo $title;
?>
This works and displays the expected title. I have tried get_the_ID(); to get the post ID but it returns an empty variable.
There is probably something missing in my template but I can't figure out what.
Any idea?
Thanks
I have found the issue and it had nothing to do with the template itself.
I discovered what's wrong by using one of the standard Wordpress themes (twentyfifteen) where every post led me to a 404 even if I clicked on a post from the Admin UI. I swapped back permalink structure to the ?p=123 option and there everything working. Definitely a permalink structure problem.
The problem was coming from the polylang plug-in. I'm using a network of wordpress site for which I have made this plug-in available. I did not need this plug-in for this particular site but somehow it needed to be active anyway. So I activated polylang and configured it to have only one language and now it works.
Got new gray hair in the process but if that can help anyone...
Thanks for your help!
Laurent
I don't think, that this template can output any posts like this. For WordPress
/*
Template Name: PAGE
*/
indicates: this is a page-template which you can asign to the content of a page but not to a post.
When you want to show a specific post on such a page you will have to:
Asign the template to the page in WP-backend
Add a new query to the template to show the post
Like:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
// The new Query
$args = array (
'p' => '26',
);
// The Query
$query = new WP_Query( $args );
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
But as this is very complicated I would recommend to:
delete the /* Template Name: PAGE */ from the template
save it as single.php or more specific as single-26.php
link to the desired post in your wp-menu
i would like to have a sidebar appear in a single posts for a specific category. I not great at php so this is what i have been able to come up with. It does not work of course lol. Your help will be very much appreciated!
<?php if( in_category('9') ) : ?>
<div>
<h1>Other Products I Recommend</h1>
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('products2')) : ?>
[ do default stuff if no widgets ]
<?php endif; ?>
</div>
<?php endif; ?>`
There are two options for that:
There is a plugin that can help you with this. It's called jetpack. When it is installed and activated, when you add any widget to a sidebar there will be a visibility option, click that and choose which pages/categories/etc to show the widget it.
Code:
As for your code:
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('products2')) : ?>
that says if the dynamic_sidebar function doesn't exists or if it's not the products2 sidebar then do X. That function will exist as sidebars are set in that function, so nothing inside that if statement will run.
You will want something like:
<?php if( in_category('9') ) : ?>
<div>
<h1>Other Products I Recommend</h1>
<?php dynamic_sidebar('products2'); ?>
</div>
<?php endif; ?>
that should display the contents of what you have added to the products2 sidebar only when you are in a post that has a category id of 9.
I have three Posts in WordPress that each have the category Event.
I'm assuming that i use a WordPress codex to call those Posts to display when i want to.
What is happening is these Posts are all being displayed on index.php. I want to be able to use the WordPress codex and get the Posts for a certain category and display them. For example, display the posts in a pop up div.
How do i get Posts not to be displayed on the front page?
How do i get Posts of a specific category to display?
Or am i going about this the wrong way? I assume that Posts can be fetched from the database and be put in a popup div.
Here is my main inner html of my index.php
<div id="main">
<div id="nav">
<?php
if(!isset($_GET['page_id'])) {
wp_nav_menu( array( 'theme_location' => 'main-menu'));
}
?>
</div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
I want it to fetch the current Page content and not the Posts.
<?php
if(!is_home() || !is_front_page) { // dont display on home page
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif;
}
?>
look here for more conditional tags template tags
and to get posts from a particular category you can pass args in wp_query look here category args
I'm trying to create a related post section in my wordpress custom theme. The objective here is if the user views a post to a certain category it will also show all the post from that category below. I have attached an image below.
http://i255.photobucket.com/albums/hh140/testament1234/relatedposts_zpsa830adfc.jpg
How do i achieve this? Do i use wp_query just like this?
<?php query_posts('cat=6', 'posts_per_page=-4=-'); if(have_posts()) : while(have_posts()) :the_post(); ?>
The code above sets what category it obtains the post. 'cat'
Solution of kwncc
<div id="post-container" class="eleven columns alpha omega post">
<?php $postCategories = ''; while ( have_posts() ) : the_post(); ?>
<h2 class="post-title"><?php the_title() ?></h2>
<?php setPostViews(get_the_ID()); ?> <!-- Set Post Views -->
<ul class="meta-icons-large">
<li id="meta-author-large"><span><?php the_author_posts_link() ?> /</span></li>
<li id="meta-categories-large"><span><?php the_category(', ') ?> /</span></li>
<li id="meta-comments-large"><span><?php comments_number() ?> /</span></li>
<li id="meta-date-large"><span><?php the_time('F jS, Y') ?> /</span></li>
<li id="meta-views-large"><span><?php echo getPostViews(get_the_ID()); ?></span></li> <!-- Display Post Views -->
</ul>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div id="related-posts-container" class="eleven columns alpha omega related-post">
<h2>related posts</h2>
<?php $postCatIds = ''; foreach($postCategories as $catIndex => &$catValue){
$postCatIds .= $category->cat_ID; if( $catIndex < (count($postCategories)-1)){ $postCatIds .= ', ';
}
}
?>
<?php query_posts('cat='.$postCatIds, 'posts_per_page=4'); if(have_posts()) : while(have_posts()) { the_post(); } ?>
<div id="related-post-thumbnail-container" class="three columns alpha related-posts">
<h3><?php the_title(); ?></h3>
</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<div id="comments-container" class="eleven columns alpha omega comments">
<?php comments_template('', true); ?>
</div>
</div>
I'd suggest you don't load related posts on page load because this will dramatically hurt your website's performance.
The free WordPress plugin Related Posts for WordPress automatically finds related posts (amongst others based on title) and caches them for you, offering you real related posts without hurting your website's performance. After the automatic linking is done you can, if required, manually add, edit or delete related posts. The plugin also comes with template tags so you can display the related posts anywhere in your custom theme you want. Simply use rp4wp_children() in your theme where you want the related posts to be displayed.
You can give it a try via the WordPress.org repo: http://wordpress.org/plugins/related-posts-for-wp/
This page consists of 2 different loops:
1. for the main content - article of the post
2. for the related posts.
For the first part, you have just to include the loop. In addition, since you want to find the related to this article posts you have to also get the current post categories.
PHP code:
$postCategories = '';
if(have_posts()) : while(have_posts()){
the_post();
$postCategories = get_the_category();
...
}
Then for the second part where you want to include the related posts you can get the category IDs related to the current post and create the query you need.
// get comma separated category IDs
$postCatIds = '';
foreach($postCategories as $catIndex => &$catValue){
$postCatIds .= $category->cat_ID;
if( $catIndex < (count($postCategories)-1)){
$postCatIds .= ', ';
}
}
//query the related categories posts
query_posts('cat='.$postCatIds, 'posts_per_page=4');
if(have_posts()) : while(have_posts()) {
the_post();
...
}
Hope that helps!
my problem is that I can navigate between Blog pages and posts but I cant navigate between the category pages.
My code looks like that:
<div class="previous-page">
<?php
if(is_single()){
previous_post_link( '%link', __( '<div title="%title">‹</div>') );
} else {
previous_posts_link( __('‹') );
}
?>
</div>
and I have the same code for the next_post/next_posts.
I thougt that the previous code also covers the category.
So what I am missing?
Greetings and Thanks
Chris
Try using this code which I currently use for all my themes:
This goes in your functions.php file:
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}
Then in your template files use this to show the navigation:
<?php if (show_posts_nav()) : ?>
<div class='navigation'>
<?php next_posts_link('« Older Entries'); ?>
<?php previous_posts_link('Newer Entries »'); ?>
</div>
<?php endif; ?>