wordpress featured posts - wordpress

i'm trying to create a portfolio website using wordpress,
each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"
now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails
i tried this
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php }; ?>
<div class="entry-content">
</div><!– .entry-content –>
<?php endwhile; ?>
(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)
but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))
this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be
thanks for your help

The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:
<?php
query_posts(array("meta_key" => "type", "meta_value" => "featured"));
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Display thumbnails -->
<?php endwhile; endif; ?>

Related

Plotting ACF repeater on a Google Map

Im building out a wordpress site that plots custom data on a google map using Advanced Custom Fields Pro but can't get it to generate the map using the custom page I have created.
I have created a Google Map Picker with ACF which I have assigned to a post category type ID 4. This is functioning as expected and I can pick the location for each post
In my custom template for wordpress site I have entered the below code to call any location information from the Category in question in this case its ID is 4
This Section pulls and displays the location as expected on the page ( I will be hiding it in the final build)
<?php
$catquery = new WP_Query( 'cat=4&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<?php the_field('location'); ?>
<?php endwhile; ?>
</div>
However the map just pulls the first blog an does not display the other
Below is the code im using to call the location and pass it into the map
<?php if( have_rows('sdg_location') ): ?>
<div class="acf-map">
<?php while ( have_rows('sdg_location') ) : the_row();
$location = get_field('location');
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>"
data-lng="<?php echo $location['lng']; ?>">
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
Can anyone advise of a better method of doing this? Is there a way to pass the location from the categories directly into the map call?
Below is a screenshot of whats happening can anyone advise?
Thanks a million for your help in advance
Screenshot of map and locations loading
Ended up using this code
<? $map_posts = get_posts(array(
'post_type' => 'post',
'cat' => '4'));
if( $map_posts ): ?>
<div class="acf-map">
<?php foreach( $map_posts as $map_post ) :
$location = get_field('location', $map_post->ID);
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
</div>
<?php endforeach; ?>
</div>

Slider with ACF field-type 'Image'

For a portfolio page, I have created a custom post type for each reference called “referenzen”. Since I need more than one image per reference (displayed in different ways), I have created some custom post types.
Now, on the front page, I want to display the newest references in a slider (kind of this type, exmple 4: http://www.wpcue.com/wordpress-plugins/advanced-post-slider/template-one/).
I tried to use some plugins, but there just created for regular post thumbs.
For now, I’m having the following code to display the latest 3 references.
<section class="entry-content cf" itemprop="articleBody">
<?php query_posts( array (
'post_type' => 'referenzen',
'posts_per_page' => 4 // minus 1
)); while ( have_posts() ) : the_post(); ?>
<?php
$image = get_field('bg');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
Now I need to bring this into some slider/carousel…
Any help is much appreciated
If you need to show posts in the form of slider/carousel, please follow below steps
1. Please download bxslider.js & include it in header.php of your theme file like below -
<script src="your-theme-directory-path/js/jquery.bxslider.min.js"></script>
Make sure that field you have created to upload image is of image type with "Image Object" option.
Correct Your code as below -
'referenzen',
'posts_per_page' => 4 // minus 1
)); while ( have_posts() ) : the_post(); ?>
<?php
$image = get_field('bg');
if( !empty($image) ): ?>
<li> <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></li>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
</ul>
Add below Javascript code in the footer.php
jQuery('.slider2').bxSlider({
slideWidth: 350,
minSlides: 1,
maxSlides: 1,
slideMargin: 5
});
</script>

Getting post id in WP_Query

I am trying to execute the query in WordPress to retrieve the last post from featured category, then check for its custom field and show an image stored in it.
Well, my code is not working for some reason.Can you spot any error there? Or point me in the right direction?
<?php $featured = new WP_Query('showposts=1&category_name=featured'); ?>
<?php if($featured->have_posts()) : ?>
<?php while($featured->have_posts()) : $featured->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'image3', true); ?>"></a>
<?php endwhile; ?>
<?php endif; ?>
Many thanks!
I hope this will do what you expecting
$args['category_name']='featured'; // posts from particular category
$args['orderby']='date'; //to get latest post
$args['order']='DESC';
$args['numberposts']='1';// to get only one post from post list
$featured = new WP_Query($args);

display posts under specific page

I am having many number of pages, I would like to display some specific posts under one specific page.
for example Under News page i need to display only news related posts only
how to do this?
This is a new query, won't effect the main Wordpress loop and can be used multiple times on a page. Change parameters to your category name and number of posts to show. Use in a page template or in page editor with php execution enabled.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; ?>

Linking to list of blog posts with wordpress?

I've made my front page a static page with wordpress. I would still like to be able to link to a listing of blog posts. How is this done?
create a new page template file called page-home.php
best way to do this is copy and paste the code from index.php into your new page...
inside this page create a new template call..
<?php
/**
Template Name: home
*/
?>
inside this page paste your index.php code..
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content('<p>Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php endwhile; endif; ?>
this will display your home page as usual, then you have to create a new call to the wordpress database to display your latest posts from which ever categories you want or show all posts from all categories..
<?php
//The Query
query_posts('posts_per_page=5&cat=YOUR_CATEGORY_HERE');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
this will then loop through the posts and display your latest posts..
have a check through the wordpress query posts codex

Resources