Hide Empty Tab on Magento Frontend Product Page - wordpress

We installed Wordpress on our server and the Fishpig extension that integrates Wordpress and Magento. We want to make use of the extension's ability to associate blog posts and products by adding related posts to a new tab on the frontend product page (where the product description and upsell products are). I got all of that to work--there is a new tab on the product pages titled "Related Blog Posts" and when selected, it displays the post titles and excerpts exactly how I wanted it to. The problem is, that tab displays even when there are no related blog posts. How do I hide it when it's empty?
There's probably something simple that I'm missing. Here's how I added the tab:
1) This file app/design/frontend/base/default/layout/wordpress.xml establishes which block the related posts are displayed in on the frontend product page.
In this file, near the bottom, I changed the reference name from product.info.additional to *related_blog_posts*.
2) To the file: app/design/frontend/default/{template}/layout/catalog.xml near line 210, I placed the following code. I put it between similar code portions for the product description and the product upsells.
<action method="addTab" translate="title" module="catalog"><alias>related_blog_posts</alias><title>Related Blog Posts</title><block>catalog/product_list_relatedposts</block><template>catalog/product/list/relatedposts.phtml</template></action>
3) Added a new file: app/code/local/Mage/Catalog/Block/Product/List/relatedposts.php Added the following code to this file:
class Mage_Catalog_Block_Product_List_Relatedposts extends Mage_Core_Block_Template
{
protected $_list;
public function __construct()
{
parent::__construct();
$this->setTemplate('catalog/product/view/additional.phtml');
}
public function getChildHtmlList()
{
if (is_null($this->_list)) {
$this->_list = array();
foreach ($this->getSortedChildren() as $name) {
$block = $this->getLayout()->getBlock($name);
if (!$block) {
Mage::exception(Mage::helper('catalog')->__('Invalid block: %s.', $name));
}
$this->_list[] = $block->toHtml();
}
}
return $this->_list;
}
}
4) Added a new file: app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml and added the following code to this file:
<?php foreach ($this->getChildHtmlList () as $_html): ?>
<div class="collateral-box">
<?php echo $_html ?>
</div>
<?php endforeach; ?>
5) In the file app/design/frontend/base/default/template/wordpress/post/associated/list.phtml I changed the following code:
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</li>
<?php endforeach; ?>
</ul>
To:
<div class="related-posts">
<?php foreach($posts as $post): ?>
<h3><?php echo $this->escapeHtml($post->getPostTitle()) ?></h3>
<?php $post->setExcerptSize($this->getExcerptLength()) ?>
<p class="related-post-excerpt"><?php echo $post->getPostExcerpt() ?></p>
<?php endforeach; ?>
</div>
This last change adds an excerpt to each related post instead of just displaying the title.
6) Cleared caches and recompiled the site.
Summary: The new tab appears on the frontend product page and the related blog posts are appearing within the tab as they should. The tab, however, displays even when there are no related blog posts for that product. I have tried multiple ways of wrapping the code in Relatedposts.phtml in if/count conditions but I can't get anything to work. How do I prevent my new tab from appearing when there is no content?

I would try something like this or like you had mentioned some sort of count, in
app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml
<?php
if($this->getChildHtmlList()): ?>
<?php foreach ($this->getChildHtmlList () as $_html): ?>
<div class="collateral-box">
<?php echo $_html ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
You also need to ensure that this block is not returning any white space either because it will be interpreted as content and create the tab. I also had a custom tab and this was the issue for me. If you are having trouble you should Zend_Debug::dump() the $this->getChildHtmlList() and see what is getting generated.

Related

Custom post type archive with date condition

Am using The7 Theme. Since I wanted to create a NEWS section to post latest news (apart from the Blog, which is separate), I used the Custom Post Type UI plugin to create a custom post type named news
I created a new file in my child theme, by copying the contents of the parent theme's archive.php file and named it as archive-news.php, with a minor modification:
<?php
/**
* Archive pages.
*
* #since 1.0.0
*
* #package The7\Templates
*/
defined( 'ABSPATH' ) || exit;
$config = presscore_config();
$config->set( 'template', 'archive' );
$config->set( 'layout', 'masonry' );
$config->set( 'template.layout.type', 'masonry' );
get_header();
?>
<!-- Content -->
<div id="content" class="content" role="main">
<div class="news_years text-center">
<?php for( $i = date('Y'); $i >= 2009; $i-- ) { ?>
<?php echo $i; ?>
<?php } ?>
</div>
<?php
//the_archive_description( '<div class="taxonomy-description">', '</div>' );
if ( have_posts() ) {
the7_archive_loop();
} else {
get_template_part( 'no-results', 'search' );
}
?>
</div><!-- #content -->
<?php do_action( 'presscore_after_content' ); ?>
<?php get_footer(); ?>
So the modification done was that I commented out the description that would be shown at the top of the page, and added year buttons.
After this, when the www.mysite.com/news was accessed, it pretty well loaded all my news items and when clicking an item, it loaded the detailed page as well. And the pagination also works very well, as it inherits the parents theme's template files.
The problem am facing is, I want to make it display year wise items also. Say for example, right now, when accessing the www.mysite.com/news url, it loads up all the News items with pagination. I want to filter it based on a particular year. That's why I included the additional FOR loop in the above code.
Upon searching, I found that the WP Query has date parameters that can be used which will resolve my issue.
But am not sure how to apply this as this archive page is already doing the fetching of posts. And also, how to pass the year in the URL as well. Any pointers would be highly appreciated.
If this is not the right forum to ask this question, please feel free to move this thread to the apt forum. Thank you.

Accessing post list from different block

Hi there,
I'm trying to implement a slider on the Wordpress (fishpig) homepage. I've created a new phtml template and added a slider block in the xml file. So far so good, markup gets rendered to the homepage where I want it to.
But the loop doesn't run, obviously because $this->getPosts() is referencing a different class than the Fishpig_Wordpress_Block_Post_list.
My question is how can I access the post list from within the core/template slider block? I'm totally new to Magento so I'm just starting to wrap my head around the concept of blocks and how the whole templating system works.
I've tried
$className = Mage::getConfig()->getBlockClassName('Fishpig_Wordpress_Block_Post_list');
$block = new $className();
$block->getPosts();
and
$this->getLayout()->getBlockSingleton('Fishpig_Wordpress_Block_Post_list')->getPosts();
but to no avail. Can someone point me in the right direction?
When you include your new block and template in the XML, change the block type to "wordpress/sidebar_widget_posts". This will allow you to use the getPosts() method.
Alternatively, you could build your own collection of posts directly in your template:
<?php $posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->setOrderByPostDate()
->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li class="item">
<a href="<?php echo $post->getPermalink() ?>">
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

WordPress page content in Modal Pop-Up

I am working on a WordPress gallery website which has only three pages: Home, Gallery, and Bio (http://adamgreener.com/).
When you click Bio, the Bio content pops up, powered by Easy Modal plugin (the content is manually typed in HTML in the plugin settings).
I also have the exact same HTML content in a WordPress page (which mobile viewers see, rather than a popup).
The page is very simple to edit, but the Modal content is not so friendly to the average user. I am seeking a way that I can allow the user to edit only the Bio page, and have that modal content update at the same time.
What would be the best route for such an action?
Thanks in advance!
You could use get_page to get the page content, and a shortcode to display it in the popup. In the functions.php file of your WordPress theme, e.g.:
add_action( 'init', 'greener_shortcode_init', 11 );
function greener_shortcode_init()
{
add_shortcode( 'greener_bio', 'greener_bio_shortcode' );
}
function greener_bio_shortcode( $atts )
{
$page_id = 123; // the ID of the Bio page
$page_data = get_page( $page_id );
$return = '';
$return .= '<h2>' . $page_data->post_title . '</h2>';
$return .= $page_data->post_content;
return $return;
}
Then, in your modal, use the shortcode:
[greener_bio]
There are two possible ways to accomplish what you're going for, both described in depth in my blog post: https://allurewebsolutions.com/open-wordpress-post-modal-without-plugin
You can also just use my plugin for this: https://wordpress.org/plugins/wp-post-modal/
First Method
The first method requires modifying the template that serves the content you want to appear in the modal. For example, you have a template called modal-content-template.php.
Inside that template we wrap the content we want loaded into the modal with:
<?php if (!$_GET) { ?>
// content that gets loaded on mobile
<?php } ?>
Full example of a template where we are excluding the WordPress header and footer:
<?php if (!$_GET) { get_header(); } ?>
<article>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="page-title"></h1>
<section class="entry-content cf">
<?php the_content(); ?>
</section>
</article>
<?php if (!$_GET) { get_footer(); } ?>
CodePen example for the JS required to make this work
Second Method
The second method is a little more elegant. We use the modalContent.load(post_link + ' #modal-ready'); method to load the modal content.
The modal contact is wrapped in a wrapper container with ID #modal-ready. This can be done without even touching the page template using a function that hooks into the content.
add_action('the_content', 'wrap_content');
function wrap_content($content)
{
return '<div id="modal-ready">' . $content . '</div>';
}

Password protecting not working for custom post type single-template.php? - Wordpress

I have created a custom post-type 'Clients' where admin user can create new clients, add pictures and details to post, then password protect the page so only a particular client can access the content.
For displaying content of this post-type on the front end, I'm using a single-clients.php template. It displays the content perfectly, but the password protect function does not display the form and hide the content, even if I'm in a different browser, cache cleared/logged out of Wordpress (viewing it as a regular end-user would).
What might I be doing wrong here?
<?php get_header(); ?>
<div class="container-client">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Display all fields and content for post-type
<?php endif; ?>
<?php endwhile; else: ?>
<div class="alert-box error">Sorry, this page no longer exists :( — Back to home</div>
<?php endif; ?>
</div>
<?php get_footer(); ?>
This is roughly how my single-clients.php page is setup. Is there any way to manually display the password function so that when end-user visits page, the content is hidden and password form is displayed?
I had exactly this problem and after some trying and reading the codex I came up with this solution:
<?php
add_filter('single_template', function($single_template) {
global $post;
if ($post -> post_type == 'your-custom-post-type') {
if (!post_password_required()) {
$single_template = 'path-to-your/single-your-custom-post-type.php';
}
}
return $single_template;
});
?>
This way the page is only rendered in the custom sinlge view after entering a password, if it is password protected.

Resources