Pulling Default WordPress Category Name in Widget - php

I am trying to add the default category name on my site to a WordPress widget. Is there a way to pull this string (not the ID) automatically? I only have one category so i thought it would be easy, but none of the following are working:
<?php bloginfo('category'); ?>
<?php get_category(); ?>
<?php get_the_category(); ?>
<?php get_category([0]); ?>
<?php get_the_category([0]); ?>
<?php get_category([0]->cat_name); ?>
<?php get_the_category([0]->cat_name); ?>
<?php echo get_category(); ?>
<?php echo get_the_category(); ?>
<?php echo get_category([0]); ?>
<?php echo get_the_category([0]); ?>
<?php echo get_category([0]->cat_name); ?>
<?php echo get_the_category([0]->cat_name); ?>
Any help is appreciated...
UPDATE: 08/01/13 - Amal pushed me onto the right path. I ended up using the following and it worked perfectly...
<?php echo get_cat_name(get_option('default_category'));?>

Try this:
$category = get_category(get_option('default_category'));

Related

Notification with alert

I am trying to notify the user for their expired products. This works well. but i am not being able to display the product name in alert which is the variable $product->name.
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product expired!');</script><?php
}
?>
<?php endforeach;?>
Just add the product name to the string:
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product <?php echo $product->name; ?> expired!');</script><?php
}
?>
<?php endforeach;?>
Use echo
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
echo"<script>alert('Product expired!');</script>";
}
?>
<?php endforeach;?>

Fishpig Magento Integration

Because I am not a professional programmer, I can’t get the ACF integration up for the fishpig magento running.
Bought both the FishPig-ACF Add-on and the ACF Pro.
Installed both and made a custom field named “repeater” and as the autor describes in his manual, I added this code to the /post/view.phtml:
<?php $value = $post->getMetaValue('repeater') ?>
So my view.phtml looks like this:
<?php
/**
* #category Fishpig
* #package Fishpig_Wordpress
* #license http://fishpig.co.uk/license.txt
* #author Ben Tideswell <help#fishpig.co.uk>
*/
?>
<?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->getPostTitle()) ?></h1>
</div>
<div class="post-view">
<p class="post-date when"><?php echo stripslashes($this->__('This entry was posted on %s<span class=\"by-author\"> by %s</span>.', $post->getPostDate(), $post->getAuthor()->getDisplayName())) ?></p>
<?php echo $this->getBeforePostContentHtml() ?>
<?php $value = $post->getMetaValue('repeater') ?>
<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; ?>
But in fronted the ACF is not shown.
THX for any help
You have copied and pasted the following code:
<?php $value = $post->getMetaValue('repeater') ?>
This code generates the repeater value and saves it in a variable named $value. That's it. This code doesn't do anything with this value or print it to the screen so the fact that nothing is displayed is correct.
To view what's inside the field, try the following:
<pre><?php print_r($post->getMetaValue('repeater')) ?></pre>
The above code will print out the value of the repeater field to the screen. Assuming you have set a value for this field for the current post, this value be an array containing the data that you set. You will then need to make use of foreach loops to cycle through the array and process/display the data.

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

Magento static Blocks is not displaying properly.

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Categories