Hi I'm trying to figure out how to put a featured image in wordpress on my custom page.
here is my code:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if (has_post_thumbnail() ) {
foreach( $recent_posts as $recent ){
the_post_thumbnail();
echo '<li>' . $recent["post_title"].' </li> ';
}
}
else {
echo "no featured post";
}
?>
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if( $recent_posts ) {
foreach( $recent_posts as $recent ){
if (has_post_thumbnail($recent['ID']) ) {
echo get_the_post_thumbnail($recent['ID']);
}
echo '<li>' . $recent["post_title"].' </li> ';
}
} else {
echo "no featured post";
}
?>
the_post_thumbnail requires you to be in a loop. If you want to retrieve with your own ID, use get_the_post_thumbnail
Related
How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>
<?php
$orderby = 'id';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty'=> 0,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul class="display-categories list-group nav navbar-nav">';
foreach ($product_categories as $key => $category) {
echo '
<li class="list-group-item">';
echo '<a href="'.get_term_link($category).'" >';
echo '<img src="'.get_term_link($category).'" />' ;
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
I can't fetch the product category icons.. i want it to fetch category icons with the list of names ... please somebody help me out with this thanks
Category Icons / Images has been stored in terms meta and can be retrieved by using
$term_meta = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
$category_icon = wp_get_attachment_url( $term_meta );
I want on my woocomerce wordpress site to click on the product category and to show me the subcategories first and then the products...
something like that.
<?php if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
Add this to your theme's 'functions.php'.
function show_product_subcategories( $args = array() ) {
if(!is_shop()) //not shop page
{
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid,
'hide_empty' => false,
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<div class="sub_categories">
<ul class="product-cats cat_products_list">';
foreach ( $terms as $category ) {
wc_get_template('content-product_cat.php',array('category'=>$category));
}
echo '</ul><div class="clearfix"></div></div>';
}
}
}
add_action( 'woocommerce_before_shop_loop', 'show_product_subcategories', 50 );
I am trying to show all custom posts in Theme options, I am using Theme Options Framework plugin. Here is the code where you can grab all pages but I don't know how to fetch all posts in checkbox so user can select which custom posts show on homepage.
// Pull all the pages into an array
$options_pages = array();
$options_pages_obj = get_pages( 'sort_column=post_parent,menu_order' );
$options_pages[''] = 'Select a page:';
foreach ($options_pages_obj as $page) {
$options_pages[$page->ID] = $page->post_title;
}
for select it is:
<?php
$args = array( 'posts_per_page' => -1 );
$posts = get_posts( $args );
if($posts) {
echo "<select>";
foreach ( $posts as $post ) {
echo "<option value="$post->ID">";
echo $post->post_title;
echo "</option>";
}
echo "</select>";
wp_reset_postdata();
}
?>
For checkbox it is:
<?php
$args = array( 'posts_per_page' => -1 );
$posts = get_posts( $args );
if($posts) {
foreach ( $posts as $post ) {
echo "<div>";
echo "<label>".$post->post_title."</label>";
echo "<input name='posts[]' value='".$post->ID."' type='checkbox' />";
echo "</div>";
}
wp_reset_postdata();
}
?>
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<h1>' .$recent["post_title"].' </h1> ';
}
?>
I added this code for getting just single latest post on desired page.
How can I add next and previous button of posts to it?
If you want to get previous post link, You can do it using following code:
<?php $prev_post = get_adjacent_post( false, '', true ); ?>
<?php if ( is_a( $prev_post, 'WP_Post' ) ) { ?>
<?php echo get_the_title( $prev_post->ID ); ?>
<?php } ?>
If you want to get next post link, Than:
<?php $next_post = get_adjacent_post( false, '', false ); ?>
<?php if ( is_a( $next_post, 'WP_Post' ) ) { ?>
<?php echo get_the_title( $next_post->ID ); ?>
<?php } ?>
You can check more information about this function here : get_adjacent_post()
$previous=$recent["ID"]-1;
echo '' . PREVIOUS.' ';
$next=$recent["ID"]+1;
echo '' . NEXT.' ';
I did this and it worked :)