Accessing post list from different block - wordpress

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

Related

configure parent/child relationship between 2 Custom Post Type only in child

I would like to create a relationship between 2 Custom Post Type avoiding that the user has to go to both to create the link.
In absolute terms, I would like the user to go to only one Custom Post Type (writing) which remains fairly basic: A title field, a text field, an image field, a link field for documents or url, and finally a selector field to select its parent (created with ACF).
In the hierarchy of the site, these posts created are at the end of the race, therefore, are only post children. They will display with "single.php".
The site obviously contains different menus which also have different sub-menus, a front-page.php for the home page, for the main menus we have the archive pages and for the sub-menus of the single pages.
The other Custom Post Types are the site menus which each contain their respective submenus. The user doesn't have to go here, hence I would like to create a parent/child relationship only by going into the child Custom Post Type.
For example, in my home page (front-page.php), I have a "Company" part if I click on it, I arrive on "archive.creb.php" where there is "Office", Secretariat" and "Management", if I click on "Office" I arrive on "single.creb.php", in this part, there is "the team" and "the calendar", if I click on "the team", I arrive at "single.php".
The team page (just like the "calendar" page) is an article written by the user.
So I'm looking for a way so that, when the user creates an article, he can choose where it will be (with the ACF selector field), and therefore choose its parent AND in addition, that the link of the article appears in this parent.
I know how to do this kind of operation by configuring a relational field in the 2 Custom Post Types concerned with ACF, but not with just one.
I did some research, especially on the ACF doc and I found this:
https://www.advancedcustomfields.com/resources/relationship/
https://www.advancedcustomfields.com/resources/querying-relationship-fields/
So I tried to adapt the ACF example with my code, but it's not easy because it's not quite the same... By schematizing the ACF example to mine, it gives this:
In both cases there is indeed a selection field creation, except that in the ACF example its selector chooses the child and I choose the parent. So I tried to reverse, using the code from single.location.php in my single.creb.php.... But it doesn't work.. my single.creb.php page shows a link loop to himself
So... I hope I was clear in my question! Hoping for an answer
Here are some codes :
functions.php
function custom_post_type(){
$arg = [
'public'=> true,
'label'=> 'CREB',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-groups'
];
register_post_type('creb', $arg);
$arg = [
'public'=> true,
'label'=> 'redaction',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-book-alt'
];
register_post_type('redaction', $arg);
}
add_action('init', 'custom_post_type');
single-creb.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single CREB</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('textesousmenu');
if($redac){
echo $redac;
};
};
};
?>
</div>
</main>
<?php get_footer(); ?>
single.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('texte');
if($redac){
echo $redac;
};
$img = get_field('img');
if($img){
echo wp_get_attachment_image($img, 'modale');
}
if (have_rows('liendocurl')){?>
<ul class="">
<?php while (have_rows('liendocurl')){
the_row();
$nom = get_sub_field('nomlien');
$docurl = get_sub_field('urllien');?>
<li>
<a href="<?php echo $docurl; ?>" target="_blank">
<?php echo $nom ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
$lien = get_field('relation');
if($lien){?>
<ul>
<?php
$name = get_the_title($lien->ID);
$link = get_permalink($lien->ID);
?>
<li>
<a href="<?php echo $link;?>">
<?php echo $name; ?>
</a>
</li>
</ul><?php
}
};
};
?>
</div>
</main>
<?php get_footer(); ?>
thank you in advance

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.

Hide Empty Tab on Magento Frontend Product Page

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.

Get a specific page's excerpt within the loop Wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

Views title is blank

I'm using the Drupal views module to create an overview of nodes. In the view i created i configured the Title. But now when the page is rendered the title isn't shown. It is shown in the breadcrumb etc. But not in the grid template, also if i use another template it still doesn't show. Any idea what this can be? I tried looking for it, but my experience with Drupal is very limited.
I checked the drupal_get_title etc. and it is always returning the title, i think something goes wrong in the views module, but i don't know what :s
Kind regards,
Daan
The problem is most likely how you print the page title. If you want it to happen globally, you should print it in the page.tpl.php. Have you inspected the $title variable in the page template? That is what it's usually called.
i idd removed the title from page.tpl.php, but i did this because i thought it should be printed in the views template. When you check views-view-unformatted.tpl.php etc you see this:
<?php if (!empty($title)): ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
<div class="<?php print $classes[$id]; ?>">
<?php print $row; ?>
</div>
<?php endforeach; ?>
so i thought $title would print the title, but it is fixed just by adding it in my page.tpl.php

Resources