I can't get rid of a right sidebar in a custom page template. Something seems to be passing from page.php to the custom page template and I don't know how to prevent this. The custom page template code is simple:
<?php
/* Template Name: sample template */
get_header(); ?>
<div id="body">
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
Below is my page.php code, inherited from a previous developer:
<?php get_header(); ?>
<div id="body">
<div class="align left">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<br />
<?php the_title(); ?>
<br />
<?php // include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' =>'Pages: ', 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php // comments_template(); ?>
<?php endwhile; endif; ?>
</div>
<div class="align right">
<?php
global $post;
$showImage = true;
$url = $url = WP_CONTENT_URL . '/uploads/';
$file = get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attached_file', true);
if ( $file == ''){
$showImage = false;
}
$url .= $file;
if ( $showImage ){
?>
<img src="<?php echo $url; ?>" alt="" />
<?php
}
?>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
I don't want to edit page.php to get a full-width custom page. How can I achieve this in the custom page template?
You can use get_sidebar(); function.
you can know more about this function here http://codex.wordpress.org/Function_Reference/get_sidebar
Related
I try to devolop my own wordpress theme. However i struggle with the following:
I added the loop to my content php and it is loaded by the page php
the template at the home site is the page.php.
the template when i click and load a post with its content is also the page.php
Ok. So now i have enabled thumbnails. But:
The Thumbnails also show up when i click and open the post.
I only want them to show up in the list of posts page, not in the posts itself.
How can i do that?
Please help me
index.php
<?php get_header(); ?>
<div id= "mainandcartwrapper" class="mainandcartwrapper">
<main>
<?php
$path = "C:\wamp64\www\almondo/wp-content/themes/almondotheme/";
$onlytemplate = str_replace($path,"", get_page_template());
echo $onlytemplate;
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part("template_parts/content"); ?>
<?php endwhile; else : ?>
<?php get_template_part("template_parts/content","error"); ?>
<?php endif; ?>
</main>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
</div>
</body>
</html>
page.php
<?php get_header(); ?>
<div id= "mainandcartwrapper" class="mainandcartwrapper">
<main>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
get_template_part("template_parts/content");
?>
<?php endwhile; else : ?>
<?php get_template_part("template_parts/content","error"); ?>
<?php endif; ?>
</main>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
</div>
</body>
</html>
content.php
<article <?php post_class();?>>
<p class="text-l almondo-link">
<a href="<?php the_permalink();?>">
<?php the_title();
?>
<br>
<?php
$path = "C:\wamp64\www\almondo/wp-content/themes/almondotheme/";
$onlytemplate = str_replace($path,"", get_page_template());
echo $onlytemplate;
?>
<p>Veröffentlicht von <?php the_author(); ?> am <?php the_date("d.m.Y"); ?></p>
</a>
</p>
</article>
content_page.php
<article <?php post_class();?> >Statische Seite:
<p class="text-xs">
<?php the_title(); ?>
</p>
</article>
Have a nice Weekend,
Your Alwin
I have this code in my event_single.php in tribe_events folder under theme. I've been trying to do the drop cap but it seems that this is not working and even the p tag is not being respected.
<div id="post-<?php the_ID(); ?>" <?php post_class('event__main'); ?>>
<?php while (have_posts()): the_post(); ?>
<div class="event__description page__content">
<h3 class="event__description__title"><?php _e('Description') ?></h3>
<?php do_action('tribe_events_single_event_before_the_content') ?>
<?php
$content = apply_filters('the_content', get_the_content());
echo preg_replace('/<p>(.)/', '<p><span class="drop-cap">$1</span>', $content, 1);
?>
<?php do_action('tribe_events_single_event_after_the_content') ?>
</div>
<div class="event__details">
<?php do_action('tribe_events_single_event_before_the_meta') ?>
<?php tribe_get_template_part('modules/meta'); ?>
<?php do_action('tribe_events_single_event_after_the_meta') ?>
</div>
<?php endwhile; ?>
</div>
I am trying to get latest posts to display through a template page i am building for pages, the loop is not running the latest post only one page
ok, I have a simple loop that gets latest post
my loop
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
and content.php
<div class="blog-post">
<h2 class="blog-post-title">
<?php the_title(); ?>
</h2>
<p class="blog-post-meta">
<?php the_date(); ?>by <?php the_author(); ?>
<a href="<?php comments_link(); ?>">
<?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
</a>
</p>
<?php if ( has_post_thumbnail() ) {?>
<div class="row">
<div class="col-md-4">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="col-md-6">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
</div>
when i run the loop in index.php i get my latest blog post, perfect.
however, i am building a template page, i try and include the loop in this page, i just get one page (not all posts).
my template
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()) ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
If you are using multiple loops on the same page, you must use rewind_posts() like so:
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()); ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
<?php rewind_posts(); ?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
This "resets" the loop to it's original state and allows you to look through the posts again. In your original code you scan through all the posts, and then in your second loop scan through nothing, as you have already scanned through all the posts!
Hmm I have found this solution using a for each rather than the while loop seems to work, but im not sure if its the best way around.
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
UPDATE
<?php
$args = array('numberposts' => 5);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
$excerpt = wp_trim_excerpt($recent['post_content']);
$permalink = get_permalink($recent["ID"]);
$title = esc_attr($recent["post_title"]);
$thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
echo $excerpt;
}
?>
How can you get the post category on single.php?
I've tried:
<h1><?php echo $this->escapeHtml($post->get_the_category()) ?></h1>
&
<?php echo $this->escapeHtml($post->get_category_parents( $cat, true, ' » ' )) ?>
&
<h1><?php echo $this->escapeHtml($post->get_the_category($post->ID)) ?></h1>
This is the entire file:
<?php $post = $this->getPost() ?>
<?php if ($post): ?>
<?php $helper = $this->helper('wordpress') ?>
<?php $author = $post->getAuthor() ?>
<div class="page-title post-title">
<h1><?php echo $this->escapeHtml($post->get_the_category($post->ID)) ?></h1>
<h1><?php echo $this->escapeHtml($post->getPostTitle()) ?></h1>
</div>
<div class="post-view">
<p class="post-date when"><?php echo stripslashes($this->__('<span class=\"by-author\"> by %s</span> on %s.', $post->getAuthor()->getDisplayName(), $post->getPostDate())) ?></p>
<?php echo $this->getBeforePostContentHtml() ?>
<div class="post-entry entry std<?php if ($post->getFeaturedImage()): ?> post-entry-with-image<?php endif; ?>">
<?php if ($post->isViewableForVisitor()): ?>
<!-- --><?php //if ($featuredImage = $post->getFeaturedImage()): ?>
<!-- <div class="featured-image left"><img src="--><?php //echo $featuredImage->getAvailableImage() ?><!--" alt="--><?php //echo $this->escapeHtml($post->getPostTitle()) ?><!--"/></div>-->
<!-- --><?php //endif; ?>
<?php echo $post->getPostContent() ?>
<?php else: ?>
<?php echo $this->getPasswordProtectHtml() ?>
<?php endif; ?>
</div>
<?php echo $this->getAfterPostContentHtml() ?>
<?php echo $this->getCommentsHtml() ?>
</div>
<?php endif; ?>
I'm working on WordPress through the Fishpig WordPress integration for Magento so the file path is template/wordpress/post/view.phtml.
Tomas is correct; you cannot use WordPress code in a Magento template file, even if that template file is integrating WordPress. None of the WP library code is included so the WP functions that you include do not exist.
It is still possible to get all of the WP data you require via Magento code though. To get a posts categories, use the following code:
<?php $categories = $post->getParentCategories() ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php echo $this->escapeHtml($category->getName()) ?>
<?php endforeach; ?>
<?php endif; ?>
try write without $post like.
get_the_category( get_the_ID() );
instead of $post->get_the_category($post->ID)
function getPages() {
$pages = Mage::getResourceModel('wordpress/page_collection')
->addIsViewableFilter()
->orderByMenuOrder()
->setOrderByPostDate()
->load();
return $pages;
}
$pages = getPages();
foreach ($pages as $post):
Collection goes here
endforeach;
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.