I have a this code in my WP theme and it works correctly. But when I want to show the last post from another website (for example: www.mag.tabgir.com) it doesn't show the post from my blog.
How can i change this code to show last post from this site www.mag.tabgir.com?
<section class="block-blog box">
<header>
<h2>recent post</h2>
</header>
<section class="content">
<?php
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();?>
<article class="clearfix">
<?php the_post_thumbnail('blog') ?>
<h1 class="title"><?php the_title(); ?></h1>
<span><?php the_time('d/M/Y') ?></span>
</article>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
see more
</section>
</section>
Below is the code it is working correct and tested locally.
$args = array(
'numberposts' => 1,// increase the number if you wish to display 2 latest
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach ($recent_posts as $result)
{
echo 'Title : '.$result['post_title'].'<br/>';
echo 'content : '.$result['post_content'];
}
Related
I'm learning how to use wordpress API. I'm a newbie with this framework, so I've decided to install Understrap to use the Bootstrap 4 framework and create a simple portfolio website. After googling a bit, I've started experimenting with the code, but there are many aspects of this wordpress theme that are unclear to me. I want to display some posts on a page and style how they will appear using the bootstrap classes markup. Is there any valid tutorial about or anyone can suggest to me the correct modifications I need to make to the template theme files?
I've tried to create a page named postpage.php with this code inside, but it will not be recognized from wordpress as a template model for a page.
CODE:
<?php
$args = array(
'posts_per_page' => 6,
'offset' => 0,
'category' => 'portfolio',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '', 'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post', 'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
First you need to specify that this is a page template by adding the following code to the top of your file:
<?php /* Template Name: Example Template */ ?>
Then it will show up in your page template dropdown. More info about page templates here.
In order to add Boostrap classes, you need to wrap the foreach statement in the Bootstrap containers and then change the ul to bootstrap columns:
<div class="container">
<div class="row">
<?php foreach ( $myposts as $post ) : setup_postdata($post ); ?>
<div class="col-sm-4">
<?php the_title(); ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</div>
If you want to use custom layout then you need to make a custom template and there you will add a page for using your custom templet. your custom template code will like this
<?php
/* Template Name: Your custom templete */
get_header();
?><?php $the_query = new WP_Query(array(
'category_name' => 'popular',
'posts_per_page' => '6',
'order' => 'DESC', // Show only the published posts
));?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();?>
<div class="story-info">
<a class="category-name arts texunset" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<span class="daycolor" style="background:<?php the_field('colorpost'); ?>;"> </span>
<span>
<?php the_title(); ?>
</span>
</a>
<div class="date">
<?php the_time('F jS, Y') ?> |
<i class="fa fa-signal"></i>
</div>
</div>
<hr>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer();?>
I'm writing a WordPress theme for my website and I am re-writing most of the appearance for it. I am looking to create a homepage like thenextweb.com that shows 3 sticky posts on the top of the homepage. My website (www.iamlittle.co.uk/beta) shows all the posts in all of the boxes which I don't want it to do.
You can use get_posts() for this:
<?php
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$my_latest_posts = get_posts( $args );
?>
<?php foreach ( $my_latest_posts as $post ) : setup_postdata( $post ); ?>
<section class="cover-feature cover-feature-smallone featured-post">
<a href="<?php the_permalink(); ?>">
<div class="cover-feature-text-item">
<h2><?php the_title();?></h2>
<p>By: <?php the_author_posts_link(); ?>. Posted on <?php the_time('F jS, Y') ?>.</p>
</div>
</a>
</section>
<?php endforeach;
wp_reset_postdata();?>
I have managed to get my loop to show only the posts that display true on an advanced custom field.
But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.
'posts_per_page' => '1'
Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You should use Meta Query here. Change your args array to:
// args
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1'
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.
Syntax:
$variable = get_field('field_name', $post->ID);
Where the $post->ID will be appering from the Loop that you use.
if (get_field('sponsored_event') == 'yes') {
// code to run if the above is true
}
else
{
// code for else part
}
Make sure that the value saved into the DB for the radio button is like you give in the if statement
This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query
Change the Wp_Query like this if you need only one post that to the latest one that has been stored.
$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Else if you want all the posts that has been stored you can use like this.
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Note:
post_per_page=10 -> Will bring the 10 posts from your post type
post_per_page=-1 -> will bring infinite posts that your post type has
Entire Loop:
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.
So this is my first post and wow, I didn't know this site existed. I've had a look around at questions and I hope that mine isnt a dumb nooby one. Although I am a noob :S
Ok, so I created a function in WordPress that will add a meta box to the new posts page so that I can specify whether or not this post should be featured (I read this is better than creating a featured category for SEO purposes?).
Anyway.. The code I have works in showing the most recent. Here is the code for that:
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_meta($my_query->post->ID, '_featuredpost_meta_value_key', true);
if ( $custom ){
?>
<article class="container" itemprop="blogPosts" itemscope itemtype="http://schema.org/BlogPosting">
<div class="row">
<h2 itemprop="about">
<?php the_title(); ?>
</h2>
</div>
<div class="row">
<div class="<?php if ( has_post_thumbnail() ) { ?>two-thirds column<?php } else {?> twelve columns <?php } ?>">
<p class="post-excerpt"><?php modified_excerpt(); ?></p>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="one-third column">
<?php the_post_thumbnail('full', array('class'=>'hide-mobile')); ?>
</div>
<?php } ?>
</div>
<div class="row">
Continue Reading
</div>
<hr />
<div class="post-info">
<ul>
<li class="date"><?php the_date();?></li>
<li class="author"><?php echo get_the_author_meta('display_name'); ?></li>
<li class="category"><?php the_category(', '); ?></li>
<li class="tags"><?php the_tags('',', ',''); ?></li>
</ul>
</div>
</article>
<?php
}
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Now, When I use the same code below, but then use:
if ( ! $custom ){
to show the posts that are not set to be featured that also works. The problem is that the pagination no longer works. When I go to the second page it just duplicates what is on the home page.
This leads me to believe that I have created a mashed together crappy bit of code. Can someone please help me build a loop, that will exclude any posts where the meta data _featuredpost_meta_value_key is set to Yes.
Thanks in advance
You'll want to use a WP Meta Query in your original $args array.
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
From the docs, here's an example:
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
But you can also use the sugar provided by the WP_Query class and pass it in as the meta_query value to your original args:
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'meta_query' => array(
'key' => '_featuredpost_meta_value_key',
'value' => 'Yes',
'compare' => '='
)
);
I'm developing my first Wordpress theme and the first loop I have is only outputting 1 item: a link to the homepage (not any of the arguments I am trying to pass in the array).
Here's the php and html:
<div class="services_list">
<?php
$args = array(
'posts_per_page'=> 999,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'service',
'meta_key' => 'featured',
'meta_value' => '1'
);
// The Query
get_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="service_item">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="service_top_link">
<div class="service_image"><?php the_post_thumbnail( array(120,120) ); ?></div>
</a>
<h3 class="service_title"><?php the_title(); ?></h3>
<div class="service_excerpt"><?php the_excerpt(); ?></div>
Learn More
</div><!-- .service_item -->
<?php endwhile;
// Reset Query
wp_reset_query();
?>
</div><!-- .services_list -->
I apologize if this question has already been answered, but I can't seem to find anything on it.
worked! thanks. I switched from get_posts to using WP_query and was having the same problem. Turns out the issue was actually with the meta value, not the query itself.