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' => '='
)
);
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 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'];
}
Well, this one is getting me quite frustrated. I have multiple custom post types (CPT). Most of them have an archive. Now I have a ctp called Portfolio and a cpt called recent work.
What I would like is to have both ctp's show up in the archives-[slug].php pages. BUT in one archive the link should be /portfolio/page-name and in the other it should be /recent-work/page-name.
Both pages would show the same content only the archive slug would be different. I don't want to have to create double content.
Does anyone know if this is possible using Wordpress and how I can achieve this...
As Gavin Thomas said, use categories (well in my case custom taxonomies). This is my loop code:
<?php $args = array(
'post_type' => 'portfolio',
'posts_per_page'=> -1,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'event-type',
'field' => 'slug',
'terms' => 'corporate-events'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<?php $naam = get_field('naambedrijf');
$soort = get_field('soort_uitje');
$foto = get_field('flickr_fotoset'); ?>
<div class="col s6 m4 l4">
<a href="<?php the_permalink(); ?>">
<div class="title">
<h2 class="truncate" style="line-height:20px;"><?php echo $naam; ?></h2>
<small class="truncate" style="color:#222;"><?php echo $soort; ?></small>
</div>
<div class="thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ;?>
</div>
</a>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
The items under the ctp are displayed using this code, but the permalink is still /portfolio/page-name and not /category-slug/page-name. How do I go about this?
I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9.
So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents.
My approach:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><?php the_title(); ?></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
Output I am getting
<div id="post-137 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
Can someone please help me with where I am going wrong?
Instead of using post_class(), the_permalink(), the_title(), and the_excerpt(), you should use the $post object to get the data, just like you did with $post->ID. The functions you've used should be called only if you're using a loop based on have_posts(). You aren't, so replace them with:
post_class() -> get_post_class( '', $post->ID )
the_permalink() -> get_the_permalink( $post->ID )
the_title() -> $post->title
the_excerpt() -> $post->post_excerpt
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.