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); ?>
Related
I am showing the last post of a blog on an external website that I am developing out of wordpress.
<?php
//Include WordPress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
//Define quantos posts serão exibidos
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<li>
<h4><?php the_title(); ?></h4>
<span><?php the_time("d/m/Y"); ?></span>
<?php the_category_ID(); ?>
<?php the_content(); ?>
<div>
« Leia Mais...
</div>
</li>
<?php endwhile;?>
The point is that the blog is separated into two categories and I would like to display only one of them.
Link where I got those functions.
http://codex.wordpress.org/Template_Tags
When you query posts, you can add the conditions for categories as well, like this:
query_posts('showposts=3&cat=CAT_ID'); //replace CAT_ID with the category ID that you want
Since you mentioned that you wanted to show the latest posts, you might need to do this:
query_posts('showposts=3&cat=CAT_ID&orderby=date&order=DESC');
I am currently using the cubic theme and ran into some trouble with template creation. I have multiple sections on my site and am trying to replicate the functionality of my home page onto another page where ideally the home page would display featured articles and another page for urban exploration would be able to display posts about urban exploration in the same format. I.e. Images with nicely displayed titles in rows of three link to posts I have created.
So far this is what my template looks like:
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<div id='primary' class='content-area'>
<main id='main' class='site-main' role='main'>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This allows me to display the header with the integrated side bar and footer, but I cannot figure out how to replicate cubic's home page functionality.
I imagine the PHP function would have to query for tags in order to place the post on the page, then look for a post's featured image and title to display it as a square image covering 1/3 of the page. Any help would be greatly appreciated.
Update:
I found you can use WP_Query to create a loop to pull information then display it using get_template_part as a means of formatting the post information. I can now display the images for my posts however the formatting is off because of the way content.php is written. [Do any users of cubic (a child theme in WordPress) know how the homepage is constructed and how I might be able to reference that file so I can recreate its format on a different page in WordPress?]
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<main id='main' class='site-main' role='main'>
<?php
$postid = new WP_Query( array( 'tag' => 'featured' ) );
if( $postid->have_posts() ):
while( $postid->have_posts() ): $postid->the_post(); ?>
<?php get_template_part('content',get_post_format()); ?>
<?php endwhile;
endif;
?>
</main><!-- .site-main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to add the loop which fetches the posts:
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I'm trying to show on my wordpress theme a specific post on a page. In this case, I'm trying to show the post on the home page and I've tried a lot to at least show the title, but all that I get is the title of the page, not of the post itself.
I've tried the_title and get_the_title() but the magic didn't happen.
Here comes the code relevant to my problem.
home.php
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
// if ( has_post_format( 'video' )) {
// echo 'this is the video format';
// }
get_template_part( 'template-parts/content-chat', get_post_format('chat') );
endwhile;
else :
// get_template_part( 'template-parts/content', 'none' );
endif; ?>
And it calls the file (it is calling the right file, double checked)
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<h2><?php the_title(); ?></h2>
<?php the_content();
echo get_post_format();
?>
</article><!-- #post-## -->
In summary, the questions are:
1) Why is it showing the title of the page?
2) How can I show properly this kind of post format in my page?
For your first question wp grabs the title of the home page based on query_posts() this is what makes wordpress so dynamic, you can actually check a page or category id and then select with query posts what to show on it. Since you do not have a query_posts in your code wp defaults to the current post id which is your home page.
For your second, I had a code snippet, but it is at home, you can look it up here
https://developer.wordpress.org/reference/functions/query_posts/
Ok, so here is the snippet. Sorry for the delay, I have been on vacation.
First you have to determine the ID of your home page, or you can use the is home page function. If you go the former route you simply go to the back end to your home page and hover over it with your mouse. the home page should show up in a link tooltip at the bottom of your page. It will be something like post=5 the number is what you need.
Next I would create a category for the post you want to display on the front page and get that ID from the category page in the back end. Again it will be something like tag_id=12
<?php
$currpage = $post -> ID;
if($currpage == 5) {
query_posts('showposts=1&cat=12');
}
while (have_posts()): the_post();
You can have multiple posts on one page this way, infact you can have your whole loop on that page then under it dynamically populate other posts under it like in this example:
http://testex-ndt.com/products/ect-products/ The top part is the page that is working a loop and the bottom (testimonials) are posts that are assigned to a specific category, the showposts here are set to 3 and will display excerpts of the latest three posts. This is one website I did in the past.
One other thing you may want to keep in mind is page templates, if you want one page to work one way and you want another type of page to work another you will have to look into templating.
here is the entire code for that page.
<?php
/*
Template Name:Main Product
*/
?>
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
<?php if (have_posts()) : ?>
<?php while (have_posts()): the_post(); ?>
<?php $postName = get_the_title($post); ?>
<!-- title link dynamic -->
<?php edit_post_link('Edit this item','',' | '); ?>
<?php the_content('Continue Reading'); ?>
<?php posts_nav_link(); ?>
<?php endwhile; ?>
<?php else : ?>
<div class="w3-col text">
<h2>Not Found</h2>
<p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
<!-- insert testimonial here -->
<section class="w3-row cl testimonials">
<!-- make code for query based on the page, then get the top three testimonials in excerpt form This is commented out because I did it with a function I attached to the header I will show below
$currpage = $post -> ID;
if($currpage == xx){
$cat = x;
}
-->
<h2>Testimonials for <?php echo $postName; ?></h2>
<?php
$currpage = $post -> ID;
$cat = testimonial($currpage); //here is the function
query_posts('showposts=3&cat=' .$cat ); ?>
<?php while (have_posts()): the_post(); ?>
<div class="w3-col excerpt" style="width:30%;">
<?php the_excerpt(''); ?>
</div>
<?php endwhile;
wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS
?>
</section>
<!-- /content float -->
</div>
<!-- /content -->
</div>
So the Function was set up like as a seperate php file so I could alter it modularly. I called the file category_help.php
<?php
/*This particular block of code is only for determining what category of testimonial is allowed in a post. */
function testimonial($myPageID){
//services (all cats)
$id = "-15,-14,-13";
//Products
//lfet
if($myPageID == 18) $id = 2;
//bfet
if($myPageID == 22) $id = 4;
//rfet
if($myPageID == 20) $id = 3;
//ect
if($myPageID == 24) $id = 5;
//ultrasound
if($myPageID == 26) $id = 8;
return $id;
}
?>
Then I called this in the top of header.php
<?php require 'category_help.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. ?>
I'm hoping that I can get some assistance on this one! I'm trying to implement some PHP in the sidebar that displays a list of the categories (as links to the category itself). If clicked, I'd like it to go to the category page of course, and then for the posts within that category to appear in a list underneath it as links.
Example of the homepage:
Category A
Category B
Category C
Example of the homepage within Category A:
Category A
- Post One
- Post Two
- Post Three
Category B
Category C
I'm sorry if this is something obvious. I'm trying to find my own solution but thus far have been unable to.
Thanks again!
You can use this codes for displaying the categories and the list of posts in
hierarchial order:
<?php
/*
Template Name: Archives with Content
*/
?>
<?php get_header(); ?>
<div id="content" class="widecolumn">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>"><?php the_title();?></h2>
<div class="entrytext">
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<div id="main">
<?php //include (TEMPLATEPATH . '/searchform.php'); ?>
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=daily&limit=1&show_post_count=1'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories('orderby=name&show_count=1'); ?>
</ul>
<?php get_footer(); ?>
1.Just copy the above codes in a file named archieve_page.php.then,go to admin->pages->add page and give title as archieve page and in the right side corner,click the dropdown template and select the "Archieve with content" and click publish and in the top click "view page" to see the page.
I wrote a little widget that does include something like that the code might get you started to create your widget: http://wp.leau.co/2010/12/01/sidebar-feed-widget-showing-tweets-stumbles-vids-and-more/
Just loop the categories as above
Then with http://codex.wordpress.org/Function_Reference/is_category you can check if you are on a category page, then check with http://codex.wordpress.org/Function_Reference/single_cat_title if it is the active page