Override Permalink and Display Post Data Outside Wordpress Directory - php

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/

Related

How can I customize category pages in Wordpress?

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

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.

Page Content In Wordpress Is Not Displaying

I am creating a wordpress theme from scratch and I think I'm having problems with my page.php file. When I go into the admin panel, add a new page and fill out the content I want then view the page I only see the title of the page while the rest is blank. For some reason the content of the page is not displaying.
The code in my page.php file is..
<?php /*
Template Name: Page Template
*/
get_header(); ?>
<div class="page">
<h1 class="entry-title"><?php the_title(); ?></h1>
<hr>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php get_footer(); ?>
I only get to see up to the horizontal rule and then the footer but everything else in between is blank. Am I maybe missing something in my functions.php file?
Try to var_dump( get_template_part('content', 'page') ) or even var_dump(the_post()), you should get important debugging info...
I found the problem. It was a piece of code in my functions.php file.
function PLEASE_the_tags($content='') {
global $post;
$html.="<div class='tags'>\n";
$html.="<a href='#' rel='tag'>".$post_the_tags."</a>\n";
$html.="</div?\n";
$content.=$html;
return $content;
}
add_filter('the_content', 'get_the_tag_list');
After I removed this, the content started showing again. I have no idea how this caused the problem but thank you for everyone's help.

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.

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

Categories