So I tried creating some code that will display an icon on a post depending on the category. I tested it and it just brings up the alt text, the img element gets displayed in the code and the directory is correct, but no image? Is there something I'm missing, something I'm not doing with the images? Thank you.
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }
Try this if you created images folder yourself inside the wp-content folder.
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="'.content_url().'/images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }
If the images are in the theme folder then you can use this:
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="'.get_template_directory_uri().'/images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }
Related
Currently it add nofollow at all products across website, how to make it at is_front_page() only?..
function woocommerce_template_loop_product_title() {
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Change the value of your rel attribute. So, when the user is at the frontpage, rel will have a value of nofollow. Otherwise, the value will be "" empty.
<?php
function woocommerce_template_loop_product_title() {
// Add the $nofollow
$nofollow = "";
if(is_front_page() ){
$nofollow = "nofollow";
}
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '"><a href="' . get_the_permalink() . '" rel='.$nofollow.'>' . get_the_title() . '</a></p>';
?>
I've got a foreach loop that is displaying a list of subpages Title, Content and URL. I can't seem to display the featured image though! Here's what I have:
<?php
$pageChildren = get_pages('sort_column=menu_order&hierarchical=0&child_of='.$post->ID);
if ( $pageChildren ) {
foreach ( $pageChildren as $pageChild ) {
echo '<div style="position:relative;float:left;width:100%;margin:0 0 20px 0;border-bottom:1px dashed #cdcdcd;padding:0 0 20px 0">';
//FEATURED IMAGE HERE
echo '<a class="newstitle" href="' . get_permalink($pageChild->ID) . '">' . $pageChild->post_title.'</a><br /><br />';
if (!empty($pageChild->post_content)){
echo '<p>'.$pageChild->post_content.'</p> ';
echo '<a class="readmore" href="' . get_permalink($pageChild->ID) . '" style="float:left">Read More ยป</a>';
}
echo '</div>';
}
}
?>
you must use
<?php echo get_the_post_thumbnail($pageChild->ID ); ?>
to get the post thumbnail as well page
Working with a Wordpress theme and currently the icons in the header open in the current window which will take traffic away from the site. I would like them to open in a new window/tab. I know target="_blank" will get the result I need, just not sure of the syntax.
foreach( $gdl_social_icon as $social_name => $social_icon ) {
$social_link = get_option($social_icon['name']);
if( !empty($social_link) ) {
echo '<div class="social-icon"><a href="' . $social_link . ' ">' ;
echo '<img src="' . $social_icon['url'] . '" alt="' . $social_name . '"/>';
echo '</a></div>';
}
}
If I understand correctly what you're after, all you need is
echo '...<a href="' . $social_link . ' " target="_blank">';
Replace 4 line:
echo '<div class="social-icon"><a target="_blank" href="' . $social_link . ' ">' ;
if ( $cat ) echo '<a class="custom-cat-btn" href="#cat-' . $cat . '">More ' . get_cat_name($cat) . ' Posts</a>';
This gives me:
http://mydomain.com/#cat-127
But I get a 404 when I click on the link
Can anyone help me get the actual link please
Thanks
You can retrieve the category link with get_category_link
So,
if ( $cat ) echo '<a class="custom-cat-btn" href="' . esc_url( get_category_link( $cat ) ) . '">More ' . get_cat_name($cat) . ' Posts</a>';
I am trying to style category names with different colours.
Here is my PHP:
<?php
foreach((get_the_category()) as $category) {
if (is_category('5') ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';}
else {
echo '' . $category->cat_name . ' ';}
}
?>
It is not returning errors, but it is not working either...
What am I doing wrong?
Here is the page I'm working on:
http://216.172.178.12/~saracimi/eng/
and the snippet of code is relative to the rectangular boxes at the center of the page.
Thanks a bunch!
is_category will return true when you are on that category archive. you need to check the category id so try:
foreach((get_the_category()) as $category) {
if ($category->cat_ID == 5 ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';
} else {
echo '' . $category->cat_name . ' ';
}
}