Display static content in Wordpress if page/post empty - php

I want to display a message if the Wordpress page is empty (eg. "Still under progress").
My loop looks like this:
<?php get_header(); ?>
<!-- Main-menu open -->
<div id ="menu-maincontent">
<?php if ( have_posts() ) while ( have_posts() ) : the_post() ; ?>
<h2><?php echo the_title(); ?></h2>
</div>
<!-- Main-menu close -->
<!-- Main-content open -->
<div id="main-content">
<!-- Main-content-in open -->
<div id="main-content-in">
<?php the_content(); ?>
</div>
<div class="cleared"></div>
<?php if ( comments_open() ) : ?>
<?php comments_template( '', true ); ?>
<?php endif; // End if comments_open() ?>
</div><!-- Main-content close -->
<?php endwhile; ?>
<?php get_sidebar(); ?>
How could I code in this message? It would be even better if I could have a separate PHP file that I call in with it (as I have a lot of page-templates in the theme).

I only changed the part which was necessary, which is div#main-content-in.
<!-- Main-content-in open -->
<div id="main-content-in">
<?php
// Get the content
$content = get_the_content();
if(trim($content) == "") // Check if the string is empty or only whitespace
{
echo "Static content";
get_template_part('slug','name');
}
else
{
// Apply filters the same as the_content() does:
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
?>
</div>
<!-- Main-content-in close -->
Sources:
http://codex.wordpress.org/Function_Reference/the_content
http://codex.wordpress.org/Function_Reference/get_the_content
http://codex.wordpress.org/Function_Reference/get_template_part

Check what I did in end while
<?php get_header(); ?>
<!-- Main-menu open -->
<div id ="menu-maincontent">
<?php if ( have_posts() ) while ( have_posts() ) : the_post() ; ?>
<h2><?php echo the_title(); ?></h2>
</div>
<!-- Main-menu close -->
<!-- Main-content open -->
<div id="main-content">
<!-- Main-content-in open -->
<div id="main-content-in">
<?php the_content(); ?>
</div>
<div class="cleared"></div>
<?php if ( comments_open() ) : ?>
<?php comments_template( '', true ); ?>
<?php endif; // End if comments_open() ?>
</div><!-- Main-content close -->
<?php endwhile; else: ?>
<? require('page_not_found.php'); ?>
<?php endif; ?>
<?php get_sidebar(); ?>

Related

Create a full-width post every 4 post grids - Wordpress Loop

How can I create a full-width post every 4 post grids? As in the following picture.
Here is my code wordpress loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-sm-6 grid-item '); ?>>
<div class="entry-image">
<?php the_post_thumbnail('grid-thumb'); ?>
<?php unbranded_entry_footer(); ?>
</div>
<header class="entry-header">
<?php
the_title( '<h3 class="entry-title">', '</h3>' );
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
<?php unbranded_posted_on(); ?>
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php echo unbranded_string_limit_words(get_the_excerpt(), 18); ?>…</p>
</div><!-- .entry-content -->
</article>
<?php endwhile; endif; ?>
May I know are you using visual composer or creating custom template?
If using visual composer you can do it from back-end or place the condition to disable the sidebar from right using
above the sidebar in page.php
<?php if(!is_page('your-page-slug' or ID)) { ?>
/*sidebar code*/
<?php } ?>

How to add post format to theme?

For classipress theme. I can register post_formats the normal way in functions (works - I can select "link" type in the editor), but I can't figure out how to get it to actually use a template file for link posts.
I have used the get_template_part('content', get_post_format()); in the past successfully,
but I can't figure how to inject that into the single.php's code for this theme:
Single.php
<?php
/**
* The Template for displaying all single posts.
*
* #package ClassiPress\Templates
* #author AppThemes
* #since ClassiPress 1.0
*/
?>
<div class="content">
<div class="content_botbg">
<div class="content_res">
<div id="breadcrumb"><?php cp_breadcrumb(); ?></div>
<div class="content_left">
<?php appthemes_before_blog_loop(); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php appthemes_before_blog_post(); ?>
<?php appthemes_stats_update( $post->ID ); //records the page hit ?>
<div class="shadowblock_out">
<div class="shadowblock">
<div class="post">
<?php appthemes_before_blog_post_title(); ?>
<h1 class="single blog"><?php the_title(); ?></h1>
<?php appthemes_after_blog_post_title(); ?>
<div style="margin-top:10px; text-align:center;">
<?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>
</div>
<?php appthemes_before_blog_post_content(); ?>
<?php if ( has_post_thumbnail() ): ?>
<div id="main-pic">
<?php cp_get_blog_image_url(); ?>
</div>
<?php endif; ?>
<?php the_content(); ?>
<div class="dotted"></div>
<div class="pad5"></div>
<?php appthemes_after_blog_post_content(); ?>
</div><!-- .post -->
</div><!-- .shadowblock -->
</div><!-- .shadowblock_out -->
<?php appthemes_after_blog_post(); ?>
<?php endwhile; ?>
<?php appthemes_after_blog_endwhile(); ?>
<?php else: ?>
<?php appthemes_blog_loop_else(); ?>
<?php endif; ?>
<div class="clr"></div>
<?php appthemes_after_blog_loop(); ?>
<div class="clr"></div>
<?php comments_template(); ?>
</div><!-- .content_left -->
<?php get_sidebar( 'blog' ); ?>
<div class="clr"></div>
</div><!-- .content_res -->
</div><!-- .content_botbg -->
</div><!-- .content -->
Content.php
<?php
/**
* Post loop content template.
*
* #package ClassiPress\Templates
* #author AppThemes
* #since ClassiPress 3.4
*/
?>
<div <?php post_class( 'shadowblock_out' ); ?> id="post-<?php the_ID(); ?>">
<div class="shadowblock">
<?php appthemes_before_blog_post_title(); ?>
<h3 class="loop"><?php the_title(); ?></h3>
<?php appthemes_after_blog_post_title(); ?>
<?php appthemes_before_blog_post_content(); ?>
<div class="entry-content">
<?php if ( has_post_thumbnail() ) the_post_thumbnail( 'blog-thumbnail' ); ?>
<?php the_content( __( 'Continue reading ...', APP_TD ) ); ?>
</div>
<?php appthemes_after_blog_post_content(); ?>
</div><!-- #shadowblock -->
</div><!-- #shadowblock_out -->
Tried a lot of things, unsuccessful in doing anything but breaking the page.
Thanks
Post formats are used to customize the presentation of post items. If you check out some theme that supports post formats (For example Twenty Fifteen), you will see that the single.php file is not used to output the contents of the post - which is delegated to content-*.php files. (For example, the the_content() function, which outputs a post's content is not in single.php, but in content.php (and content-link.php and others).
Your single.php does not seem to be built this way, so it does not support post formats, but it can be refactored easily, just split it into two files (in single.php you should keep the "frame" code that is common for every post format, then move the rest into a new content.php file, which will be used as the default format):
single.php
<?php
/**
* The Template for displaying all single posts.
*
* #package ClassiPress\Templates
* #author AppThemes
* #since ClassiPress 1.0
*/
?>
<div class="content">
<div class="content_botbg">
<div class="content_res">
<div id="breadcrumb"><?php cp_breadcrumb(); ?></div>
<div class="content_left">
<?php appthemes_before_blog_loop(); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php appthemes_before_blog_post(); ?>
<?php appthemes_stats_update( $post->ID ); //records the page hit ?>
<?php
// Note the below line which loads the desired content*.php
get_template_part('content', get_post_format());
?>
<?php appthemes_after_blog_post(); ?>
<?php endwhile; ?>
<?php appthemes_after_blog_endwhile(); ?>
<?php else: ?>
<?php appthemes_blog_loop_else(); ?>
<?php endif; ?>
<div class="clr"></div>
<?php appthemes_after_blog_loop(); ?>
<div class="clr"></div>
<?php comments_template(); ?>
</div><!-- .content_left -->
<?php get_sidebar( 'blog' ); ?>
<div class="clr"></div>
</div><!-- .content_res -->
</div><!-- .content_botbg -->
</div><!-- .content -->
content.php
<div class="shadowblock_out">
<div class="shadowblock">
<div class="post">
<?php appthemes_before_blog_post_title(); ?>
<h1 class="single blog"><?php the_title(); ?></h1>
<?php appthemes_after_blog_post_title(); ?>
<div style="margin-top:10px; text-align:center;">
<?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>
</div>
<?php appthemes_before_blog_post_content(); ?>
<?php if ( has_post_thumbnail() ): ?>
<div id="main-pic">
<?php cp_get_blog_image_url(); ?>
</div>
<?php endif; ?>
<?php the_content(); ?>
<div class="dotted"></div>
<div class="pad5"></div>
<?php appthemes_after_blog_post_content(); ?>
</div><!-- .post -->
</div><!-- .shadowblock -->
</div><!-- .shadowblock_out -->
Now, for additional post formats, for example gallery, you will have to create a file named content-gallery.php and implement it. You can derive it from the default content.php, but you decide how it should look like.

adding content above recent posts in wordpress

I am trying to insert meta slider to display above my posts in wordpress.
I correctly inserted the code in the theme index.php file above the loop. it works correctly, but it also displays on top of the side bar as well, and i would like it just above the posts.
is there any advice on how i can do this? where in the loop would i need to insert it?
here is what i have
<?php
/**
* The main template file
*
* #package SimpleMag
* #since SimpleMag 1.0
**/
get_header();
global $ti_option;
?>
<?php $archive_sidebar = get_field( 'page_sidebar', get_option('page_for_posts') ); ?>
<section id="content" role="main" class="clearfix animated">
<?php
echo do_shortcode("[metaslider id=437]");
?>
<?php if ( $ti_option['posts_page_title'] == 'full_width_title' ) : ?>
<header class="entry-header page-header">
<div class="wrapper title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="wrapper">
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
<div class="grids">
<div class="grid-8 column-1">
<?php endif; ?>
<?php if ( $ti_option['posts_page_title'] == 'above_content_title' ) : ?>
<header class="entry-header page-header">
<div class="title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="grids <?php echo $ti_option['posts_page_layout']; ?> entries">
<?php
if ( have_posts() ) : while ( have_posts()) : the_post();
get_template_part( 'content', 'post' );
endwhile;
?>
</div>
<?php ti_pagination(); ?>
<?php else : ?>
<p class="message">
<?php _e( 'Sorry, no posts were found', 'themetext' ); ?>
</p>
<?php endif;?>
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
</div><!-- .grid-8 -->
<?php get_sidebar(); ?>
</div><!-- .grids -->
<?php endif; ?>
</div><!-- .wrapper -->
</section><!-- #content -->
<?php get_footer(); ?>
As long as the sidebar is deactivated, your placement is fine. When the sidebar is active you should place it just below <div class="grid-8 column-1">. That's the beginning of the main area of your page, next to the sidebar. (I'm guessing the div is using something similar to bootstrap, taking up 8 of 12 available grids).
To prevent future updates of your theme to overwrite your customizations, if this is in fact a supported theme, you should also consider creating a child theme. It's real easy and is explained here: https://codex.wordpress.org/Child_Themes

How to make a second loop in single.php work?

-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.

WordPress Sidebar on single.php

So, my sidebar is working just fine on my blog page: beta.cleantelligent.com/blog
However, when you click a single post, I'd like that same sidebar to show up.
I am assuming this would be under single.php, right? That code is listed below. Let me know if you need any other code and I'll post it here.
I have the 'blog' sidebar in my page attributes on the Edit Page in Wordpress, but it's just not being recognized by the template, I guess?
Any help would be great. Thanks!
<?php
get_header(); ?>
<div class="blackbar">
<div class='bbw'>
Blog
</div>
</div>
<div class='cont-wrap'>
<div id="primary">
<?php
$post_obj = $wp_query->get_queried_object();
$post_name = $post_obj->post_name;
if($post_name == 'blog'){
echo 'Blog';
}else{
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
}
?>
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'archive' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div>
The blog page is using index.php
<?php
get_header(); ?>
<div class="blackbar">
<div class='bbw'>
<?php
$post_obj = $wp_query->get_queried_object();
$post_name = $post_obj->post_name;
if($post_name == 'blog'){
echo 'Blog';
}else{
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
}
?>
</div>
</div>
<div class='cont-wrap'>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
<?php comments_template( '', false ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div>
</div><!-- #main -->
Here is my sidebar.php:
<?php
$options = twentyeleven_get_theme_options();
$current_layout = $options['theme_layout'];
$nav = sb_get_page_nav($post);
if ( 'content' != $current_layout ) :
?>
<img class="sidebar-top" src='<?php bloginfo('stylesheet_directory'); ?>/images/sidebar-top.png' />
<div id="secondary" class="widget-area" role="complementary">
<?php
$post_obj = $wp_query->get_queried_object();
$post_name = $post_obj->post_name;
$title = 'cs-' . $post_name;
?>
<?php
if($post_name == 'news-events'){
if ( ! dynamic_sidebar( 'sidebar-web' ) ) :
endif;
}
?>
<?php if(!$nav['no_nav']) { ?>
<div class="SimpleSideNav">
<?php wp_nav_menu(array('container_id' => 'left-navigation','menu' => $nav['title'])); ?>
</div>
<?php } ?>
<?php if ( ! dynamic_sidebar( 'sidebar-all' ) ) : ?>
<?php endif; // end sidebar widget area ?>
<?php /*
<nav id="left-nav">
<div class="nav-wrapper">
<?php if(!$nav['no_nav']) {
wp_nav_menu(array('container_id' => 'left-navigation','menu' => $nav['title']));
} ?>
</div>
</nav>
*/ ?>
<?php if ( ! dynamic_sidebar( $title ) ) : ?>
<?php endif; // end sidebar widget area ?>
<img class="sidebar-bot" src='<?php bloginfo('stylesheet_directory'); ?>/images/sidebar-bot.png' />
</div><!-- #secondary .widget-area -->
<?php endif;
$parent_title = get_the_title($post->post_parent);
if($parent_title == 'Tour'){
echo"
<script>
jQuery('.w-1').hide();
</script>
";
}
?>
I fixed it by creating a brand new sidebar under sidebar-blog.php and redirecting my templates to it.

Categories