How can I customize category pages in Wordpress? - php

I'm creating a blog with wordpress, I'm using the DIVI theme and I need to change the appearance of the category pages of the blog...
what's the easiest way to do that?
I understood I should look for category.php in the editor and create a new php file, but I couldn't find anything...

You will find category.php in your theme folder and customize according to your requirement.
If category.php file is not exist in your theme then you can create a new file with category.php name and do customization it will automatically uses this file when you will display category posts.
You need to create category template like below :
<?php
/**
* A Simple Category Template
*/
get_header(); ?>
<section id="primary" class="site-content">
<div id="content" role="main">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<header class="archive-header">
<?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 class="archive-title">Design Articles</h1>
<div class="archive-meta">
Articles and tutorials about design and the web.
</div>
</header>
<?php
// The Loop
while ( have_posts() ) : the_post();
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div class="entry">
<?php the_excerpt(); ?>
<p class="postmetadata"><?php
comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed');
?></p>
</div>
<?php endwhile; // End Loop
else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

First off, if there actually exists a file named category.php in your theme folder, it would be unwise to follow #Lalji Nakum's advice regarding editing the file directly. That would mean that you potentially lose all your changes in a future theme update. Instead you should either create a template file containing the ID or the slug of the category you would like to change. If you are to change the way all categories are displayed, you should instead create a child theme - holding your own version of category.php.
If there is no category.php in your theme folder, that means the theme controls this view in either archive.php or index.php. There's a strict hierarchy that WordPress follows, looking for a way to display categories. You would then create the file and make whatever changes to how they are to be displayed. A problem here might be that you would have to make it from scratch. An alternative would be to fall back to the child theme solution, track down where in fact your theme is controlling the view (in any of the two previously mentioned files), duplicate the file into your new child theme and do your changes here.
It's now established that you have no category.php in your theme today. You then have to choices, primarily. This is the best one:
Create category.php within your theme folder.
Create the contents of the file from scratch. A good place to start would here:
<div id="container">
<div id="content" role="main">
<h1 class="page-title"><?php
printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></h1>
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo '<div class="archive-meta">' . $category_description . '</div>';
get_template_part( 'loop', 'category' );
?>
</div><!-- #content -->
</div><!-- #container -->
Make all your wanted changes to the template.
Finished!
Creating a child theme is fairly simple, and is explained here: https://codex.wordpress.org/Child_Themes
Also it sounds like you are looking to create the file from the editor. That's not built in functionality, but could be managed resorting to creative solutions like this: http://ronangelo.com/create-new-theme-file-in-wp-admin-without-ftp/ . The better alternative would be to use ftp/ssh, though.
Read more about how category templates, including the mentioned hierarchy, work here: https://codex.wordpress.org/Category_Templates

Related

How to move the featured image below the title in Marketify single post

I'm trying to move the post's featured image below the post title in the Marketify theme, however I can't seem to find this hidden in the code.
Can anybody please help me find the code for the post's featured image? How do I move this below the post title?
Firstly, create a Child Theme for your WordPress Theme.
Then look in '/wp-content/themes/[theme-name], where you should find a file entitled 'single.php'. Copy this into your child theme, taking care to ensure you replicate the same directory hierarchy. The 'single.php' is usually the name of your default Blog Post Template.
Open up the 'single.php' file, which you have saved within your child theme, with a programme such as Notepad++ (Notepad will also do but is not as easy on the eye).
You should see something like:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}?>
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1> //This is the title of your Blog Post.
<div class="entry">
<?php the_content(); ?> //This is the content of your Blog Post.
</div>
<?php endwhile; ?>
<?php endif; ?>
There will be a slight variation in the coding but what you are looking to do is highlight:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}?>
and then move this coding to beneath <h1><?php the_title(); ?></h1>. You will need to play around as to where exactly you will need it but I hope this helps get you started.
The reason I have suggested that you do this within a Child Theme is that when the Theme Developer rolls out an update, it will delete any modifications you have made within the parent files.
Good Luck!
Thanks for your comment. I assumed it would be similar to code you've written too, but the title and featured image are hidden in the "do_action( 'marketify_entry_before' );".
get_header(); ?>
<?php do_action( 'marketify_entry_before' ); ?>
<div class="container">
<div id="content" class="site-content row">
<div role="main" id="primary" class="col-xs-12 col-md-8 <?php echo ! is_active_sidebar( 'sidebar-1' ) ? 'col-md-offset-2' : '' ?>">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php comments_template(); ?>
<?php endwhile; ?>
None the less, for anyone else with the same problem; I figured out how to hide this section in css like so...
.single .header-outer.has-image .page-header {
display: none;
}
And then could use the standard Wordpress functions to make the title and featured image appear where I wanted.
Thanks :)
Tom

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 blog dates are displaying the current date for every post

My WordPress blog, which is running a custom theme, displays the date for each entry post as the same date: today. I display the last three posts on my main home page, but those dates are fine. However my main blog page shows the current date for every post.
I am able to FTP into my site, and have access to all the PHP files, the problem is I don't know which file this error might be in, whether it be index.php, page.php, single.php, I have no idea. If anyone can suggest where the problem might be, I can help by sharing that code.
Here is the index.php
<?php
get_header(); ?>
<div class="wrap blog">
<h1>Blog</h1>
<div class="blog-left">
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next post navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
</div>
<div class="blog-right">
<?php get_sidebar(); ?>
</div>
</div>
<?php
get_footer();
Have a look at template hierarchy chart to figure out which file is used to display those posts. It might be archive.php, front-page.php, home.php, index.php depending on the theme and setup. From there, you'll see the function or which file is loaded to display each post's content.
Considering the sample code, its probably in content.php or in case it's a special post format, in content-{format}.php
I'm almost sure that if the theme is using Template Tags, is using the_date function, when it should be using the_timefunction.
You can read the docs for the_date, in the description there's a note you should read.

trying to understand page.php in Wordpress "application" theme

I'm trying to set up a commercial website using Wordpress and I'm experimenting with different themes. I like "application", but I would like to get rid of the sidebar in "pages" (as opposed to "posts"). So I figured I would edit the template. I believe that is the file "page.php." I'm just learning PHP so some of this is mysterious. My immediate question is how to get rid of the sidebar, but more important than that, I just want to ask some basic questions about the structure of this file.
Here is page.php in the themes/application directory:
<?php get_header(); ?>
<!--content-->
<div id="content">
<div id="left-col">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post-head-page">
<?php if ( is_front_page() ) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
</div><!--post-heading end-->
<div class="post-entry">
<?php the_content(); ?>
<div class="clear"></div>
<?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'application' ), 'after' => '' ) ); ?>
</div><!--post-entry end-->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div> <!--left-col end-->
<?php get_sidebar(); ?>
</div> <!--content end-->
</div>
<!--wrapper end-->
<?php get_footer(); ?>
Basic questions:
Does this represent the structure of both Pages and Posts? I only care about Pages (I won't use posts in my website; it's a commercial website)
I'm told that when I create a custom template, I have to conform to the structure of the pages in my theme. What is the "structure" of this file? How can I tell what that is for my theme?
And if you can see a simple way to remove the sidebar let me know.
This file only represents the structure for pages. Not for posts. To see what templates are used for what content, see this documentation, including a nice diagram.
I believe they mean that you'll need to keep the same html/php structure as in this file. When creating custom page templates, duplicate this file and then start adjusting it to your needs.
Remove the line with get_sidebar() to remove the sidebar.
Hope that helps!

Custom layouts for custom post types? (wordpress)

I'm intending to have a few different custom post types but I want them to have different layouts to that of the normal posts.
Normal posts have two different appearances themselves, one for the index page and one for when you click through to the permalink page.
For custom posts I want to do the same thing (two different layouts, both different from normal posts) but for some reason my code doesn't seem to be making a difference.
I've so far used custom post template plugin as well as tried to code in a post-[postype].php file, but both seemed ineffective.
For my single.php here's what the code is -
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<!--- post wrapper home/post --->
<?php if ( is_home()) { echo '<div class="fullposthome">' ; }
else { echo '<div class="fullpost">' ; }
?>
<?php if( get_post_meta($post->ID, 'imgtest', true) ) { ?> <!--- made following div appear if said custom field presetn ---->
<div class="testbox"><img src="<?php the_field('imgtest'); ?>" alt="" width="100%" height="auto"/></div> <!--- div with custom field inside --->
<?php } ?>
<?php if ( is_home()) { echo '<div class="contenttextboxhome">' ; }
else { echo '<div class="contenttextbox">' ; }
?>
<?php get_template_part( 'content', 'single' ); ?>
</div>
<?php temptheme1_content_nav( 'nav-below' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!--- post wrapper --->
</div><!-- #primary -->
For custom posts I've tried changing the line
for custom posts I tried as that is what I'm assuming the names are referencing [content/single.php]
- this is in underscore.me / _S framework mind you, I'm also going to try on Thematic framework but since _S is more bare bones it would be easier for me to build it how I want it.
So my question I guess is where am I going wrong with my coding or how do I use the Custom Post Template plug in properly?
If your custom post type is "products"
then
Archive file should be : archive-products.php
Taxonomy should be : taxonomy-product_category_slug.php
Single file should be : single-products.php
if you want different content template then
create file of like content-product.php
and in your single.php use get_template_part( 'content', 'product' );
For current situation if everything working fine then just create content file, customize it.

Categories