Wordpress 3.6: Page under category - php

The Problem:
My menu bar contains a set of categories. Posts automatically align. A new Plugin is based on Pages. The Page e.g. 'p1' is always on the main site.
The Idea:
Create a template that assigns a page to a specific category e.g. 'p1' -> 'c1'. I found a piece of code
<?php if (is_category('c1')) : ?>
The problem is that I don't know how to tell the program:
if (is_category('c1')) : show page / vice versa?>
How do I do that?

You will want a naming standard between pages and categories. Once that is done you can do the following (or something like it):
<?php if(is_category('c1)) : ?>
<?php query_posts('category_name=c1&order=asc');
if ( have_posts() ) : while( have_posts() ) : the_post();?>
<div class="pageWrapper">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; else: ?>
<p>No content was found</p>
<?php
endif;
wp_reset_query();//If writing your own queries, always a good idea to reset it. ?>

Related

ARCHIVE.PHP SHOWS ONLY ONE POST PER CATEGORY

I realized my website (www.inunfuturoaprile.it) with a minimal Wordpress theme that does not include the category.php template. I have created the template by me: the goal was to get pages that display a list of the posts belonging to a specific category. Unfortunately, due to my scarce knowledge of php, the code does not seem to work properly: I only see one post per category (e.g. https://www.inunfuturoaprile.it/politica/).
Here's the category.php file code:
<?php
/**
* Template di Categoria
*/
get_header(); ?>
<div id="content">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// Since this template will only be used for Design category
// we can add category title and description manually.
// or even add images or change the layout
?>
<h1><strong><?php single_cat_title(); ?></strong>: Elenco Articoli</h1>
<div class="entry">
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?> // <small><?php the_time('j F Y') ?></small>
<?php endwhile; // End Loop
else: ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Someone can help me? Thank you :)
I think your posts_per_page parameter is set to 1.
You can see other posts using pagination:
https://www.inunfuturoaprile.it/politica/page/2/
https://www.inunfuturoaprile.it/politica/page/3/
Check it on Settings->Reading page ( /wp-admin/options-reading.php), parameter "Blog pages show at most"
But it also can be set up somewhere in the theme code.
Changing it may affect your main page - since the main page shows only one post too.
So you can change it only on category.php page. Add this after get_header():
get_header();
global $wp_query;
$wp_query->set('posts_per_page', 10);
// or this - if you need no pagination at all:
// $wp_query->set('nopaging', true);
$wp_query->query($wp_query->query_vars); ?>

WordPress : using page template to fetch posts in wp page

i want to display posts in wp page. to do that this is what i did i created a template named mypage-page.php and copied code from page.php to mypage-page.php
this is my mypage-page.php
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
</main><!-- .site-main -->
// this has header and footer as well .
now i created a page mytest using this template ,what i expect it should list posts but it does not, please help me to understand where i'm wrong .
i'm just a beginner
If your page is a Custom Page template then you have initialise a custom query.
WordPress's default query won't work in a custom page template.
You can do that query using WP_Query Class :
https://codex.wordpress.org/Class_Reference/WP_Query
Or
get_posts() :
https://codex.wordpress.org/Template_Tags/get_posts
I'd suggest the earlier one.
you can also use this, query_posts

Wordpress - Calling on Posts From Different Categories Through a Slider

Hope someone can help me, I've been struggling for days on this trying to find the answer...
Basically, I have a wordpress site that has a slider (not a plug-in just open source code) which is called to using a 'get_template' but it displays the same three posts on every single page. I have different posts in different categories and want the slider to correspond on each separate page and echo the posts from each particular category.
<div id="slidorion">
<div id="slider">
<?php
query_posts( 'posts_per_page=3' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="slide">"><?php the_post_thumbnail(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div id="accordion">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="link-header"><?php the_title(); ?></div>
<div class="link-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
here is a link to the site if you need to see it to totally understand what I mean and need to do...
http://www.kaijocreative.co.uk/footballnatter
Thanks!
You should alter your query adding cat or category to your query_posts( 'posts_per_page=3' ); according to what you exactly want
see Query_posts () and also have a look at WP_Query class
you need to use find out the category ids from each post, then use these ids in the
$category = get_the_category();
$post_catid= $category[0]->term_id;
$querystr='cat='.$post_catid.'&posts_per_page=3';
query_posts($querystr);

Use a different page template for wordpress e-commerce

I would like to be able to display pages for wordpress plugin: 'wp e-commerce' in a different page template than the normal index.php page.
An example URL for the cart is: products-page/checkout/
However, it just fits that content into the index.php template. Is there a way to create a different page template or an if/else statement that changes based on which e-commerce page the user is on, like how the native wordpress hierarchy works?
Thanks!
I found you could target page titles with conditional statements, then used the page title for the checkout page and created this:
<?php
if (is_page( 'Checkout' ) ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php }
else{ ?>
<!--Normal Page Content-->
<?php } ?>

WordPress show only one category, excerpts only, on a page

I have a page set up to show only posts from one category, which I'm calling using the php query_posts function. How to I make it so that the posts are displayed as excerpts only, not as full articles?
Here's the code I'm using on the page:
<?php query_posts('category_name=news&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile;?>
*incorporated WordPresses excerpt function:
<?php query_posts('category_name=news&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_excerpt(); ?>
<?php endwhile;?>
Now I'm seeing first a list of the excerpts that I want followed by a repeat of the posts in the entirety...
There is a core Wordpress method that does that exactly :
http://codex.wordpress.org/Function_Reference/the_excerpt
And to control the length of the excerpt :
http://codex.wordpress.org/Function_Reference/the_excerpt#Control_Excerpt_Length_using_Filters

Categories