EntityMalformedException when retrieving Drupal 7 custom field - exception

I am playing with Drupal and I am trying to add a second line to the site slogan.
The following is the piece of page.tpl.php where I am working.
<?php if ($site_name || $site_slogan): ?>
<div id="name-and-slogan" class="hgroup">
<?php if ($site_name): ?>
<h1 class="site-name">
<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>">
<?php print $site_name; ?>
</a>
</h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<p class="site-slogan"><?php print $site_slogan; ?></p>
<?php endif; ?>
<?php
/* ADDED */
$node = menu_get_object();
$siteslogan2 = field_get_items('node', $node, 'field_siteslogan2');
?>
<?php if ($siteslogan2): ?>
<p class="site-slogan2"><?php print $siteslogan2; ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
I basically added a new Content Type with a field called siteslogan2 (field_siteslogan2) and now I would like to retrieve and show it here.
The first problem is that the $node var is not defined (even if according to the documentation it should be). The second problem is that I receive EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() when I define it manually and run it.
First of all, is this the right approach to the problem? Secondly, why do I receive the EntityMalformedException and how can I fix it?
var_dump($node) produces NULL. It must be the way I get the $node content that is not good. The doc is a bit cryptic to me when it says:
$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).

This sort of thing is can be done using a preprocessing function in your theme's template.php to define and set a variable in the $varables array.
This variable then becomes available to whichever template ( page, node, form, etc ) you have done the preprocessing on. Your theme's template.php file will most likely have comments on how to do this.
for instance, doing this in the template.php creates or modifies the value of the variable $display_header, making it available for use in node.tpl.php
function yourthemename_preprocess_node(&$variables, $hook) {
$variables['display_header'] = false;
}
You can then modify the node template file to use this variable.
For something simple like a sub-slogan, you can add a setting to the theme, so it shows up on the theme's configuration page like any other theme setting.
This requires implementing this function in your theme's theme-settings.php:
function yourthemename_form_system_theme_settings_alter(&$form, $form_state) {}
This provides information to the settings form regarding the new setting. like this:
https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_form_system_theme_settings_alter/7
Hopefully that's enough to get you started.

Related

Wordpress Polylang adding pll_e breaks HTML

i'm using polylang to translate my blog site along with loco translate. i'm manually adding string translations which was working fine with get_theme_mod parts, but there is a place that i want to also add custom string translation, after i add manually it breaks html and css won't work then.
it should be seen like this after adding custom string translation ; works fine without pll_e
but after i add pll_e to the that part in index.html ;
<?php get_header(); ?>
<div class="content">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<?php echo get_template_part(pll_e ('inc/page-title') ); ?>
<?php endif; ?>
it breaks the html but translation works. its seen like this ; looks like this
does anyone knows the solution ? i think its about get_template_part and get_theme_mod because the same things that i've done with get_theme_mod parts works fine.
by the way there is difference like this with pll_e and without it.
without pll_e
with pll_e
i solved the problem by editing index.html like this ;
<div class="content">
<div class="page-title group">
<div class="page-title-inner group">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<h2> <?php echo get_template_part(pll_e('inc/page-title') ); ?> </h2>
<?php endif; ?>
</div><!--/.page-title-inner-->

Wrong post title displaying from wordpress loop

I'm building my own theme. I have a page set for my blog (with a template that I made) which I'd like to just display a few of my posts. It uses the following loop:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_post();
the_content();
?>
</div>
<?php
}
}
?>
The titles of my posts are "First Post, Second Post, Third Post, and Fourth Post" respectively. When the posts are displayed on the blog page, they are displayed in the correct order, but the titles of the posts are incorrect. The first post's title reads: "Second Post". The second post's title is: "Third Post", and so on until the last (most recent) post which has the title: "Blog" (the page title). What happened to the titles that they got so screwed up?
What I've Tried:
I've researched this a lot before I came here. I tried using get_the_title() instead but that lead to no titles being displayed. I've also tried using the_title_attribute() to no avail. I also understand that I shouldn't be using query_posts for this loop but I'm unsure which is the correct method to use for getting the posts in this particular case. Most of the info I've read was unclear though and didn't seem to fix the issue.
Any help is greatly appreciated.
Try something like this: untested
<?php
global $post;
$args = array();
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
YOUR HTML HERE
<?php endforeach;
wp_reset_postdata(); ?>
Okay Nevermind! I just found out what the problem was. I moved, "the_post()" to just after the while loop so now it reads like this:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
the_post();
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_content();
?>
</div>
<?php
}
}
?>
I found this solution while I was reading about the_post() in the wordpress codex. Turns out this function is setting up information for the next post in the line, so it shouldn't be mixed in with the html output for the current post.
In regards to whether or not I should be using "query_posts()" is still something I'm unsure of and willing to take any advice. But the loop in it's current form is working.

jdoc style values for Joomla 2.5 template

I'm creating an HTML5 template in Joomla 2.5 and I wanted to know what are my options for the style attribute in the code below?
<jdoc:include type="modules" name="top" style="???" />
Open templates\system\html\modules.php. There are defined system styles, like xhtml, rounded etc. You can also see the code how each style will be rendered.
If you want to add your own style, you need to create a new module chrome. In your template html folder (not system, don't edit above file), create file called modules.php.
Inside, make a function like this
defined('_JEXEC') or die;
function modChrome_mystyle($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="moduletable">
<?php if ($module->showtitle != 0) : ?>
<h3><?php echo $module->title; ?></h3>
<?php endif; ?>
<?php echo $module->content; ?>
</div>
<?php endif;
}
This way you can create custom module outputs, just edit the code the way you want.
Then, in your template file, include the module with
<jdoc:include type="modules" name="top" style="mystyle" />

Magento - split options of container1 and container2

In the default layout the options and add-to-cart-button are called by
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
I would like to split the configurable options from the add-to-cart and quantity field to show them on a different position in my layout. Any ideas or ready to use workarounds?
You can split it very easy (but I spent a lot of time to find it :) ) - if you look to the app/code/core/Mage/Core/Block/Abstract.php to PHPDoc of public function getChildChildHtml, you will see that the second parameter determined child block name. So, you can call first before the price block render
<?php echo $this->getChildChildHtml('container1', 'product.info.options.wrapper', true, true) ?>
and after price block rendered, call
<?php echo $this->getChildChildHtml('container1', 'product.info.options.wrapper.bottom', true, true) ?>
While your final solution will depend on where these blocks need to be moved/inserted in your layout, you can definitely split the “Add to Cart” product.info.options.wrapper.bottom out from configurable options product.info.container1 or product.info.container2 like this:
<catalog_product_view>
<reference name="product.info.container1">
<action method="unsetChild"><name>product.info.options.wrapper.bottom</name></action>
</reference>
<reference name="product.info.container2">
<action method="unsetChild"><name>product.info.options.wrapper.bottom</name></action>
</reference>
</catalog_product_view>
The easiest way to then show the “Add to Cart” button separately is to comment out the conditional in catalog/product/view.phtml which allows the product.info.addtocart block to be shown whether the product has options or not:
<?php if (!$this->hasOptions()): // Remove this condition ?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
...
<?php endif; ?>
</div>
...
<?php endif; ?>
Hopefully that helps you understand the structure of these blocks. Additional resources that may be helpful:
What is container1 and container2 in product view page in Magento?
How to move breadcrumb Block purely via local.xml?

How can I append a <span> tag to a <h1> heading in Wordpress

I need to add a spantag to an h1 heading in wordpress. I tried writing in through the CMS but it renders the spantag as text.
The site's title is a name and I want to give different styles to the words.
Suggestions?
Following David Thomas's advice I've written this: but it appends the span last and empty.
<h1>
<a href="<?php bloginfo('url'); ?>/">
<?php
$completeName = bloginfo('name');
$split = explode(" ",$completeName);
echo $split[0]."<span>".$split[1]."</span>"
?>
</a>
</h1>
You should be using get_bloginfo as Felipe suggested, but still pass in the 'name' parameter.
I was just working on the same code for the same reason and thought I should post for future visitors:
<?php
$completeName = get_bloginfo('name');
$nameParts = explode(" ", $completeName);
echo '<span>'.$nameParts[0].'</span> '.$nameParts[1];
?>
Apply the style to the h1 instead.
By the way, you probably want to edit the theme.
What about:
<? echo get_bloginfo('name', 'raw'); ?>
Can't test it for now, but here's the documentation about it:
bloginfo()
==> it just echoes the result of get_bloginfo() and there's a second parameter to this function
==> wptexturize(), a function that WON'T be called so beware!