Make a wordpress 404 page dynamic - wordpress

So I cant find and info about this, but I want to hook the default 404 page to a page thru a custom page template so that I can manage the content on the 404 page. Is this possible ?

To do this, simply create a page named 404.php in your theme.
Take a look at the simple structure of the 404.php file that is shipped with Twenty Thirteen. It basically features tags that display the header, sidebar, and footer, and also an area for your message:
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<header class="page-header">
<h1 class="page-title"><?php _e( 'Not Found', 'twentythirteen' ); ?></h1>
</header>
<div class="page-wrapper">
<div class="page-content">
<h2><?php _e( 'This is somewhat embarrassing, isn’t it?', 'twentythirteen' ); ?></h2>
<p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .page-content -->
</div><!-- .page-wrapper -->
</div><!-- #content -->
</div><!-- #primary -->
So, to change the error message your visitor sees, revise the text within the h1 heading and within the page-content class; if necessary, add more paragraphs below that.
You can see more about this in the WordPress Codex.
Now to add content to this page you'll need to first create a new page in WP and tell it to use your 404 template. Once we've added page content we just need to add some code to the template php file. The following assumes that our new page has an ID of '12'.
<?php
// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12);
$content = $post_12->post_content; //would echo post 12's content
//$excerpt = $post_12->post_excerpt; // would echo post 12's content up until the <!--more--> tag
?>
Now add this code into your template like so:
<?php
/**
* The template for displaying 404 pages (Not Found)
*
* #package WordPress
* #subpackage Twenty_Thirteen
* #since Twenty Thirteen 1.0
*/
get_header(); ?>
$post_12 = get_post(12);
$content = $post_12->post_content; //would echo post 12's content
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<header class="page-header">
<h1 class="page-title"><?php _e( 'Not Found', 'twentythirteen' ); ?></h1>
</header>
<div class="page-wrapper">
<div class="page-content">
<h2><?php $content ?></h2>
<p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .page-content -->
</div><!-- .page-wrapper -->
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
However, assuming that you want to install this on a system where you don't know the page ID you could always create a page via your theme or plugin that uses a fixed name/title, eg: "custom-404".
Then you can change your template's code to call that post by name/title, since we always know what this is called.
$page = get_page_by_title( 'custom-404' );
//$post_12 = get_post(12);
$content = $page->post_content; //would echo post custom-404's content

Related

Wordpress Page Template Issue: Cannot see my Custom Page Template in page attribute dropdown

I am using Understrap to develop a custom theme and when I add a new page template, it does not show up on the page attributes dropdown.
These are the fixes I have tried and they don't work:
1) Activating to another theme and changing back to Understrap.
2) Using a plugin to clear cache due to the caching issue of wordpress 4.9.
3) Bumping Theme Version.
4) Updating Wordpress.
Nothing Seems to work and I am in a fix!.
I am using MAMP for local dev.
folder structure
page templates folder, I've highlighted the files that I have added, none of them show on the dropdown
Thank you for any help that you can provide and please do let me know if you need more info.
EDIT:
<?php
/**
* Template Name : Portfolio Page Template
*
* template for the portfolio masonry style
*
* #package understrap
*/
get_header();
$container = get_theme_mod( 'understrap_container_type' );
?>
<div class="wrapper" id="full-width-page-wrapper">
<div class="<?php echo esc_attr( $container ); ?>" id="content">
<div class="row">
<div class="col-md-12 content-area" id="primary">
<main class="site-main" id="main" role="main">
<?php echo '<div class="card-deck">' ?>
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="card">
<img class="card-img-top" src="<?php get_post_thumbnail_url($post->ID); ?>" alt=" ">
<div class="card-block">
<h4 class="card-title"><?php print get_the_title(); ?></h4>
<p class="card-text"><?php print get_the_excerpt(); ?></p>
</div><!--card-block-->
<div class="card-footer">
<small class="text-muted"><?php get_the_terms($post->ID, array( 'Skills' )); ?></div>
</div>
</div><!--card-->
<?php endwhile; ?>
</div><!--#card-deck-->
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .row end -->
</div><!-- Container end -->
</div><!-- Wrapper end -->
<?php get_footer(); ?>
This is the template that I want to Include.
EDIT 2:
As #Alexander Suggested I included that code in the functions.php. It Didn't work..I then included a small snippet to display the page template array on the index page and modified the code snippet like so.
if( is_page_template( 'page-templates/page-portfolio.php' )){
echo "template exists";
}
else{
echo "this template does not exist";
}
This is the result:loop and array
It seems like space issue. Can you make only below change in your custom template file:
From
Template Name : Portfolio Page Template
To
Template Name: Portfolio Page Template
I checked the same in my local set up and confirmed above. It will work for you.
On the one hand, You need to adhere to the file structure that is adopted in Wordpress.
So, page templates must been putted in the root folder of theme or child theme.
But also there is always another way, you can include the file in functions.php:
if ( is_page_template( 'page-templates/my-template.php' ) ) {
include_once 'page-templates/my-template.php';
}
OK I have found the problem, it was quite silly, There is space between name and :, well it shouldn't be there. Thank you for all your answers and suggestions.

Using Wordpress conditional is_page

I need help with using Wordpress conditional. The below code is the actual code I'm having problems with.
The goal is to check if the page is "bb_press" and if it is, load another template called "archive-forum".
If it's not, then continue using current template.
I tried defining if is_bbpress in the beginning, but I'm not doing something right. Perhaps the ending is the problem..
Help appreciated
<?php
if ( is_bbpress() ) {
get_template_part('archive-forum') {
else {
<--- Continue with the current template code --->
}
<?php endif;?>
<?php get_footer(); ?>
After this, I'm getting a blank page with nothing loaded.
EDIT: This is the orignal template I'm trying to modify. Without your suggestions. Tried them, but I'm doing something wrong. What am I missing here? Am I placing the "if" in the wrong place? Thank you all for your help so far.
<?php
/**
* Directory archive page
*
**/
get_header(); //Header Portion
$tmpdata = get_option('templatic_settings');
global $posts,$htmlvar_name;
do_action('after_event_header');
/* Left content side bar for all pages */
global $htmlvar_name;
if(function_exists('tmpl_get_category_list_customfields')){
$htmlvar_name = tmpl_get_category_list_customfields(CUSTOM_POST_TYPE_EVENT);
}else{
global $htmlvar_name;
}
if (!is_active_sidebar( 'tmpl_listings_left_content') ){
$class="content-middle";
}else{
$class="";
}
/* Here we use for Show left content sidebar , it can be use for many other purpose too */
do_action('tmpl_all_pages_left_content');
?>
<div class="content-sidebar <?php echo $class; ?>">
<script type="text/javascript">
var category_map = '<?php echo $tmpdata['category_map'];?>';
<?php if($_COOKIE['display_view']=='event_map' && $tmpdata['category_map']=='yes'):?>
jQuery(function() {
jQuery('#listpagi').hide();
});
<?php endif;?>
</script>
<!--taxonomy sidebar -->
<!--end taxonomy sidebar -->
<div class="page-head">
<?php
/* back page link */
tmpl_back_link();
do_action('after_directory_header'); /*do action for display the breadcrumb in between header and container. */
do_action('directory_before_container_breadcrumb');
?>
</div>
<?php
if ( is_active_sidebar('listingcategory_listing_above_content') ) : ?>
<div class="filters">
<div id="sidebar-primary" class="sidebar">
<?php dynamic_sidebar('listingcategory_listing_above_content'); ?>
</div>
</div>
<?php endif; ?>
<div class="view_type_wrap">
<?php /*do action to display the breadcrumb inside the container. */
/* Archive page title */
do_action('directory_before_archive_title'); ?>
<h1 class="loop-title">
<?php echo ucfirst(apply_filters('tevolution_archive_page_title','Listing'));?>
</h1>
<?php
if($archive_description[CUSTOM_POST_TYPE_LISTING]['description'] !=''){
?>
<div class="archive-meta"><?php echo $archive_description[CUSTOM_POST_TYPE_LISTING]['description']; ?></div>
<?php
}
do_action('directory_after_archive_title');
do_action('directory_before_loop_archive');
?>
</div>
<div id="content" class="contentarea large-9 small-12 columns <?php directory_class();?>">
<!--Start loop archive page-->
<div id="loop_listing_taxonomy" class="search_result_listing <?php if($tmpdata['default_page_view']=="gridview"){echo 'grid';}else{echo 'list';}?>" <?php if( is_plugin_active('Tevolution-Directory/directory.php') && $tmpdata['default_page_view']=="mapview"){ echo 'style="display: none;"';}?>>
<?php if (have_posts()) :
while (have_posts()) : the_post();
do_action('directory_before_post_loop');?>
<div class="post <?php templ_post_class();?>" >
<?php
/* Hook to display before image */
do_action('tmpl_before_category_page_image');
/* Hook to Display Listing Image */
do_action('directory_category_page_image');
/* Hook to Display After Image */
do_action('tmpl_after_category_page_image');
/* Before Entry Div */
do_action('directory_before_post_entry');?>
<!-- Entry Start -->
<div class="entry">
<?php /* do action for before the post title.*/
do_action('directory_before_post_title'); ?>
<div class="listing-wrapper">
<!-- Entry title start -->
<div class="entry-title">
<?php do_action('templ_post_title'); /* do action for display the single post title */?>
</div>
<?php do_action('directory_after_post_title'); /* do action for after the post title.*/?>
<!-- Entry title end -->
<!-- Entry details start -->
<div class="entry-details">
<?php /* Hook to get Entry details - Like address,phone number or any static field */
do_action('listing_post_info'); ?>
</div>
<!-- Entry details end -->
</div>
<!--Start Post Content -->
<?php /* Hook for before post content . */
do_action('directory_before_post_content');
/* Hook to display post content . */
do_action('templ_taxonomy_content');
/* Hook for after the post content. */
do_action('directory_after_post_content');
?>
<!-- End Post Content -->
<?php
/* Hook for before listing categories */
do_action('directory_before_taxonomies');
/* Display listing categories */
do_action('templ_the_taxonomies');
/* Hook to display the listing comments, add to favorite and pinpoint */
do_action('directory_after_taxonomies');?>
</div>
<!-- Entry End -->
<?php do_action('directory_after_post_entry');?>
</div>
<?php do_action('directory_after_post_loop');
endwhile;
wp_reset_query();
else:?>
<p class='nodata_msg'><?php _e( 'Apologies, but no results were found for the requested archive.', 'templatic' ); ?></p>
<?php endif;
if($wp_query->max_num_pages !=1):?>
<div id="listpagi">
<div class="pagination pagination-position">
<?php if(function_exists('pagenavi_plugin')) { pagenavi_plugin(); } ?>
</div>
</div>
<?php endif;?>
</div>
<!--End loop archive page -->
</div>
</div>
<?php get_footer(); ?>
This is correct code for your condition and will work fine. You have to close the brace for the condition. Check this for condition reference click here
<?php
if ( is_bbpress() ) {
get_template_part('archive-forum');
} else {
// continue with the current template code
}
get_footer();
?>
If you are using brackets, you do not need to use endif; This whole endif; and opening <?php and closing ?> in every row is very ugly. <!-- not valid in PHP code. And you need to add ; after get_template_part('archive-forum');
<?php
if ( is_bbpress() ) {
get_template_part('archive-forum');
} else {
//Continue with the current template code
}
get_footer();
?>
<!-- More none php code here. If there are no more php code, you can leave ?> -->

Wordpress - Individual pages as sections on front-page

I created multiple pages like services, about, our team... in wordpress and i can modify content of those pages creating individual php files for each page like page-services.php or page-about.php so i can use advanced custom fields plugin in those pages. But what i need to do now i to include those pages as sections in my front-page or static-page so those previous pages are actually sections in my front-page. And i try to do this with get_template_part() and with get_post() and get_page() and include() but nothing is working. I look for answer all over internet but i just cant find it and i think it should be very simple. I rly appreciate any help.
<div id="service" class="text-center">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<h3><?php the_title(); ?></h3>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p><?php the_field('service_subtitle'); ?></p>
</div>
</div>
<div class="row">
<?php if(get_field('service_box')):
while(has_sub_field('service_box')): ?>
<div class="col-sm-4 column-box wow fadeInUp animated" data-wow-delay="0.3s">
<h3><?php echo the_sub_field('service_box_title'); ?></h3>
<div class="title-line"></div>
<p><?php echo the_sub_field('service_box_content'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
This is my code on service page or in page-service.php file. And now i need to include this page or this code in my front page. Also i have multiple pages (sections) like this which i need to include in my front page also. End i dont want to just copy this and rest of code in front-page i wont it to be on separate pages and then include those pages in front page.
You can query the page content based on ID and insert to your current front-page code. Like the code below.
<?php
// Assume that the Id of the page you need to include is 7.
$mypages = get_pages( array( 'include' => '7', 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<h2><?php echo $page->post_title; ?></h2>
<div class="entry"><?php echo $content; ?></div>
<?php
}
?>

Wordpress the_content / post is empty

I'm building my own template for Wordpress and I'm bumping into an issue when displaying the content of a post.
I have already built one page template for the home page and it works fine. The loop outputs what I want to display. Now I'm building the template to display an article but the loop doesn't return anything.
Here is the code of the page template:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
This results in displaying the "sorry, no posts matched your criteria" message when i click on one of the links on the homepage. The strange thing is that the page really exists (it has ID=26 as you'll see here below):
<?php
$post = get_post(26);
$title = $post->post_title;
echo $title;
?>
This works and displays the expected title. I have tried get_the_ID(); to get the post ID but it returns an empty variable.
There is probably something missing in my template but I can't figure out what.
Any idea?
Thanks
I have found the issue and it had nothing to do with the template itself.
I discovered what's wrong by using one of the standard Wordpress themes (twentyfifteen) where every post led me to a 404 even if I clicked on a post from the Admin UI. I swapped back permalink structure to the ?p=123 option and there everything working. Definitely a permalink structure problem.
The problem was coming from the polylang plug-in. I'm using a network of wordpress site for which I have made this plug-in available. I did not need this plug-in for this particular site but somehow it needed to be active anyway. So I activated polylang and configured it to have only one language and now it works.
Got new gray hair in the process but if that can help anyone...
Thanks for your help!
Laurent
I don't think, that this template can output any posts like this. For WordPress
/*
Template Name: PAGE
*/
indicates: this is a page-template which you can asign to the content of a page but not to a post.
When you want to show a specific post on such a page you will have to:
Asign the template to the page in WP-backend
Add a new query to the template to show the post
Like:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
// The new Query
$args = array (
'p' => '26',
);
// The Query
$query = new WP_Query( $args );
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
But as this is very complicated I would recommend to:
delete the /* Template Name: PAGE */ from the template
save it as single.php or more specific as single-26.php
link to the desired post in your wp-menu

Menu in WordPress

When I installed WordPress I had a home menu. While editing, I added a menu item to the menu but my home menu item disappeared.
I don't know what's going on. How do I add the home menu item which links to homepage back?
I'm not sure why your home item would have disappeared after adding another item, but make sure your theme is using wp_page_menu():
http://codex.wordpress.org/Template_Tags/wp_page_menu
This carries the benefit of listing your "Home" along with your other pages.
Edit the line in the header.php
wp_page_menu( 'show_home=1');
reference in the codex : http://codex.wordpress.org/Function_Reference/wp_page_menu
If you have already added Home as menu, then copy the content of index.php into your Home.php (create this empty file in your theme) file.
Then run and check the home page.
Step 1. For linking your home page, create index.php as template file for home like the code below:
Use these codes if you're using custom theme(theme created by yourself in themes folder of wp-content other than twentyelevan, twentyten and so on.
<?php
/**
* Template Name: home
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header(); ?>
// whatever body code u need,u can include it here.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-header">
<!-- <div class="date"><?php the_time( 'M j y' ); ?></div>-->
<!-- <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent
Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>-->
<!-- <div class="author"><?php the_author(); ?></div>-->
</div><!--end post header-->
<div class="entry clear">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<?php the_content(); ?>
<!-- <?php edit_post_link(); ?>-->
<?php wp_link_pages(); ?>
</div><!--end entry-->
<div class="post-footer">
<!-- <div class="comments"><?php comments_popup_link( 'Leave a Comment', '1
Comment', '% Comments' ); ?></div>-->
</div><!--end post footer-->
</div><!--end post-->
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<div class="navigation index">
<div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
<div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
</div><!--end navigation-->
<?php else : ?>
<?php endif; ?>
<div id="slider">
<?php
$tmp = $wp_query;
$wp_query = new WP_Query('posts_per_page=5&category_name=featured');
if(have_posts()) :
while(have_posts()) :
the_post();
?>
<a href="<?php the_permalink(); ?>"><?php
the_post_thumbnail('nivothumb'); ?></a>
<?php
endwhile;
endif;
$wp_query = $tmp;
?>
</div><!-- close #slider -->
<?php get_footer(); ?>
Or, if you're using wordpress builtin themes, then use these codes:
<?php
/**
* Template Name: onebyone
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<html>
// whatever body code u need,u can include it here.
</html>
Step 2. Then open the home page in wordpress, Pages -> All pages, click edit.
Step 3. In edit page of home, in the right side you will see Dropdown named Template. Click the
dropdown, there you will see your template name as home select it and then click update
button.
Step 4. Then,On top left side corner,click your website or blog name and visit the website or
blog to check your home page by clicking on it.
Do you have a link to your site?
It may be as simple as checking a box. OR, you can create a custom menu.

Resources