Use a different page template for wordpress e-commerce - php

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 } ?>

Related

Override Permalink and Display Post Data Outside Wordpress Directory

I need to pull a blog post and display on my custom single.php page that lives outside the Wordpress folder.
I have an index.php that page pulls a list of my most recent pages, however, clicking the permalink sends me nowhere. The permalink thinks it is in the Wordpress folder, so the link is relative to that.
I just want the index.php permalinks, when clicked, take the user to single.php outside my blog folder and display that post.
Here is what I have so far, but again, I don't know how to get he permalinks to send the user to my single.php page
My folder structure to help you visualize my layout
+ /blog/
- All the standard wordpress stuff in this folder
- index.php (shows recent)
- single.php (where I want the user to go)
single.php
<?php include_once('_header.php');
require_once(constant('ROOT') . '/blog/wp-load.php');
?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content-single', get_post_format() );
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<hr/>
<?php include_once('_footer.php'); ?>
You can use the post_type_link filter to change permalinks.
something like:
add_filter('post_type_link', 'custom_links')
function custom_links($post_link, $post) {
if (some conditions of $post) {
str_replace("what you want to replace", "it's replacement", $post_link)
}
return $postlink;
}
https://developer.wordpress.org/reference/hooks/post_type_link/

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 3.6: Page under category

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. ?>

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