Custom post type loop - php

I need some guidance on this script.
I'm working on a custom post type loop but I'm stuck on how to convert this static html to a php loop
<?php
$loop = new WP_Query(array('post_type' => 'project', 'posts_per_page' => -1));
$count =0;
?>
<!--Text Sliders-->
<div class="ps-contentwrapper">
<?php if ( $loop ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'tagproject' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<div class="ps-content">
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div><!--end of ps-content-->
</div><!-- /ps-contentwrapper -->
<!--Image Sliders-->
<div class="ps-slidewrapper">
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<nav>
</nav>
Here is the tutorial I'm trying to convert. Demo.
What I'm trying to figure out is how to line up everything dynamically. If someone can point me to the right direction I appreciate it.
My version of it with the current code I pasted above.
EDIT: Here is my code now after a bit of research. Now I'm trying to figure out how I can match the featured image to the appropriate post as it cycles through in this script. The div tag that echos the url needs to loop as many times the loop does and cycle appropriately.
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div><!--end of ps-slides-->
Full code below:
<div class="ps-contentwrapper">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ps-content">
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!--end of contentwrapper-->
<!--Image Sliders-->
<div class="ps-slidewrapper">
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div><!--end of ps-slides-->
<nav>
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
$prev_url = wp_get_attachment_url( get_post_thumbnail_id($id) );
?>
<?php
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
$next_url = wp_get_attachment_url( get_post_thumbnail_id($nid) );
?>
</nav>
</div>

use code for your previous next post image url:
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
$prev_url = wp_get_attachment_url( get_post_thumbnail_id($id) );
?>
<?php
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
$next_url = wp_get_attachment_url( get_post_thumbnail_id($nid) );
?>

Related

Display images with (post object) field type ACF

I have a ACF field group with post object field type. I have 8 products to display. I have displayed the title and permalink but can not display the product images. Any help would be appreciated. Here is my code:
<div class="col-md-12">
<?php
$featured_posts = get_field('products_images');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post);
$title = get_the_title( $featured_post);
$custom_field = get_field( $featured_post);
?>
<li>
<?php echo get_the_post_thumbnail( $custom_field); ?>
<?php echo esc_html( $title ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
You should use like this
<div class="col-md-12">
<?php
$featured_posts = get_field('products_images');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post);
$title = get_the_title( $featured_post);
$custom_field = get_field( $featured_post);
?>
<li>
<?php echo get_the_post_thumbnail( $featured_post, 'full' );?>
<?php echo esc_html( $title ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>

Returning string from function

So I have a function in functions.php and I want to call it in other parts of my wordpress site with shortcode, but the problem is that I don't know how to save results of my function into a variable. If anyone can help me I would be very grateful. This is the php code:
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
echo $posts;
if( $posts ): ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('poslovna_darila') ): ?>
<?php while( have_rows('poslovna_darila') ): the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif;
}
add_shortcode ('slick_slider' , 'get_slider'); ?>
The code returns array, but I need to get a value out of it or if it's possible the slider.
You can use an output buffer to return the string in your shortcode.
<?php
function get_slider() {
$args = array(
'post_type' => 'something',
);
$posts = get_posts($args);
//echo $posts;
ob_start(); // start the output buffer
if ( $posts ) : ?>
<div class="custom-posts-grid">
<?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<?php if ( have_rows('poslovna_darila') ): ?>
<?php while ( have_rows('poslovna_darila') ) : the_row(); ?>
<div class="slider_slick">
<?php // vars
$image = get_sub_field('thumbnail_for_poslovna_darila');
$link = get_sub_field('url_poslovna_darila');
$count = count($posts);
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
// return the contents of the output buffer as a string
return ob_get_clean();
}
add_shortcode( 'slick_slider' , 'get_slider' ); ?>

How Do I Apply Custom Field Link to Slide Button?

I created a relational custom field (slide_link) to link my slides to pages, but i am having difficulty applying the link to my slider button in the home.php file. Here are my codes:
<div class="flexslider">
<ul class="slides">
<?php
$query = new WP_Query( array('post_type' => 'slide') );
while ( $query->have_posts() ) : $query->the_post();
?>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
$url = $thumb['0'];
?>
<?php
global $post;
$meta = get_post_meta( $post->ID );
$captn = isset( $meta['caption'][0] ) ? filter_var( $meta['caption'][0], FILTER_SANITIZE_STRING ) : '';
$slideurl = isset( $meta['slide_link'][0] ) ? filter_var( $meta['slide_link'][0], FILTER_SANITIZE_STRING ) : '';
?>
<li data-thumb="<?php echo $url; ?>">
<img src="<?php echo $url; ?>" />
<p class="flex-caption"> <?php echo($captn); ?> </p>
See More
</li>
<?php endwhile; ?>
</ul>
</div><!-- /home banner -->
Function.php where I registered the custom field
//function to register vision field
add_filter('the_permalink', 'getCustomFeature6');
function getCustomFeature6($slideurl) {
global $post;
$meta = get_post_meta($post->ID, 'slider_link', true);
return $slideurl;
}
Any help would be appreciated!
I do not understand why are you using add_filter('the_permalink', 'getCustomFeature6'); . I would use this:
<?php $slideurl = get_post_meta($post->ID, "slider_link", true); ?>
<li data-thumb="<?php echo $url; ?>">
<img src="<?php echo $url; ?>" />
<p class="flex-caption"> <?php echo($captn); ?> </p>
See More
</li>

Wordpress: Next/Previous post link are not working

Please let me know why Next and Previous Post links are not working on the following code? I am trying to display only one post at a time. I have tried to check on different post but not able to find something similar to what I have. Please guide...
<?php
$args = array( 'numberposts' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
<?php comments_template(); ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
<div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
</nav>
I think I had the same problem as you, simply adding this will make the link go to previous_post_link or next_post_link
<?php previous_post_link( '%link','Previous' ) ?>
<?php next_post_link( '%link','Next' ) ?>
Best of luck
According to the get_posts documentation, you should use the following method to display prev/next links:
<?php
$postlist = get_posts( 'orderby=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->

display just 10 items in custom template

Hi i have this template in wordpress, i would like to just display 10 items, because right now in general options in wordpress i have 5 items but i would like to make an exception with this template i dont know where i have to modify the code in order to show in this template 10 items:
<?php
/*
Template Name: Blog List
*/
?>
<?php get_header(); ?>
<div class="content-wrap">
<div class="content">
<?php tie_breadcrumbs() ?>
<div id="content" class="podcast_archive">
<!--<div class="podcast_full">-->
<?php if ( have_posts() ) : ?>
<header><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<h1><?php _e( 'El Jurado del Pueblo' , 'ss-podcasting' ); ?></h1>
</header>
<?php
$feed_url = trailingslashit( home_url() ) . '?feed=podcast';
$custom_feed_url = get_option('ss_podcasting_feed_url');
if( $custom_feed_url && strlen( $custom_feed_url ) > 0 && $custom_feed_url != '' ) {
$feed_url = $custom_feed_url;
}
$itunes_url = str_replace( array( 'http:' , 'https:' ) , 'itpc:' , $feed_url );
?>
<section>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post(); ?>
<?php
$terms = wp_get_post_terms( get_the_ID() , 'series' );
foreach( $terms as $term ) {
$series_id = $term->term_id;
$series = $term->name;
break;
}
?>
<article class="podcast_episode">
<?php if( has_post_thumbnail() ) { ?>
<?php $img = wp_get_attachment_image_src( get_post_thumbnail_id() ); ?>
<a>" title="<?php the_title(); ?>">
<?php the_post_thumbnail( 'podcast-thumbnail' , array( 'class' => 'podcast_image' , 'alt' => get_the_title() , 'title' => get_the_title() ) ); ?>
</a>
<?php } ?>
<h3>
<strong><?php the_title(); ?></strong>
<div class="podcast_meta"><?php echo $series; ?><aside></div>
</h3>
<div id="audio">
<?php global $ss_podcasting;
$enclosure = $ss_podcasting->get_enclosure( get_the_ID() );
if( $enclosure ) {
$audio_player = $ss_podcasting->audio_player( $enclosure );
echo $audio_player;
} ?>
<?php the_content(); ?>
</div>
<div id="audioinfo">
<a>">Descargar Audio</a>
<span class="audiometa">
TamaƱo: <?php echo get_post_meta(get_the_ID(), 'filesize', true) ?>
</span>
</div>
<?php echo do_shortcode('[divider]');?>
</article>
<?php
endwhile;
?>
</section>
<?php endif; ?>
<?php wp_pagenavi(); ?>
<div class="podcast_clear"></div>
</div>
<?php comments_template( '', true ); ?>
</div><!-- .content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
?>
i would some advise so i can do it by myself :)
thank you very much
You'll want to use the query_posts() function for that, just before your loop.
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'showposts' => '10' ) );
query_posts( $args );
http://codex.wordpress.org/Function_Reference/query_posts
You'd need to modify your template's query. I'd suggest making use of the WP_Query class. For example:
$args = array(
'posts_per_page' => 10
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Loop item here
}
wp_reset_postdata();
} else {
// No results found
}
if
$args = array(
'posts_per_page' => 10
);
not work than check at back-end under Setting->Reading there is a "Blog pages show at most" if there is 5 post than it display only 5 post.

Categories