newbie to writing PHP here. Many hours of searching and don't know if I'm looking in the right places.
I have a custom taxonomy (directory) with custom post categories for the listings.
I am using Advanced Custom Fields to add an image to each custom category (directory-image).
I am currently displaying the custom categories perfectly fine within a custom directory page template using the code below.
$cat = get_query_var('cat'); $args = array(
'orderby' => 'id',
'child_of' => $cat,
'hide_empty' => 0,
'taxonomy' => directory_categories
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<a href="' . get_category_link( $category->term_id ) . '" class="cat-icon-view">' . "\n";
echo '<img src=" " width="90%" height="164"></img>' . "\n";
echo ' <h5>' . $category->name . '</h5>' . "\n";
echo '</a>' . "\n";
}$
It has space to put the image, however I am not confident at writing php markup to include the image that is outputted from the custom image field under the custom category.
<?php the_field('directory-image', false); ?>
I have read many pages on writing PHP within PHP but I wasn't able to make it work and I'm sure there's a better way: Help!
Write PHP code in PHP
How to write php within html within php?
Display Categories with Image in Multiple Pages of Homepage - Magento
If you already have added a custom image field to category taxonomy, you should access it via tax meta. For the reference use official doc.
foreach ($categories as $category){
$image = get_term_meta($category->term_id, 'directory-image', true);
}
Related
Whenever I try to get the category name of a post, it returns an empty "array". I am not proficient with PHP in WordPress, though I have tried many things to get this to work. I have tried using both get_the_category() and the_category(', '), but the latter pulls the category names and puts them entirely at the beginning of the function (it looks like it strips the div off of them entirely).
I'd like to display a list of categories that each post belongs to and still keep them within my div ID's. Strangely enough (to me), get_the_date() works perfectly fine as is, but get_the_category() doesn't.
"Array" in orange should be showing category names separated by commas (ideally):
This is the code I'm using to get the posts inside of my functions.php file.
function ctmblog_posts() {
// Query Arguments
$ctmblog_args = array(
'orderby' => 'date',
'ignore_sticky_posts' => '1',
'cat' => 8,88,87,5 // Use the category id, can also replace with category_name which uses category slug
//'category_name' => array(SLUG OF FOO CATEGORY),
);
//Loop to display 10 recently updated posts
$ctmblog_loop = new WP_Query( $ctmblog_args );
$counter = 1;
$string .= '<ul><div id="timeline">';
while( $ctmblog_loop->have_posts() && $counter < 10 ) : $ctmblog_loop->the_post();
$string .= '<div id="timeline_event"><span id="timeline_date">'. get_the_date() .'</span> — '.'<span id="timeline_category">' .get_the_category() . '</span>'.' <br><li> ' .get_the_title( $ctmblog_loop->post->ID ) . '</li></div>';
$counter++;
endwhile;
$string .= '</div></ul>';
return $string;
wp_reset_postdata();
}
//add a shortcode
add_shortcode('blog-posts', 'ctmblog_posts');
If I could get any pointers, I'd really appreciate it! This is a great learning experience for me. I've searched the web and tried solutions other people have suggested to others, but I think this might be a unique case for me.
Let's read the documentation for the get_the_category() function: https://developer.wordpress.org/reference/functions/get_the_category/
It should work, right? Let's remember that in WordPress each post can have multiple categories - this results in the function returning an array.
Code by wordpress.org user Stefano
$categories = get_the_terms( $post->ID, 'taxonomy' );
// now you can view your category in array:
// using var_dump( $categories );
// or you can take all with foreach:
foreach( $categories as $category ) {
echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
}
So, I do have a function to get related posts in my functions.php for Wordpress. It is working fine BUT the images do not have an alt tag.
Part of my code:
$img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.png" alt="' . get_the_title() . '" />';
$related .= '<li>' . $img . '<p>' . get_the_title() . '</p></li>';
Any idea how I can tell Wordpress to add the title as alt tag, like it is with the title tag inside the link?
thanks
Try to put the POST|PAGE ID as a get_the_title() parameter.
Inside the loop, single.php or page.php:
get_the_title( get_the_ID() );
Outside the loop: You need to get the post ID before.
$post_id = X (there are many ways to get the ID);
get_the_title( $post_id );
To help you how to get the post ID, I want to know more about your file.
What file is? Single, Page, Category (inside loop)...
So I found the solution. Dont know if its a special thing because of the Genesis Framework I`m using or if this is a general Wordpress.
However with adding attr to the genesis_get_image array it was done.
'attr' => array ('alt' => 'Related Image')
Since my last question was answered pretty quickly, I thought I'd try my luck again.
I am trying to create a gallery within a custom post type I have created. I would like to be able to add the images/gallery to the post through the wordpress admin editor, but then have a function pull the images, wrap them in divs, and replace the existing gallery with the new images.
I would like to do the because I would like the images to fit into a grid of different sized images. For example, image 1 would be the full width, image 2 would be half the width, image 3 quarter and so on.
I have tried two methods, one being get_children()
$featuredImage = get_post_thumbnail_id( $post->ID );
$imageArgs = array(
'numberposts' => 5,
'order' => 'DESC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_type' => 'attachment',
'exclude' => $featuredImage
);
$attachments = get_children($imageArgs, ARRAY_A);
$rekeyed_array = array_values($attachments);
$child_image = $rekeyed_array[0];
echo '<div class="project-img"><img src="' . $child_image['guid'] . '" class="project-image"></div>';
$child_image = $rekeyed_array[1];
echo '<div class="project-img w2"><img src="' . $child_image['guid'] . '"></div>';
$child_image = $rekeyed_array[2];
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>';
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>';
and the other being get_post_gallery()
$gallery = get_post_gallery( get_the_ID(), false );
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] AS $src )
{
?>
<div class="project-img">
<img src="<?php echo $src; ?>" alt="Gallery image" />
</div>
<?php
}
I haven't made much progress with the get_post_gallery(), but I sorta understand that I will have to use wp_get_attachment_url() to get the full sized images instead of thumbnails.
Now, two questions:
I'm a little confused about arrays, so how would I go about
selecting the first image in the array and wrapping it in a div with
class "image-large" and then the second image and wrapping it in a
div with class "image-medium"?
How do I replace the gallery/images I have added through the editor
with the new gallery/images? Right now, I get two instances of the
images, the original added through the editor, and the images
obtained through the functions.
EDIT
I figured out question 1, I think. Read up on associative arrays and realized you can do something like echo $gallery['src'][0]; to get the source url of each image. Still confused about question 2, though.
Figured it out.
//Remove original Gallery
function remove_the_first_gallery( $output, $attr ){
$output = '<!-- gallery 1 was here -->'; // Must be non-empty.
return $output;
}
add_filter( 'post_gallery', 'remove_the_first_gallery' );
That removed all galleries on the page. But since my new gallery isn't technically a post_gallery, it was left alone.
Im trying to create a custom shortcode which can be used to display the latest post, it should show the featured image for the post and the title for the post and all be wrapped in a link to the relevant article.
I have this code:
function latest_post_shortcode($atts){
$q = new WP_Query(
array( 'orderby' => 'date', 'posts_per_page' => '1')
);
$list = '<div class="latest-post">';
while($q->have_posts()) : $q->the_post();
$list = '' . the_post_thumbnail('latest-post', array('class' => 'img-responsive')) . '<br />' . get_the_title() . '';
endwhile;
wp_reset_query();
return $list . '</div>';
}
add_shortcode('latest-post', 'latest_post_shortcode');
I have managed to use this code to diplay the featured image but it is being moved from within the tag and placed at the top of my Wordpress page.
Can anyone update my code or show me a better way of creating this result?
Thanks
Nick
If you use get_the_post_thumbnail() instead of the_post_thumbnail(), things should work as you want them to.
Its because you are replacing the contents of $list variable.
You need to add them. So, use $list = $list + [Something] instead of $list = [Something]
Currently I'm working on a project which I have installed a Wordpress plugin to the site called 'Awesome Filterable portfolio Plugin'. This is a plugin to show portfolio items in categories.
The plugin is working fine and as aspected. However, my clients wants to show not all portfolio items by default, when going to the portfolio page, but only the three portfolio items from a category; in my case 'visitekaartjes'.
I already raised this question to the plugin developer, but never got a reply back on the Wordpress plugin support page.
My knowledge of PHP is limited, so i'm struggling to get this last item done. But, I have an idea were to look, but even after searching and trial & error for days, it doesn't get to the eureka moment. That's why i'm trying my luck here.
Here's the code:
$items = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'afp_items ORDER BY ' . $orderby);
if( $afpOptions['sort_cat'] == 'on' ){
$orderby = ' ORDER BY cat_name';
} else {
$orderby = '';
}
$cats = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'afp_categories' . $orderby);
?>
<?php
//AFP Main Container
$output='<div class="afp-clear"></div>
<div id="afp-container">';
//Start Echo Categories
$output.='<ul id="afp-filter">
';
foreach ( $cats as $cat ){
$output.='<li>' . $cat->cat_name . '</li>';
}
$output.='</ul>';
//End Echo Categories
//Start Echo Portfolio Items
$output.='<ul class="afp-items">';
$k = 1;
foreach ($items as $item ){
$output.='<li class="afp-single-item" data-id="id-' . $k . '" data-type="' . ereg_replace("[^A-Za-z0-9]", "", $item->item_category) .'">
<a class="fancybox" title="' . $item->item_description . '" href="' . $item->item_image . '"><img alt="" class="img-link-initial" src="' . $item->item_thumbnail . '"></a><br />
<ul class="afp-item-details">';
if($item->item_title != null) { $output.='<li><strong>' . $item->item_title . '</strong></li>'; }
if($item->item_client != null) { $output.='<li>' . $item->item_client . '</li>'; }
if($item->item_date != '0000-00-00') { $output.='<li>' . date("m/d/Y", strtotime($item- >item_date)) . '</li>'; }
if($item->item_link != null) { $output.='<li><a target="_' . $afpOptions['project_link'] . '" href="' . $item->item_link . '">Project Link</a></li>'; }
$output.='</ul>
</li>';
$k++;
}
$output.='</ul>
Hope somebody can help me with this.
Thanks in advanced,
Roland
If you want to allow a visitor to click a button and view more or all items you maybe should implement this clientside using JavaScript & CSS. (Unless you have thousands of items in the portfolio)
You set the height of the container to the height of 1 row of items. And then create a button that will extend this height, to show either one row at a time or to show all items at ones.
I actually implemented this myself on http://annual-insight.nl/referenties/ You can just check this file: http://annual-insight.nl/wp-content/plugins/ai-referenties/js/air-functions.js to see what I did there.
It requires your portfolio items to be of equal height.
Actually, the portfolio is a custom post type and the portfolio category is a custom taxonomy, used to group the portfolio items in categories. In this case, to query a custom post type with custom taxonomy, WordPress provides better and easy solution, for example, to query for three portfolio items with custom taxonomy (portfolio category) design you can write a query like this
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category', // Could be anything in your case
'field' => 'slug',
'terms' => 'design'
)
)
);
$query = new WP_Query( $args );
Now you can loop it like,
if($query->have_posts()): while()
while ($the_query->have_posts()): $query->the_post();
?><h2><?php the_title(); ?></h2><?php
endwhile;
endif;
This may doesn't answer your question but gives you an idea to do it properly.