My existing code outputs all posts in the portfolio category, however I want to also add a class to each item that is populated by the categories the post relates to. I've tried using get_the_category and think i'm nearly there but can't work out what I haven't quite done correct:
<?php query_posts('category_name=portfolio&order=DSC&orderby=ID&posts_per_page=20');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$portfolio_link = get_post_meta($post->ID, 'portfolio_link', true);
$categories = get_the_category($postID);
?>
<li class="<?php echo $categories->cat_name;?>">
<?php if ($portfolio_link) { echo "<a href='$portfolio_link'>"; } ?>
<?php the_post_thumbnail('small'); ?>
<?php if ($portfolio_link) { echo "</a>"; } ?>
</li>
<?php endwhile; endif; wp_reset_query();?>
At the moment no classes are output at all, but no errors are shown either!
get_the_category() returns an array so you need to iterate over it.
$catNames = array();
foreach($categories as $category) {
$catNames[] = $category->cat_name;
}
$classes = implode(' ', $catNames);
<li class="<?php echo $classes ?>">
Related
Am looking for a way to implement pagination on my categories
i tried to use paginated_links(); functions but it works only for posts, and not the actual categories. I have looked around and it seems this is a little trickier than I was expecting...
$cats = get_categories(
array('parent' => $cat->cat_ID)
);
// loop through the categries
foreach ($cats as $cat) {
echo '<div class="card">';
// setup the cateogory ID
$cat_id= $cat->term_id;
echo '<div class="card-body">';
// make images for the category
echo '<div class="card-title">' . $cat->name . ' <i class="fas fa-arrow-right"></i></div>';
echo '</div>';
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=3");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php echo '<div class="blog-list-points">' ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_title();?>">
<?php the_excerpt(); ?>
<hr>
</a>
<?php echo '</div>'?>
<?php endwhile; endif; echo ' </div> '// done our wordpress loop. Will start again for each category ?>
<?php }
// done the foreach statement ?>
I need to display buttons for all post categories. When I click on a button, it links to the archive page of that specific category. Is there a way to highlight the current category?
My code
<div>
<?php foreach(get_categories($args) as $category) { ?>
<?php echo $category->name; ?>
<?php } ?>
</div>
Please try this:
<?php
$selected_category = get_queried_object();
$current_category = $selected_category->term_id;
foreach(get_categories($args) as $category) {
$selected_class = '';
if( $category->term_id == $current_category ){
$selected_class = "selected_a";
}
?>
<a href="<?php echo get_category_link($category); ?>" class="<?php echo $selected_class; ?>" ><?php echo $category->name; ?></a>
<?php
}
?>
You can then add background CSS for "selected_a" class. Thanks
Quasi-noob here. I just can't get any result from get_field. Here's my test page:
http://23.21.199.240/test
I can retrieve the repeater field data with a FOR loop, but the special ACF get_field function isn't doing what I expected. (It seems so simple, so I won't be surprised if I'm making a totally bonehead mistake.)
Any help would be greatly appreciated!
<?php
/*
Template Name: test
*/
?>
<?php get_header(); ?>
<?php query_posts('category_name=worldwise&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
id: <?php the_ID(); ?><br />
<!--let's try to display all the topics (an ACF repeater field) for this post....-->
<?php if(get_field('topic')): // if there are topics, get all their data in an array ?>
<?php
$topic_list_1 = ''; // set up an empty topic list
while(has_sub_field('topic')): // for every topic
$topic_title_1 = get_sub_field('topic_title'); // get the title
$topic_list_1 .= '<li>' . $topic_title_1 . '</li>'; // and add it to the topic list
endwhile; // no more topics, move on
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_1; ?></ul>
<?php else: ?>
<p style="color:red">GET_FIELD did not find any topics</p>
<?php endif; ?>
<!--or, let's try displaying those topics another way....-->
<?php
$vid_id = $post->ID;
$vid_custom = get_post_custom($vid_id);
?>
<?php if($vid_custom['topic'][0] != null): ?>
<?php
$topic_list_2 = '';
for($r=0;$r<$vid_custom['topic'][0];$r++):
$topic_title_2 = $vid_custom[topic_.$r._topic_title][0];
$topic_list_2 .= '<li>' . $topic_title_2 . '</li>';
endfor;
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_2; ?></ul>
<?php else: ?>
<p style="color:red">The FOR loop did not find any topics</p>
<?php endif; ?>
<br />
<?php endwhile; else: ?>
<p>Sorry, no posts or pages matched your criteria.</p>
<?php endif; ?>
I don't like using get_sub_field methods as it gets a bit complicated when you're nesting repeater fields.
This is what's in my opinion the easiest way to get what you're trying to do:
<ul>
<?php foreach (get_field('topic') as $row) :?>
<li><?php print $row['topic_title'] ?></li>
<?php endforeach; ?>
</ul>
In my wordpress theme, I want to add in the footer a side loop to fetch the latest posts.
the first post of this loop displays thumb pix, title, and post preview..
the 5 following only displays the title/link.
Since I already use the regular <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
in the main div, I have to use a side loop based on get_posts()
this is what I want to get, but it's not working:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$count = count($posts);
foreach ($posts as $post) : start_wp(); ?>
<?php if ($count < 2) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php endforeach; ?>
I know how to add count/condition for the regular wordpress loop but not with the get_posts() function.
Can you please help me achieving that ?
Thanks in advance ;)
edit: the solution :
Ok I used the 'offset' argument to achieve that:
<?php $posts = get_posts('numberposts=2&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="footernews-thumb">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(thumbnail); ?>
<?php endif; ?>
</a>
<h2 class="footernews-title"><?php the_title(); ?></h2>
<p class="footernews-preview"><?php the_content_rss('', TRUE, '', 20); ?></p>
<?php endforeach; ?>
<?php $posts = get_posts('numberposts=3&offset=1');
foreach ($posts as $post) : start_wp(); ?>
<h2 class="footernews-title"><?php the_title(); ?></h2>
<?php endforeach; ?>
Since the "loop" is not really a loop, I decided to avoid counting occurrence.
I would use a simple incrementing check as such....
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$i = 1;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php ++$i;
<?php endforeach; ?>
<?php unset($i); ?>
This will then only give you the additional output for the first iteration, after that it will just keep going using the lesser code.
You're setting $count to the number of posts you're getting (5) so it will NEVER be less than 2.
You need to change it to this:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$first = true;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($first) : ?>
/// code for the 1st post (thumb etc..)
<?php $first = false; ?>
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif;?>
<?php endforeach; ?>
I want to show posts, that are in a similar category as the page. Here is my code (just the important php part, not html)? What is wrong?
<?php $pages = get_pages(array('child_of' => 13)); ?>
<?php foreach ($pages as $page): ?>
<?php echo $page->post_title; ?>
<?php echo $page->post_excerpt; ?>
<?php echo $page->post_content; ?>
<?php $categories = get_the_category($page->ID);?>
<?php query_posts('post_type=projekt&category_name=$categories&showposts=1');?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?></a>
<?php endforeach; ?>
Thanx!
try
<?php $pages = get_pages(array('child_of' => 13));
foreach ($pages as $page) {
echo $page->post_title;
echo $page->post_excerpt;
echo $page->post_content;
$categories = get_the_category($page->ID);
query_posts('post_type=projekt&category_name='.$categories.'&showposts=1');
while (have_posts()) : the_post(); ?>
<a><?php the_title(); ?></a>
<?php endwhile;
endforeach; ?>
without testing, this line looks incorrect:
<?php query_posts('post_type=projekt&category_name=$categories&showposts=1');?>
the $categories variable is being interpreted literally because of the single quotes
<?php query_posts('post_type=projekt&category_name=' . $categories . '&showposts=1');?>