I have a bit of a strange problem with my WP-query. I have a custom post type (portfolio), with a custom taxonomy called year. I have categories for each year, so what I want to do is display all posts for each year. The problem is, only 2012 works. Doesn't matter if I order the categories ASC/DESC - only 2012 works.
<section id="content">
<?php
$categories = get_categories('taxonomy=year&order=DESC');
foreach($categories as $category) : ?>
<article class="year">
<h2><?php echo $category->name ?></h2>
<div class="items">
<?php
$posts = get_posts('taxonomy=year&post_type=portfolio&year=' . $category->slug);
foreach($posts as $post) : ?>
<div class="item">
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->slug . ']" >';
the_post_thumbnail('thumbnail');
echo '</a>';
?>
</div>
<?php endforeach; ?>
</div>
</article>
<?php
endforeach;
wp_reset_query();
?>
</section>
What am I doing wrong? To me, it seems right.. I've tried a bunch of different takes on this, everything from real querys to ridiculous sortings but I just can't get it right..
Thank you in advance!
I've solved it myself now, still not getting it 100% but it works at least.. There has got to be some smarter way of doing this, since im now looping trough all images for every term. Well, here's the code (get posts grouped by term from custom taxonomy).
<section id="content">
<?php
$categories = get_categories('taxonomy=year&order=DESC');
foreach($categories as $category) { ?>
<article class="year">
<h2><?php echo $category->name ?></h2>
<div class="items">
<?php
$args = array(
'post_type' => 'portfolio'
);
query_posts($args);
$count = 0;
while(have_posts()) : the_post();
$terms = get_the_terms( $post->ID, 'year' );
foreach ( $terms as $term ) {
$imgslug = $term->name;
}
if($imgslug == $category->name) {
if($count == 6) {
echo '<div class="expanded-items">';
}
?>
<div class="item">
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->slug . ']" >';
the_post_thumbnail('thumbnail');
echo '</a>';
?>
</div>
<?php
}
$count++;
endwhile;
if($count >= 6) {
echo '</div>';
}
?>
</div>
<div class="expand">Visa fler</div>
</article>
<?php } ?>
</section>
That is with an expandable list, so it shows 6 from the start and then expands to show the rest of the items (jQuery).
Related
I am trying to get the image from a category but I cannot retrieve the image.
Today I already learned that I had to use
get_field('product', $term->taxonomy . '_' . $term->term_id);
to fetch content.
But when I use this same method to fetch an image URL from an ACF Field linked to my custom post type category, I do not recieve any values.
This is my code (the var_dump is included):
<?php
$args = array(
'post_type' => 'segments-overview',
'orderby' => 'date', // we will sort posts by date
);
$query = new WP_Query( $args );
$all_terms = [];
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$terms = get_the_terms(get_the_ID(), 'category-segments-overview');
foreach($terms as $term) $all_terms[$term->term_id] = $term;
endwhile;
foreach($terms as $term):
?>
<div class="segments-card">
<div class="img">
<?php
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="content">
<div class="title"><?php echo $term->name; ?></div>
<!-- <?php print_r($term); ?> -->
<a class="button transparent" href="/segments/<?php echo $term->slug; ?>">
<?php echo __('View All','axia'); ?>
</a>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
else :
?>
<div class="no-posts-found">
<h2>There were no items found</h2>
<h3>Please try a different search</h3>
</div>
<?php
endif;
?>
I use this var_dump to see if everything is fetched:
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
echo '<pre>';
echo "Image field value:";
var_dump($image);
echo "Category field value:";
var_dump($term);
echo '</pre>';
The only thing is that I do not get the value from my image in my category that is made in ACF.
You can simply get value by passing term object as second parameters.
$image = get_field('image', $term);
Check the docs here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I have a custom post type named " actualites ".
I want to wp_query the post data ( title and content for now ).
Here's the code I used :
<div class="row">
<div class="col-sm-8 blog-main">
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
</div>
<!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul>
<?php endif; ?>
</div><!-- /.blog-sidebar -->
For some reason this makes my website loop endlessly. Nothing appears, not even the header or the footer. Why does this happen ? It's 100% the wp_query part.
I tried the standard query and had an "expected statement " that probably was the cause of my issue. I'm trying the alternate wp query code now, and I don't have any clue as to what doesn't work. Any help ? Thanks !
Your Code should be as follows. In your code $the_query variable is not defined. I think that's why it's not working. Also turn on WP DEBUG MODE when you are developing. So you will be able to see errors and warnings.
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
Current theme is set up to generate a thumbnail based on user input (variable height). I would like to have this thumbnail link to a full size featured image via prettyphoto. Current code calling generated thumb:
<?php
//if our user has a post thumbnail
//out featured image URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
?>
<?php if($src[0] != '') : //if user has featured image ?>
<img src="<?php echo ddTimthumb($src[0], $contentW, get_post_meta($post->ID, 'postThumbHeight', true)); ?>" alt="<?php the_title(); ?>" />
You can use this just modify the classes and other attributes according to your need.
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a rel="prettyPhoto" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail();
echo '</a>';
}
?>
I am actually using this same thing in one of my project see the code link and see if that can help you :) and once again don't expect it to work by copy pasting this is just to give you an idea modify it to your needs :)
Link to pastebin for code sample (or see below)
<div class="row" id="gallery-main">
<?php $args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
);
query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="span4 portfolio-item">
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a class="image-link pi-img" rel="prettyPhoto" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('portfolio-listing');
echo '<div class="hover-style"></div></a>';
}
?>
</div>
<?php endwhile;endif; ?>
<?php wp_reset_query(); ?>
</div>
I recently tried the previous post and the next post button link. I do that with image in the left and right side of the site.Its working perfect.But i didn't know how to do this
For Example:
<div class="alignleftfp">
<?php next_post('%', '<img class="imgalign" src="' . WP_CONTENT_URL . '/uploads/1.png" alt="Next" /> ', 'no');
?>
</div>
<div class="alignrightfp">
<?php previous_post('%', '<img class="imgalign" src="' . WP_CONTENT_URL . '/uploads/1.png" alt="Next" /> ', 'no');
?>
</div>
Is it Possible to Show the Previous Post and Next Post Link with Under Title in every Bottom of the Post. Here is the Screenshot.
<nav id="nav-single">
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
?>
<?php
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
?>
<span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?>
<h2><?php echo $prev_post->post_title; ?></h2>
</span>
<span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
<h2><?php echo $next_post->post_title; ?></h2>
</span>
</nav>
I hope you are using this code in single.php from where the whole of the post is displayed. For displaying the links (Next/Prev), you need to check the function.
get_template_part()
in the same file (Single.php of your theme). In my case function in single.php has been passes parameters like
<?php get_template_part( 'content-single', get_post_format() ); ?>
So, you will open the "content-single.php" according to the parameters specified in the function and paste the same code
<div class="alignleftfp">
<?php next_post('%', '<img class="imgalign" src="' . get_bloginfo('template_directory') . '/images/1.png" alt="Next" /> ', 'no');
?>
</div>
<?php previous_post('%', '<img class="imgalign" src="' . get_bloginfo('template_directory') . '/images/2.png" alt="Next" /> ', 'no');
?>
</div>
below <h1 class="entry-title"><?php the_title(); ?></h1>
I hope this will solve your problem.
I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>