I am trying to create a loop that will work as a custom navigation menu in the header using custom post type manager. I am creating a function in the functions.php and then calling the function in the header.php. I can't use the regular " while : have posts etc.." because it actually changes the page content. I just want to create a function that will bring up the image, custom fields, etc..
Here is my code that doesn't work:
<?php
// Our Team Navigation Menu
function our_team_arg( $arg2 ) {
$arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');
query_posts($arg2);
$myposts = get_posts( $arg2 );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="founderblk">
<a href="<?php the_permalink(); ?>">
<img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
</a><br />
<span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
</div>
<?php
endforeach;
wp_reset_postdata();
}
// END Our Team Navigation Menu
?>
you can use wp_query and use while normally it will not change the page content when you are using wp_reset_postdata();
and query_posts actually call wp_query internally
here is an example
$args = array('posts_per_page' => 60, 'post_type' => 'site-product');
// The Query
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) { $custom_query->the_post(); ?>
<div class="founderblk">
<a href="<?php the_permalink(); ?>">
<img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
</a><br />
<span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
for the second line in your code
$arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');
I assume that you write it for testing because it is actually overwrite the function arguments
Related
I am trying to loop through a post type called blog. The pagination works fine when the Wordpress permalinks are set to plain however when I change it to post the name and click to go on pagination link, it loads a 404 error.
I found out that you can't have the same post type and page name since it will cause a 404 error. I wanted to know if there was a workaround because changing the name of the post type will affect the blog posts.
My page-blog.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'blog',
'posts_per_page' => 2,
'paged' => $paged,
'has_archive' => false,
'rewrite' => array(
'slug' => '/blog', // if you need slug
'with_front' => false,
),)
);
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
// Set variables
$title = get_the_title();
$post_date = get_the_date('M j');
$amount_of_time_to_read = get_field('amount_of_time_to_read');
?>
<a href="<?php the_permalink(); ?>" class="post-blog-link">
<div class="post">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class="post-image-v2" style="background-image:url('<?php echo $url ?>');">
</div>
<div class="post-content-v2">
<h2 class="post-title"><?php echo $title; ?></h2>
<div class="post-excerpt">
<p><?php echo get_excerpt(); ?></p>
</div>
<p class="post-date"> <span class="caps"><?php echo $post_date; ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
</div>
</div>
</a>
<!--
-->
<?php endwhile; ?>
<center>
<div class="pagination mt-25">
<?php pagination_bar( $loop ); ?>
</div>
</center>
<?php wp_reset_postdata();
endif;
?>
My functions.php
add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
global $wp_post_types;
foreach ($wp_post_types as $wp_post_type) {
if ($wp_post_type->_builtin) continue;
if (!$wp_post_type->has_archive && isset($wp_post_type->rewrite) && isset($wp_post_type->rewrite['with_front']) && !$wp_post_type->rewrite['with_front']) {
$slug = (isset($wp_post_type->rewrite['slug']) ? $wp_post_type->rewrite['slug'] : $wp_post_type->name);
$page = get_page_by_slug($slug);
if ($page) add_rewrite_rule('^' .$slug .'/page/([0-9]+)/?', 'index.php?page_id=' .$page->ID .'&paged=$matches[1]', 'top');
}
}
}
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
return ($page ? get_post($page, $output) : NULL);
}
what do you want to achieve to have a Post Type and Page to be of the same slug?
As per my understanding you want to display the archive of your custom post type "Blog". All you have to do is create a file name archive-blog.php and use the plain WordPress loop. That way you don't need to have a page-blog.php (Delete it) to display the Archives of your "Blog" post type. yourwebsite.com/blog will automatically display your "Blog" archive.
Use the code below to paste in your archive-blog.php
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
// set vars
$amount_of_time_to_read = get_field('amount_of_time_to_read');
?>
<a href="<?php the_permalink(); ?>" class="post-blog-link">
<div class="post">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); ?>
<div class="post-image-v2" style="background-image:url( '<?php echo $url ?>' );">
</div>
<div class="post-content-v2">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-excerpt">
<p><?php the_excerpt(); ?></p>
</div>
<p class="post-date"> <span class="caps"><?php the_date( 'M j' ); ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
</div>
</div>
</a>
<?php endwhile; ?>
<?php
// You need to tweak this function, it shouldn't be needing a $loop var to work
// paste the function here and may be we will take a look at that
// pagination_bar( $loop );
the_posts_pagination();
?>
<?php else : ?>
<?php // No Posts Found ?>
<?php endif; ?>
Had to add this to my functions.php
add_rewrite_rule('^blog/page/([0-9]+)','index.php?pagename=blog&paged=$matches[1]', 'top');
I'm new in wordpress. I'm trying load custom field of post from function.php. Below is code for function post grid layout function where I use custom field:
$args = array(
'post_type' => 'post',
'category_name' => 'category',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'ASC'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$c = 1;
$bpr = 5;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="member">
<div class="div-block-image">
<?php the_post_thumbnail(); ?>
</div>
<div class="div-block-29 w-clearfix">
<div class="text-block-21"><?php the_title(); ?></div>
<div class="text-block-22">subTitle</div>
<div class="text-block-23">Text...</div>
<a href="<?php the_permalink() ?>" class="more w-inline-block">
<div class="text-block-24">More</div>
</a>
<p><?php echo get_post_meta($post->ID, 'linkedin', true); ?></p> // custom-field
<p><?php echo get_post_meta($post->ID, 'bio', true); ?></p>
<a href="#" target="_blank" class="link-block w-inline-block">
<div class="biotxt">bio</div>
</a>
<a href="#" target="_blank" class="link-block w-inline-block">
<div class="text-block-20"></div>
</a>
</div>
</div>
<?
if( $c == $bpr ) {
echo '<div class="clear"></div>';
$c = 0;
}
$c++;
endwhile;
} else {
_e( '<h2>Oops!</h2>', 'rys' );
_e( '<p>Sorry, seems there are no post at the moment.</p>', 'rys' );
}
wp_reset_postdata();
I want to load this function from template page. All is loading normally except custom field:
<p><?php echo get_post_meta($post->ID, 'linkedin', true); ?></p>
If run function code from template page its running normal. Any ideas?
$post->ID is not correct, as it's picking up the ID from the global $post object, which just happens to be the same when you're on the template page, but not necessarily when you're using it in a function in functions.php. Use get_the_ID() instead.
Within your custom loop, you will want to update those to use: get_the_ID() and not $post->ID.
<p><?php echo get_post_meta(get_the_ID(), 'linkedin', true); ?></p>
This will get the ID from the current loop.
Ref: https://developer.wordpress.org/reference/functions/get_the_id/
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;
I'm trying to display 6 posts from a specific custom post type in Wordpress. Everything is working, except for when I remove the line "$wp_query = new WP_Query();". When missing that line, 20 posts gets displayed in alphabetical order and I have no idea why. wp_reset_postdata() or wp_reset_query() don't seem to do anything.
<?php $wp_query = new WP_Query( ); ?>
<div class="blog-footer row margin-top">
<?php
global $post;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-2 col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Happy to get any inputs on why this behaves the way it does. Appreciate it!
global $post is a global variable mostly used by wordpress itself and is used to get contents/ attributes of current post or page mostly..
Read details here
https://codex.wordpress.org/Function_Reference/$post
try below code.
<div class="blog-footer row margin-top">
<?php
wp_reset_query() ;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $b_post ) { ?>
<div class="col-md-2 col-sm-4">
<a href="<?php $b_post->post_name; ?>">
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $b_post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0] ?>"/>
<h4><?php $b_post->post_title; ?></h4>
</a>
</div>
<?php }
wp_reset_postdata();?>
</div>
I'm working on a website http://www.matchlessphotography.com which has got a great display of photos - it tiles for ages...
Essentially the client would like this to continue on infinately.
I have no idea how to do this and have had a look at some tutorials without any bearing...
This is how I am currently getting the posts:
<?php
global $post;
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<a class="photohovera" href="<?php the_permalink(); ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<? echo $url ?>&h=138&w=197" width="197" height="138" title="<?php the_title(); ?>" />
<span class="spanno">
<strong class="title_blog_mini_post">
<?php the_title(); ?>
</strong>
</span>
</a>
<?php endforeach; ?>
I guess I just need to do this again but with an increased offset each time...?
Paul Irishs jQuery Plugin Infinite Scroll is the way to go here.
The plugin looks for a pagination container e.g. div.navigation and an item container for the items you are going to retrieve like .photohovera in your case.
WordPress provides a function for displaying pagination links. It's called paginate_links and its default output should match the requirements of the plugin.
However you will need to change they way you are getting your posts right now from get_posts() to a WP_Query Object:
<?php
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = new WP_Query( $args );
$results = $myposts->get_results();
foreach( $results as $post ) : setup_postdata($post); ?>