I want to display placeholder image if there is no category image and no featured image for a post
Then if there is a category image display then instead
Then if there is a featured image for a post display this instead
<?php if (has_post_thumbnail()) { ?>
<img src="<?php echo the_post_thumbnail_url(); ?>" class="single-blog-post-image" alt="Post Featured Image" />
<?php } else {
$category = get_the_category();
$categoryImage = 'http://akjservices.foamydev.com/wp-content/uploads/'.$category[0]->category_nicename.'.png';
if (!has_post_thumbnail() && file_exists($categoryImage)) {
?>
<img class="single-blog-post-image" src="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $category[0]->category_nicename ; ?>.png" alt="Category Featured Image" />
<?php } else { ?>
<img src="/wp-content/themes/akj/img/placeholder-part-image.jpg" alt="AKJ Services Default Image" class="single-blog-post-image">
<?php } }?>
It is working apart from it only gets the category image from the very first category nothing else
How can I amend my code to fix it so I can get any post category image?
Can see an example here
http://akjservices.foamydev.com/product/fr-sfj-2-7-5k/ => default placeholder image
The code specifies that the image name must be the same as the category name for example A06B-6044.png - however happy to ignore this and for it just to show the category image no matter what the name is.
You should loop the categories, You are accessing the only first one, don't forget to remove [0] after $category then.
$categories = get_the_category()
foreach( $categories as $cateogry ){
$category->category_nicename;
...
}
Related
I have a wordpress project that is using ACF fields to pass images/video into a carousel. How would I get the alt text for the associated image?
I have tried to get_field('image') and various get_sub_field() calls, but image does not seem to be a field even though get_sub_field('still_image_url') and get_sub_field('image_link') are already pulling in the respective data for those fields.
I'm not even sure how to get the id for the image. Another php file is using the_ID();, but that is not working here.
<?php while (have_rows('top_slider')) : the_row(); ?>
<?php
$video_url = get_sub_field('video_url');
$video_url_responsive = get_sub_field('video_url_responsive');
$video_link = get_sub_field('video_link');
$image_url = get_sub_field('still_image_url');
$image_link = get_sub_field('image_link');
$has_target = strpos($image_link, '/') !== 0;
?>
Make sure you are using the return format of the Image either as IMAGE ARRAY or IMAGE ID.
Use the below code to get the ALT tag of the image if the return format is IMAGE ARRAY.
<?php
$image =get_sub_field('image');
if( !empty($image )): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
Use the below code to get the ALT tag of the image if the return format is IMAGE ID.
$image_id = get_sub_field('image');
$img_url = wp_get_attachment_image_src($image_id, 'full');
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);
?>
<img src="<?php echo $img_url[0]; ?>" width="<?php echo $img_url[1]; ?>" height="<?php echo $img_url[2]; ?>" alt="<?php echo $image_alt; ?>" title="<?php echo $image_title; ?>">
Here the "image" denotes the field name which you set while creating the field.
get_sub_field('image');
Refer the image for understanding about Field Name, Return Format etc
You can see the image id and other details on the REST API if you will set the options to show on REST API.
After adding the contents to WP REST API, you can display the pages' content on the WP REST API as you can use this example link:
https://write-your-site-url-here/wp-json/wp/v2/pages
Hope you're well.
I have got the below code working as intended, but is there a way of ONLY showing the div 'listinggallery' if there are images returned?
At the moment, it works great if there are images in the listing, but if there are no images, then I have an empty styled div showing. Ideally I want create a rule to say "IF listingimage 'true' then show 'listinggallery'".
I have tried placing the 'listinggallery' div elsewhere within the code but just seems to crash my site, so hoping I can create a rule?
Kind regards,
Spencer
<div class="listinggallery">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1024, 768); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id); //Get the class which is a extra field (See below how to add extra fields)
?>
<div class="listingimage">
<div class="thumbnail">
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<a href="<?php echo $full_image_url; ?>" class="fancybox">
<img src="<?php echo $thumbnail_image_url; ?>" alt="<?php echo $title; ?>" title="<?php echo $title; ?>">
</a>
<?php if( !empty($url) ){ ?></a><?php } ?>
</div>
</div>
<?php endforeach; endif; ?>
</div>
If you move the creation of the <div> inside the block which decides if there is anything to display...
<?php
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
// Output start of gallery div
?>
<div class="listinggallery">
<?php
//Cool, we got some data so now let's loop over it
foreach($images as $image):
// rest of code as it currently is
endforeach;
// Close of gallery div
?>
</div>
<?php
endif;
?>
I would like to know how to show a certain gallery in magento, on a page.
I created a static block with name and identifier "category_listing" and put this code below:
{{block type="catalog/product_list" column_count="4" category_id="366" template="catalog/product/list.phtml"}}
Note that I want to show only the category 366.
And on the "mypage.phtml" I put this:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('category_listing')->toHtml(); ?>
Now when I enter the page appear several categories, not just the category 366 (would be 3 products):
http://s24.postimg.org/q9o65red1/Captura_de_tela_2015_05_22_08_14.jpg
thank you
<?php
print $this->getLayout()
->createBlock("blockname")
->setTemplate("yourtemplate.phtml")
->toHtml();
?>
and in your phtml file you define the function to get the product from a particular id
<?php
$categoryid = 366;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?>
</a>
<?php
}
?>
you want to show only the category with id 366 but in your xml you defined the id 7?
-David
Using Advanced Custom fields I've created a simple field that allows me to have a custom banner image of each page/post at the top of the page. My issue is that it's displaying the banner image of the most recent post at the top of the category page too. I'd like to be able to add a customized image for the categories but I'm not sure where to go from here.
Here is my code:
<?php if(is_front_page()) : ?>
<div class="homepage-slider">
<?php layerslider(1) ?>
</div>
<?php else : ?>
<?php
if( get_field('banner_image'))
{
echo '<img src="';
the_field('banner_image');
echo '">';
}
else
{
echo '';
}
?>
<?php endif; ?>
I have 4 main categories in an opencart site.
I want to have a different header image for the 4 separate categories.
How can I get the current category name and put an if statement to select the image for the header?
Current header image code:
<div id="headerWrapper">
<div id="header">
<div class="div1">
<div class="div2">
<?php if ($logo) { ?>
<img src="<?php echo $logo; ?>" title="<?php echo $store; ?>" alt="<?php echo $store; ?>" />
<?php } ?>
</div>
</div>
</div>
You will need to work out if the path is set and the route is product/category to begin with, to check if you are even on a category page. Then you will need to use that information in a switch really (rather than a large if else listing). Can you give more detail on what it is you are wanting to change in the code above? Is it the logo or are you wanting to add a class to the header that you can then use to style via your stylesheet
To find out if you are in the category, use this in the index() method in catalog/controller/common/header.php
$get = $this->request->get;
$this->data['cat_id'] = false;
$this->data['cat_name'] = '';
if(!empty($get['route']) && $get['route'] == 'product/category' && !empty($get['path'])) {
$cats = explode('_', $get['path']);
$cat_id = array_pop($cats);
$cat_id = (int) $cat_id;
if($cat_id) {
$this->load->model('catalog/category');
$result = $this->model_catalog_category->getCategory($cat_id);
$this->data['cat_id'] = $cat_id;
$this->data['cat_name'] = $result['name'];
}
}
This will give you two variables to use in your template's common/header.tpl file
$cat_id and $cat_name
$cat_id will be the current category id, or false if it's not a category page
$cat_name will have the current category name if one exists, or an empty string if it doesn't