I've created a custom theme and I'm using a template for a blog page. In my template I have a code that calls specific posts according to category. So far so good, problem is when post contains "read more". Link is active but when I click on it I only get an empty page instead of an whole article.
This is my code:
<?php
$args = array(
'category_name' => 'blog',
'post_type' => 'post',
'posts_per_page' => 10,
);
query_posts($args);
while (have_posts()) : the_post();?>
<?php global $more; $more = 0; ?>
<?php echo '<p class="date"><i class="fa fa-calendar"></i> ';
echo get_the_date();
echo '</p>';
echo '<h2>';
echo '<a class="permaLink" href="';
echo the_permalink();
echo'" title="';
echo the_title_attribute();
echo'">';
echo the_title();
echo '</a>';
echo '</h2>';
echo '<p class="posts">';
echo get_the_content('<br/><br/>Read more...');
echo '</p>';
echo '<hr/>'; ?>
<?php endwhile; ?>
Any help would be appreciated. Thanks
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 created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>
I want to show a few products per product category on front page. I am able to show the products but can't get the product category name displayed using the slug.
Okay, This worked :
<?php
$product_cat_name = get_term_by( 'slug', 'slugname', 'product_cat' );
echo $product_cat_name->name;
?>
You can find product category using this code...
I have also added code to get woocommerce category image
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC'));
foreach($catTerms as $catTerm) : ?>
<?php $thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id ); ?>
<a href="<?php echo site_url();?>/product-category/<?php echo $catTerm->slug; ?>">
<img src="<?php echo $image; ?>"></a>
<a href="<?php echo site_url();?>/product-category/<?php echo $catTerm->slug; ?>"> <h2><img src="<?php echo $a['url']; ?>">
<?php echo $catTerm->name; ?></h2></a>
<?php endforeach; ?>
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'm building a simple sidebar widget to display a loop of a custom post type "shows".
Each "show" has about 3 custom fields, which i'd like to output via the loop as well. This is the code i'm using:
This is the loop within my plugin code:
<?php
// WIDGET CODE GOES HERE
$args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$month = get_post_meta($post->ID,'month-abbreviation',true);
$date = get_post_meta($post->ID,'date',true);
$citystate = get_post_meta($post->ID,'city-state',true); ?>
<article class="sidebar-show clearfix">
<a class="show-link" href="<?php the_permalink(); ?>">
<div class="date-box">
<span class="month"><?php echo $month; ?></span>
<span class="date"><?php echo $date; ?></span>
</div>
<div class="venue-box">
<?php echo "<h4>".get_the_title()."</h4>"; ?>
<?php echo "<p>".$citystate."</p>"; ?>
</div>
</a>
</article>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();
?>
<?php
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("ShowsSidebarWidget");') );?>
This code pulls in the post titles, but doesn't display the custom fields month-abbreviation, date, and city-state.
What's missing here?
EDIT: Removed double quotes after avexdesign's reply.
are you sure $post->ID is available in this context?
maybe you should try: get_the_ID() instead.
Ok, I see a few things you can try.
month-abbreviation - should be single quotes. Like 'month-abbreviation'
Try taking out: $month= $date= and $citystate =
Might need echo
So Try:
echo get_post_meta($post->ID,'month-abbreviation', true);
echo get_post_meta($post->ID,'date', true);
echo get_post_meta($post->ID,'city-state', true);
Try something like:
<?php $loop = new WP_Query( array( 'post_type' => 'shows', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<span class="month"><?php echo get_post_meta($post->ID, 'month-abbreviation', true); ?></span>
<span class="date"><?php echo get_post_meta($post->ID, 'date', true); ?></span>
and so on... for your other custom IDs