Restricting posts to a specifc category and subcategory - php

On this site: http://www.palmbeachwoman.com/stories/ I used a programmer to customize the template. His code for this page is:
<?php
$args = array( 'numberposts' => 18, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
echo '<ul id="latest_stories">';
foreach ($postslist as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
<span class="s_thumb"> <?php the_post_thumbnail( 'full' ); ?> </span>
<span class="s_title"> <?php the_title(); ?> </span></a>
<span class="s_cotegories">More Stories from <?php the_category( ', ' ); ?></span></li>
<?php endforeach; ?>
The posts were supposed to be restricted to a specific category (#82) and any subcategories within it. I know how to do this with the traditional post display: http://alijafarian.com/how-to-display-wordpress-posts-for-a-specific-category/ but since I didn't write this code, I'm not sure how to modify it.

You can set the category in the array past to get_posts
$args = array( 'numberposts' => 18, 'post_status'=>"publish",'post_type'=>"post",
'orderby'=>"post_date", category=>82);

Related

Php with WordPress how to call up permalinks to two separate posts

i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;

Woocommerce how to show custom attributes other than default in home page?

After a long and recursive search on the web, I didn't find a solution for my needs.
I need to display, in home page, not only product name and price, but also some custom attribute I already have set in all my products. Let's say 'year' and 'author'.
All snippets found are single product or shop page related only, nothing for home page (or any other page anyway).
Tried also with woocommerce shortcodes, but they are not applicable for that purpose.
I already use:
[recent_products per_page="6" columns="6" orderby="year" order="desc"]
to display recent products ordered by "year" att (I found a snippet to add 'orderby' custom att).
Any help will be really appreciated.
Here's the code that you can use for displaying products from a specific category with meta data on the home page:
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'categoryNAme', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3>Title: <?php the_title(); ?></h3>
<h3>Content: <?php the_content(); ?></h3>
<h3>Date: <?php the_date(); ?></h3>
<h3>Author: <?php the_author(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

Get term of post belong

I had this query:
$args=array(
'post_type' => array('product'),
'order' => 'DESC',
'orderby' => 'rand',
'showposts' => 24
);
query_posts( $args );
if(have_posts()) : while(have_posts()) : the_post();
?>
<div class="col-xs-12 col-s-6 col-sm-4 col-md-3 our-products">
<a class="products-img" href="<?php the_permalink();?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail('product-size',array('class' => 'img-responsive'));}?></a>
<a class="products-tit" href="<?php the_permalink();?>">
<h2><b><?php the_title();?></b></h2>
</a>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
I need to get tax term of product belong to it.
Example:
I had Category: Products -> Men fashion/Women fashion -> T-shirt -> product A.
How can i get the term of T-shirt which product A belong to it?
You can use WordPress get_the_terms() function for this purpose.
Here is the example:
$_terms = get_the_terms( $post->ID , 'Your Taxonomy' );
In "Your Taxonomy" you put the name of the taxonomy that you just registered e.g. product-category
So just like this:
$_terms = get_the_terms( $post->ID , 'product-category' );
And then
<?php
foreach($_terms as $_term) {
?>
<a href="<?php echo get_term_link($_term->slug, 'product-category') ?>">
<?php echo $_term->name ?>
</a>
<?php
}
?>
This function get_term_link() is use for term link.
Hope this will help you.

Breadcrumb trail shows incorrect trail?

Why the following code would cause my breadcrumb trail to show the same post link in the breadcrumbs no matter what page you are on in the site?
I have been stuck on this issue for ages now and just cannot figure it out...
See what I mean here.
<!-- Loops through Stories and returns images -->
<ul style="width: 1520px">
<?php
global $post;
$category_id = get_cat_ID('stories');
$args = array( 'numberposts' => 9, 'offset'=> 0, 'category'=> $category_id );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<!-- stories -->
<li>
<figure>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(array(64,64)); // Declare pixel size you need inside the array ?></a>
<?php endif; ?>
</figure>
<!-- /post thumbnail -->
<h3><?php the_title(); ?></h3>
</li>
<?php endforeach; ?>
</ul>
<!-- /Loops through Stories and returns images -->
Try to reset the postdata, like in this example from the Codex:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>

Wordpress doesn't display the permalinks of two of my post

Here is my webpage: http://dastousgroupeconseil.com/faq-2/
Here is the complete code of my page: http://pastebin.com/PQUsYdha
I used this php code to display the excerpt and the hyperlink of my posts, but some of them don't want to display. Is it because I reset the $post variable after each endforeach?
<?php global $post;
$args = array( 'numberposts' => 4, 'offset'=> 0, 'category' => 140 );
$myposts = get_posts( $args );
foreach( $myposts as $post) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" target='_blank' ><?php the_excerpt(); ?></a>
<?php endforeach; wp_reset_postdata(); ?>
I've had issues using setup_postdata a few times myself, I tend to skip it altogether and just use the retrieved $post objects.
So for the permalink:
<?php echo get_permalink($post->ID); ?>

Categories