i am creating a related post article feature for my wordpress enabled site.being new to php and wordpress facing some issue.
though i can user wordpress build in feature to show related post of author but since site is hosting articles of authors who don't have any profile with us and there are many such authors so when a new post is being created for the author we tend to save a custom field as author_email.
so based on this we want to show all post being published of a particual author.i tried using get_posts() method
<?php
$args = array( 'meta_key' => 'author_email', 'meta_value' => 'XYZ#XYZ.com');
$authorposts=get_posts($args); ?>
<div id="content">
<span class="breadcrumbs">Home » <?php the_category(', ') ?></span>
<?php if (count( $authorposts ) > 0) {
foreach ( $authorposts as $post ): setup_postdata($post) ?>
<div id="headline_author">
/*
showing post tilte with image and some part of it
*/
<div class="clearfloat"></div>
</div>
<?php endforeach; ?>
<div class="post-nav">
<div class="previous"><?php previous_posts_link('‹ Previous Page') ?></div>
<div class="next"><?php next_posts_link('Next Page ›') ?></div>
</div>
<?php
} else {
echo '<p>No articles by this user</p>';
}
?>
</div>
it showing the first 5 results and a link to the next page but when i click on the next page it show me that it has gone to second page as its also visible on the URL but its showing the same 5 results what it has shown in the first page.
i am an idea that the query i have written on the same page is getting executed again from the beginning but not sure how to fix it.
since i am using cutom fields to get posts i can use simple query to fetch post based on the limit or offse etc.
can any one help me to fix the things or can point me hw to do it in right way
thanks in advance
I am not word-press/php expert just started learning about it. it seems that you have not set the paged global variable by which it can track the page number.
you need to do something like this.
$post_per_page = get_option('posts_per_page');
if (!is_paged()) {
$custom_offset = 0;
} else {
$custom_offset = $post_per_page*($paged-1);
}
$args = array( 'meta_key' => 'author_email', 'meta_value' => xyz#xyz.com' ,'numberposts' => $post_per_page,'offset' => $custom_offset);
Related
My version of wordpress is 5.2.3
I have a custom post type and I want to add it a sticky post.
I tried many plugins and hack to figured out how to solved this problem (sticky custom post type isn't a native feature of wordpress) but I still don't know how to solved it.
Does anyone have a solution for me ?
-----EDIT------
I try this plugin https://wordpress.org/plugins/custom-post-type-sticky/. He put the same feature than the classic post of wordpress (a checkbox for put a sticky post) he works perfectly in back but nothing appear in front. (event with an additional code found here https://www.sktthemes.org/wordpress/add-sticky-posts-wordpress/) My code is a basic loop like :
<?php
$args_ressource = array(
'post_type' => 'ressource',
'order' => 'DESC',
'posts_per_page' => 4,
);
$ressource = new WP_Query( $args_ressource );
if ( $ressource->have_posts() ) { ?>
<ul>
<?php
while ( $ressource->have_posts() ) {
$ressource->the_post();
$imageArticle = get_field('hero_image');
?>
<li>
<a href="<?php the_permalink(''); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
wp_reset_postdata();
?>
To be precise I need to show my sticky custom post type on the left of my page, and on the right the rest of the custom post but without this sticky post.
With the basic article a simple loop works because this is a basic feature of wordpress, but not with custom post type
Many thanks
admin:
Create meta_value mimicking sticky function on/off
logic:
query "posts" filtering only checked posts
create second query where you exclude above "post" ids
template:
run a loop for sticky
run the common loop
I am trying to add custom divs into my index page and I created some page templates, that i was thinking of using as a way to filter out the pages. So basically if the post uses 'Media template' it would be displayed in that div.
But I am struggling in writing the proper php code that would loop trough all the posts and find the post that uses this template.
In the end I want to find a post/page that uses 'Media template' and post it's content in the folowwing div:
<div class="home-media-content col-sm-3 px-1 pb-2">
<div class="embed-responsive embed-responsive-16by9">
<?php
$pages = get_pages();
foreach ($pages as $page) {
echo $page->post_title; //Posting just for the test
echo $page->template; //SHOULD GET THE TEMPLATE and if the template name is 'Media template' display post content
}
?>
</div>
</div>
I am not sure if it is the best way of displaying custom things so any better suggestions are welcome!
Edited to use WP_Query
See below for how I'd do it, using the setup_postdata() and get_page_template() functions. I think you should be able to get this info from the '_wp_page_template' meta for each page if you don't want to setup postdata, but if you're going to be doing a proper loop anyway then the latter is probably easiest.
<div class="home-media-content col-sm-3 px-1 pb-2">
<div class="embed-responsive embed-responsive-16by9">
<?php
$my_query = new WP_Query(
array(
'post_status' => 'publish',
'post_type' => 'page',
)
);
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
the_title(); // Posting just for the test
if ( get_page_template() === 'media-template.php' ) { // This needs to be the file slug rather than template name
do_something();
} else {
do_something_else();
}
}
}
wp_reset_postdata(); // Reset postdata back to normal
?>
</div>
</div>
Dear PHP / Wordpress / Dev Experts,
Ive build a plugin, with a custom post type and some advanced custom fields. The main goal is to list the members in my band, with pictures and name.
You can see it here: http://www.lucky13.nl/test/
I've managed to get everything working to my taste, however.. I have 5 bandmembers in my band which i've added, but i'm only seeing 4. Where is the 5th entry / post? I find that the first added bandmember is not displaying.
I assume this has to do with the loop, and the array not listing all items? But i'll leave this to the experts.. I would appreciate any comments of help!
My code:
<?php
/*
Plugin Name: VrolijkWebdesign - Bandmembers
Description: For a bandwebsite to show bandmembers.
*/
/* Start Adding Functions Below this Line */
/* NAME FUNCTION */
function new_section_1(){
$bandmembers = new WP_Query(array(
'post_type' => 'bandmembers'
));
while($bandmembers->have_posts()) : $bandmembers->the_post();
if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!-- START HTML -->
<div class="span2">
<figure class="snip1104">
<img src="<?php echo $image[0] ;?>" alt='sample33'/>
<figcaption>
<h5> <?php the_field('firstname'); ?> <span> <?php the_field('lastname'); ?>
</span></h5>
</figcaption>
</figure>
</div>
<!-- END HTML -->
<?php endif;
endwhile;
}
add_shortcode('band', 'new_section_1');
?>
$bandmembers = new WP_Query(array(
'post_type' => 'bandmembers',
'posts_per_page' => '5'
));
Try setting the posts_per_page argument. Since the default might be set to '4' by other filters.
If you want to get all posts in a single query use '-1' instead of '5'
You could also try the below for debugging purposes only:
-Try to set post_status to 'any' to make sure that post statuses are not a problem.
-Try var_dump($bandmembers) after doing the query to see the fetched posts before the loop starts.
I'm a bit stumped on this one. I'd like to create a page that is an archive of all posts of a certain category - and show excerpts. However, Reading through the Wordpress docs, they say a page cannot be a post or associated with categories.
So, I'm now wondering if this is possible or if there is a work around. I'd really like to put this on a page as I'd like to have a custom sidebar as well. Is this possible with a custom page template? Any other ways this can be done that I am not thinking of?
Thanks!
You can add this function to functions.php and then you can call it from any page template.
If you need to create a new page template for the sidebar.
FTP, download page.php
rename page.php to page-custom.php (custom can be anything, just make sure its page-whatever.php
In the page-custom.php
Replace the comments section with (Custom Template Name can be anything you want)
/**
* Template Name: Custom Template Name
*
*/
replace the loop in your new template with a call to get_posts_custom('catSlug');
then add this to your functions.php
if(!function_exists('get_posts_custom')){
function get_posts_custom($catSlug){
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $catSlug,
'ignore_sticky_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(); ?>
<article class="post">
<time class="date"><?php the_time('m.d.y') ?></time>
<p><strong><?php the_title(); ?></strong>
<?php the_excerpt(); ?>
</p>
</article>
<?php
endwhile;
}
wp_reset_query();
}
}
Reference http://codex.wordpress.org/Class_Reference/WP_Query
I didn't fully test the function, but I am using a modified version of the same code. Mine doesn't filter on the category slug, so that may need tweaking, but I did test to make sure it didnt break functions.php
Create a page, lipsum for example.
Create a PHP file with the page-[the title].php, page-lipsum.php for this example.
Write your loop in that file.
FTP that file to the directory of your theme.
When you go to the URL for that page, the results should be what you're looking for.
I am using WordPress 3, and I created a custom post type called article, which gives me the URL format of mywebsite/articles/article-title. How do I see all the article entries in the URL mywebsite/articles?
Assuming you set up everything correctly and you want to see the post type on a public template page, try this code into mycustomtemplate.php or the equivalent.
<?php $loop = new WP_Query( array( 'post_type' => 'article', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
You can customize the loop just like you would blog posts and pages. If you want to get all on one page you'll want to remove the limit of 10 on post_per_page I wouldn't suggest it though. I would say set it to 50 or 100 and still use pages.
Source: Custom post types in WordPress
I have the simplest solution. Just create file archive-{custom post type}.php and then, just do loop content as usual.
If you already set the permalink, just type yourdomain.com/{custom post type}.
You can easily achieve this from the definition of your custom post type, starting with WP 3.1. Just set has_archive to true.
Source: http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types