WordPress Next and Prev posts - confused by categories - php

I've set up a WordPress navigation for my single.php template that gets the previous and next post's thumbnails like this:
<?php
// Newer posts
$nails_next_post = get_next_post('%link', '', FALSE, 3 ); // Get the previous post
$nails_next_post_thumbnail = get_the_post_thumbnail($nails_next_post->ID); // Get thumbnail
?>
<?php if ($nails_next_post != null) : ?>
<div class="post-nav-next">
<?php if ($nails_next_post_thumbnail != null): ?>
<?php echo $nails_next_post_thumbnail; ?>
<?php else : ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
<?php endif; ?>
<?php next_post_link('%link', 'Forward' , TRUE, 3 ); ?>
</div>
<?php endif; ?>
<?php
// Older posts
$nails_prev_post = get_previous_post('%link', '', FALSE, 3 ); // Get the previous post
$nails_prev_post_thumbnail = get_the_post_thumbnail($nails_prev_post->ID); // Get thumbnail
?>
<?php if ($nails_prev_post != null) : ?>
<div class="post-nav-previous">
<?php if ($nails_prev_post_thumbnail != null): ?>
<?php echo $nails_prev_post_thumbnail; ?>
<?php else : ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
<?php endif; ?>
<?php previous_post_link('%link', 'Back' , TRUE, 3 ); ?>
</div>
<?php endif; ?>
The problem I am having is that the links continue to point to the next or previous post in the current post's category, rather than just the next or previous post in the chronology (except posts in category 3 of course). I'm out of my deoth here. Does anyone have any thoughts? Thanks :-)

I think you are using the wrong parameters on the get_next_post and get_previous_post functions. You probably accidentally used the parameters which go with next_post_link/previous_post_link
You only need two parameters and both are optional:
http://codex.wordpress.org/Function_Reference/get_next_post
Simply try this:
$nails_next_post = get_next_post();

Related

Get category name from Magento

I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.
What I have done so far:
<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
<div class="container">
<div class="col-md-12">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
<?php else: ?>
<strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>| </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif ?>
</div>
I have taken this code from page/html/breadcrumbs.phtml.
I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?
If you want to show category name on category page. You can achieve this by 2 ways.
<?php echo $this->getCurrentCategory()->getName(); ?>
Or
<?php echo Mage::registry('current_category')->getName() ?>
On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends.
At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.
Here's the code, see if it works for you.
//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();
//set CatID to the second element of the array since the first element
//is the base category of which all others are children.
$_catID = $categoryIds[1];
//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);
//get the level of the current category
$level = $_category->getLevel();
//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){
// we want the second level category, since the first is the base category.
// ie if we call the default category 'base' and the product is in category
//base->foo->bar->baz we want to return the link to foo.
// while current category is deeper than 2 we ask it for it's parent
//category until we reach the one we want
while ($level > 2){
$parent = $_category->getParentId();
$_category =Mage::getModel('catalog/category')->load($parent);
$level = $_category->getLevel();
}
//Now we can build the string and echo it out to the page
$caturl = $_category->getUrl_key();
$_linkstring = 'http://www.yourwebsite.com/' . $caturl . '.html';
echo 'in category:';
echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
echo ' '. $_category->getName();
echo '</a>';
}
Get the category name, image, description from category id in magento
$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
Please put the category id in the load function

Editing a wordpress loop to bring in one page via its ID number

This loop brings in all the pages and shows it on a single page. How can I edit it so that I only bring in one page identified by its ID? (the page wont change)
Thanks
<section id="<?php echo $post->post_name;?>" class="page-area<?php echo $bgClass;?>"<?php echo $style;?>>
<div class="wrapper"<?php if($fullEmbed<>''):?> style="width:100%"<?php endif;?>>
<?php if($hideTitle!='Yes'):?>
<hgroup class="title">
<h1<?php echo $font;?>><strong><?php echo $mainHeading;?></strong></h1>
<?php if($subHeading<>''):?><p<?php echo $font;?>><?php echo $subHeading;?></p><?php endif;?>
</hgroup>
<?php endif;?>
<?php if($fullEmbed<>''):?>
<div class="full-embed"><?php echo van_shortcode($fullEmbed);?></div>
<?php else:?>
<div class="entry"<?php echo $font;?>>
<?php van_content(true,true);?>
</div>
<?php endif;?>
</div>
</section>
<?php endwhile;?>
You mean like:
<?php
$page_id = 123; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), WordPress will generate an error. By default, this will return an object.
echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
echo apply_filters('the_content', $page_data->post_content); // echo the content and retain WordPress filters such as paragraph tags. Origin: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
?>
Straight from the WP Codex http://codex.wordpress.org/Function_Reference/get_page

How do I do If Post Meta DOES NOT exist?

I have the following code:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Later in my code I want to be able to say that if buy-link does not exist - i.e. there is no data in that field - then do something, else do something different.
Not sure how to do this! Help appreciated!
(By the way, I posted this question to Wordpress Stack Exchange first. It was voted closed there because it apparently concerns PHP boolean logic more than Wordpress - https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)
<?php if($buycheck ==''){ /*stuff*/ } ?>
this will render $buycheck, and if it is empty == is equal to '' nothing.
You can set a global variable that you can check later to see if the buylink exists:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Then later on in your code:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
If you need the actual value, you can set a global containing the return value from get_post_meta so you can use the actual value.

Ad block won't place correctly

Basically, I need for the image at the bottom of the page with no text wrapped around it to be in the place of test post 4 (it's going to be a Google Adsense block but I am using images as a placeholder for now). I'm not sure how to do this, right now the code is the following:
<div class="google_ad_post">
<?php if ($count == 3) { ?>
<br /><img src="****" alt="post ad">
<?php } ?>
<?php $count++; ?>
</div>
Yet, the image is still at the bottom of the page. How can I fix this?
Here is the image:
I can't post pictures just yet so the URL to the image is http://i.imgur.com/7rw5B.jpg
I have to see your code to help you better with the Scripting Part, but something logical like this should work:
<?php
$count = 0;
foreach( $posts as $post ) : ?>
<?php if ($count == 3) { ?>
<div class="google_ad_post">
<img src="****" alt="post ad">
</div>
<?php } else { ?>
<div class="post" id="post-<?php the_ID(); ?>><div class="content"><?php the_content(); ?></div>
<?php } ?>
<?php $count++; ?>
<?php endforeach; ?>

How to hide "image" custom field if empty

I created an unordered list of custom fields and I wish to hide them if they are empty. For text custom fields I used the code:
<?php if (get_field('phone') != '') { ?>
<li><strong>Phone: </strong><?php the_field('phone'); ?></li>
<?php } ?>
However, I have a custom field which is for images, like this:
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
How can I hide the field if no image was uploaded (obviously, the above code won't work)?
Thanks in advance.
I think it should be
<?php if (get_field('logo') != ''): ?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php endif; ?>
Assuming the_field('logo') will return a falsy value if there are no images
if (the_field('logo')) {
?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php
}
<?php if( get_field('field_name') ): ?>
<p>My field value: <?php the_field('field_name'); ?></p>

Categories