How to add post format to theme? - php

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.

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

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

Customizing Wordpress to show posts by id in Index page

I'm having problem in showing posts in my index page in wordpress the title and contents doesnt show. Currently i'm modifying the index.php of twenty-twelve template to show the custom post by id, my goal is to show 3 posts in my index page, here's my code below:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if ( current_user_can( 'edit_posts' ) ) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php printf( __( 'Ready to publish your first post? Get started here.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
The code above is the default index.php of twenty-twelve template in wordpress. I replaced this code from:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
To this, because i want to show only post 23 by id.
<?php $the_query = new WP_Query( 'page_id=23' ) ?>
<?php while ( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
So the whole code for my new index.php is:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header();
?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('page_id=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
That's all what i have guys. Can you help me? Is there anything wrong on showing the posts? Thanks in advance guys. :)
An approach if u dont want to use Wp_query
Try this:
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $post = get_post(23);?>
<?php echo $post->post_title; ?>
<?php echo $post->post_content; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to display post with id 23. Right?
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('p=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
You are missing little like this :
$query = new WP_Query( 'p=7' );
ALternate you can try query_posts() for specific post in query like this:
<?php query_posts('p=23'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>

Sticky Post appears twice in homepage loop. Is there a way to make it appear only once?

So in WordPress, if you run the regular loop, Sticky Posts appear twice. Once at the beginning (as desired) and then once in the chronological order among regular posts. Is there a way to make a post sticky and not appear the second time?
Here's the code from the index.php file.
<?php get_header(); ?>
<div id="content">
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Default') ) : ?>
<?php endif; ?>
</div>
<div class="textContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<div class="featuredImage">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'main-thumb' ); } ?>
</div>
<div class="readMore">
<?php // the content of the post
the_content('Read More'); ?>
</div>
<div class="comments">
<?php _e('', 'surplus'); ?> <?php comments_popup_link('{0 Comments}', '{1 Comment}', '{% Comments}'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="nav3">
<?php posts_nav_link(); ?>
</div>
<?php get_footer(); ?>
Thank you!
Liz

Display static content in Wordpress if page/post empty

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(); ?>

Categories