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/
Related
i have a problem when trying to show a current 4 post using WP_Query.
But, the older post (The very first post) also shown in the first loop.
This is my code:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
This is the result =>
How to fix this problem? is there any problem with my code?
Thankyou.
If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.
Source # https://developer.wordpress.org/themes/functionality/sticky-posts/
I'm pretty sure you applied is sticky to your post from 2012.
To verify you can just add the following to your loop inside the while statement:
<?php //...
while( have_posts() ): the_post();
if( is_sticky() ):
echo 1;
else: echo 0;
endif;
endwhile;
//... ?>
Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
<?php
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
<h2><?php echo the_title(); ?></h2>
<div><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<?php
the_content();
echo '<div class="entry-content">';
echo '</div>';
echo "</div></a>";
endwhile;
?>
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 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;
So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
Taxonomy image
page image
Here, I have categorized my page with different page. In that, I categorized with taxonomy and slug. All the title have different taxonomy as shown in the image. But here I am getting multiple image because it have multiple taxonomy.
So, I just want 8 images as the Business theme are as shown in images and have to display as per the taxonomy.
As you can see the taxonomy count that is 4,4 and 5, So I am getting record like that, but I want to get record as per Business theme, as they are eight but as per taxonomy.
Here what I have tried:
<div class="portfolio-items">
<?php
$terms = get_terms('portfolio_cat');
foreach($terms as $row)
{
$args = array('post_type' => 'portfolio','tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug',
'terms' => array( $row->slug ),
)
));
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
$i=1;
?>
<div class="portfolio-item <?php echo $row->slug; ?> col-xs-12 col-sm-4 col-md-3">
<div class="recent-work-wrap">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php $large_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'large' ); ?>
<img class="img-responsive" src="<?php echo $url; ?>" alt="">
<div class="overlay">
<div class="recent-work-inner">
<h3><?php the_title(); ?></h3>
<?php $content = get_the_content(); ?>
<p><?php echo $content; ?></p>
<a class="preview" href="<?php echo $large_url; ?>" rel="prettyPhoto"><i class="fa fa-eye"></i> View</a>
</div>
</div>
</div>
</div><!--/.portfolio-item-->
<?php
// End of the loop.
$i++;
endwhile;
endif;
WP_Reset_Query();
}
?>
</div>