I have this code for displaying a shortcode.
<?php
function recent_posts_function() {
$mypost = array( 'post_type' => 'gallery_pictures', );
$loop = new WP_Query( $mypost );
?>
<div id="boxhover">
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!--Fade-->
<?php $ddd = '<div class="mosaic-block fade">
<a href="'. $image[0] . '" data-fancybox-group="gallery" target="_blank" class="mosaic-overlay preview fancybox" title="' . the_title . '">
<div class="details">
<h4>' . the_title() . '</h4>
<p>' . the_content_rss('', TRUE, '', 30) . '</p>
<br/>
<div class="btt">VIEW</div>
</div>
</a>
<div class="mosaic-backdrop"><img src="' . $image[0] . '" alt="gallery thumbnail" /></div>
</div>';
endwhile; ?>
</div>
<?php
return $ddd;
}
function register_shortcodes(){
add_shortcode('gallery', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
as you can see from the above codes, the 'return $ddd' should return all the output from the loops that the 'while' process done but its display only one.
Im currently looking for a solution and would love to hear any suggestion, recommendations and ideas on how to do it. Thank in advance.
You need to add a [dot] before your = [equal] on the loop while.
This will cause each loop add content current with the previous.
<?php function recent_posts_function() {
$ddd = ''; //First declare the string var ?>
...
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $ddd .= '<div class="mosaic-block fade">'; // Put a [dot] before sign symbol ?>
<?php endwhile; ?>
...
return $ddd;
...
<? php } ?>
Related
I am having some trouble trying to add an if statement to hide an ACF field inside of an HTML echo.
<?php
$link = get_permalink();
$availability = get_field('availability');
$delivery_date = get_field('delivery_date');
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<a href="' .$link. '">
<div class="thumbnail" style="background: url('.$url.')">
<div class="tags one">
<span class="availability">' .$availability. '</span>
'if( get_field('delivery_date') ):' <span class="delivery-date">' .$delivery_date. '</span> 'endif;'
</div>
</div>
</a>';
}
?>
Please would someone be able to advise on where I'm going wrong with the if statement to hide the field if it's empty? At the moment it just errors the page.
Break the PHP code out of the text string, this is one way
<?php
$link = get_permalink();
$availability = get_field('availability');
$delivery_date = get_field('delivery_date');
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<a href="' .$link. '">
<div class="thumbnail" style="background: url('.$url.')">
<div class="tags one">
<span class="availability">' .$availability. '</span>';
if( get_field('delivery_date') ):
echo '<span class="delivery-date">' .$delivery_date. '</span>';
endif;
echo '</div>
</div>
</a>';
}
?>
I build this site with wordpress:
http://mida.org.il/
As you can see, homepage takes a lot of time to load.
I'm trying to fix this - there are five custom loops in that page, three of them using posts_per_page and cat to query posts, and posts_per_page set to 3.
My question is, if the loop gets to the third post, it stops and and breaks out, or its keep looping until it gets to the last post?
If the second is correct, no wonder that it's so slow, this site holds thousands of posts.
The code for the loops:
if ( $first_special_cat ){
$args = array( 'cat'=>$first_special_cat, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = $first_special_cat;
$cat_id = get_cat_ID($first_special_cat);
}else{
$args = array( 'cat'=>50, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = get_cat_name(50);
$cat_id = 50;
}
$the_query = new WP_Query($args);
echo '<div class="special-proj-main-title">';
echo '<div class="homepage-blueline-title"></div>';
echo '<h4 class="special-cat-name"><a href="' . esc_url( get_term_link($cat_id) ) . '">' . $cat_name . '</h4>';
echo '</div>';
?>
<div class="row">
<div class="col-sm-4">
<?php if ( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); //Setting the three posts to the right: ?>
<h2 class="special-project-title"><a class="special-proj-title-link" href="<?php echo esc_url( get_the_permalink() )?>"><?php the_title()?></a></h2> <br/>
<div class="post-meta special-project-meta"><?php mida_post_meta()?></div><br/>
<?php
endwhile;
wp_reset_postdata(); ?>
<span class="to-all-posts"><?php echo sprintf( __('Load more posts from %s', 'mida'), $cat_name ); ?></span>
<?php
else:
echo "You put wrong id";
endif;
?>
</div>
<div class="col-sm-8 home-background-img">
<?php
if ( $first_special_post )
$args = array('name' => $first_special_post, 'posts_per_page' => 1 );
else
$args = array('cat'=>50, 'posts_per_page' => 1, 'orderby'=>'date' );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$first_special_img = get_field('rectangular_image');
if ( $first_special_img )
$first_special_img_src = wp_get_attachment_image_src( $first_special_img['id'], 'full' );
else
$first_special_img_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
?>
<div class="special-project-section" style="background: url('<?php echo $first_special_img_src[0]; ?>');background-size: contain;">
<a href="<?php echo esc_url( get_the_permalink() )?>" title="<?php the_title() ?>"><span style="
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
z-index: 1;"></</span>
</a>
<?php
echo '<div class="special-cat-on-img">';
echo $first_special_text ? '<h5><div class="special-cat-name-img">' . $first_special_text . '</div></h5>' : '<h5><div class="special-cat-name-img">' . __('Special Project', 'mida') . '</div></h5>'; ?>
<h6 class="speical-cat-title-img"> <?php the_title() ?> </h6>
<?php echo '</div>'; ?>
<div class="blue-line"><?php echo '<div class="special-proj-ex">' . $first_special_cat_ex . '</div>'; ?></div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
</div>
X3.
every loop query different categories ( $first_special_ are custom fields, input from the user ).
So can anyone help me optimize this code (and answer the above question)?
Thanks!
You problem is NOT in your loop but rather in the page itself. Total page size is a whopping 11.8MB! This looks primarily due to a ton of images. You might try a little image optimization (use jpg's for post thumbs/images) and make sure images are sized correctly. Honestly, lazy loading might be a good solution here! There are plenty of options out there to make it easy to implement.
I have this code:
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
Which gets a post which has the tag 'testtag'.
Instead of copying this code, and using 'testtag1', 'testtag2'
How can I just grab ALL the posts which have the tag 'testtag' and just keep adding them into 4 columns?
Any links/help
Nevermind, simple fix!!
<div class="row" style="margin-top:20px;">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
</div>
I have a theme with a portfolio that uses filters (ALL, NEW, OLD) to display categories. However, on load, I wish for a particular ("new") category to be the ONLY category displayed.
Then if a user wishes to see past products, they'd click "OLD" and so on..
Please keep in mind that I would create another portfolio and that portfolio's new items would be displayed only on load at first.
I've attempted to find the answer for hours but I have yet to be able to find it.
Here is the code of the portfolio php page:
<?php
/**
* Taxonomy Template for the Portfolios
*/
?>
<?php get_header(); ?>
<?php
if ( $post ) :
$port = wp_get_post_terms( $post->ID, array('portfolios') );
$portfolio = $port[0];
$post_img_width = "";
$post_img_height = "";
global $mav_data; // fetch options stored in $mav_data
$portfolio_order_1 = $mav_data['portfolio_order_1']; // date, title
$portfolio_order_2 = $mav_data['portfolio_order_2']; // ASC, DESC
$tax_query[] = array(
//'taxonomy' => $wp_query->query_vars['taxonomy'],
'taxonomy' => 'portfolios',
'field' => 'slug',
'terms' => array( $portfolio->slug )
);
$args = array(
'post_type'=> 'project',
'posts_per_page'=> -1,
'orderby' => $portfolio_order_1,
'order' => $portfolio_order_2,
'tax_query' => $tax_query
);
$port_query = new WP_Query($args);
?>
<?php
$tag_extra_fields = get_option('portfolios_fields');
$portfolio_layout = $tag_extra_fields[$portfolio->term_id]['_portfolio_layout'];
?>
<section id="content" class="portfolio one-column <?php echo esc_attr( $portfolio_layout ); ?>" role="main">
<div class="wrapper clearfix">
<header class="page-header">
<h1 class="entry-title"><?php echo( $portfolio->name ); ?></h1>
<?php if ($portfolio->description) { ?><p class="portfolio-header-description"><?php echo( $portfolio->description ); ?></p><?php } ?>
<?php
$temp = array();
foreach ( $posts as $post ) {
$temp_args = array();
while ($port_query->have_posts()) : $port_query->the_post(); // the loop begins, we need it here. It's important!!
$temp_cats = wp_get_object_terms( $post->ID, 'project_category'/*, $args*/ );
if ( $temp_cats ) {
foreach ( $temp_cats as $temp_cat ) {
if ( ! in_array( $temp_cat->slug, $temp ) ) {
$temp[] = $temp_cat->slug;
$categories[] = $temp_cat;
}
}
}
endwhile;
}
// DAHEX
$temp = array();
if(!empty($categories)) {
foreach ( $categories as $category ) {
$temp[] = array ('term_id'=>$category->term_id,'name'=>$category->name,'slug'=>$category->slug,'term_group'=>$category->term_group,'term_taxonomy_id'=>$category->term_taxonomy_id,'taxonomy'=>$category->taxonomy,'description'=> $category->description,'parent'=>$category->parent,'count'=> $category->count);
}
}
usort($temp, array(new Sorter('slug'), 'sort')); // Sorting Array by slug
$categories = array();
foreach ( $temp as $category ){
$categories[] =(object) $category;
}
// DAHEX
if(!empty($categories)) {
if(!is_wp_error( $categories )){
echo( '<ul id="filters" class="option-set">
<li>All</li>' );
foreach ( $categories as $category ) {
echo '<li><span class="sep">/</span>' . $category->name . '</li>';
}
echo( '</ul> <!-- /end #filters -->' );
} else {
echo '<span class="ooops">';
_e( 'Ooops! nothing found...', 'mav_framework' );
echo '</span>';
}
}
?>
</header> <!-- /end .page-header -->
<section id="projects">
<?php
while ($port_query->have_posts()) : $port_query->the_post(); // the loop begins
$terms = get_the_terms( get_the_ID(), 'project_category' );
$terms = $terms == false ? array() : $terms;
?>
<?php
$custom = get_post_custom($post->ID);
$portfolio_permalink = $custom["project_permalink"][0];
$portfolio_desc = $custom["project_desc"][0];
if ( !isset( $lightbox_path ) ) {
$lightbox_path = '';
}
// Prepare Project Image Thumb
$project_img = $custom["project_img"][0];
$project_img_ID = $custom['project_img_ID'][0];
// Need some proof check to ensure that no "notice" is thrown ...
if ( ! empty( $portfolio ) ) {
$term_slug = $portfolio->slug;
if ( isset( $custom["lightbox_path"][0] ) ) {
$lightbox_path = $custom["lightbox_path"][0];
} else {
$lightbox_path = '';
}
$empty_thumb = '<img class="portfolios_single_thumb portfolio-image" src="' . get_template_directory_uri() . '/images/thumb.png" width="' . $post_img_width . '" height="' . $post_img_height . '" alt="' . $post->post_title . '" />';
if ( isset( $project_img_ID ) ) {
if ( is_numeric( $project_img_ID ) ) {
$thumb_ID = $project_img_ID;
$thumb = wp_get_attachment_image( $thumb_ID, 'mav-thumbnails', false, array( 'class' => 'portfolios_post_image_thumb portfolio-image', 'alt' => $post->post_title ) );
if ( empty ($thumb) ) {
$thumb = $empty_thumb;
}
} elseif( $project_img_ID != "" ) {
$thumb = '<div class="project_iframe_thumb-$term_slug"><iframe width="' . $post_img_width . '" height="' . $post_img_height . '" src="' . $project_img . '" title="' . $project_img_ID . '" frameborder="0" allowfullscreen></iframe></div>';
} else {
$thumb = $empty_thumb;
}
} else {
$thumb = $empty_thumb;
}
}
?>
<article id="project-<?php the_ID(); ?>" class="element <?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->slug)). ' '; } ?>">
<?php
/**
* Generate the Project Image (Thumb)
*/
if ( $lightbox_path != '' ) { ?>
<figure class="thumb-container">
<a href="<?php echo esc_attr( $lightbox_path ); ?>" data-rel="prettyPhoto" title="<?php the_title_attribute(); ?>">
<span class="overlay lightbox"></span>
<?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
<?php echo( $thumb ); ?>
</a>
</figure> <!-- /end .thumb-container -->
<?php
} elseif ($portfolio_permalink) {
?>
<figure class="thumb-container">
<a target="_blank" href="<?php echo esc_attr( $portfolio_permalink ); ?>" rel="bookmark">
<span class="overlay link"></span>
<?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
<?php echo( $thumb ); ?>
</a>
</figure> <!-- /end .thumb-container -->
<?php } else { ?>
<figure class="thumb-container">
<a href="<?php the_permalink() ?>" rel="bookmark">
<span class="overlay">
<span class="view"><?php _e( 'View', 'mav_framework' ); ?></span>
</span>
<?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
<?php echo( $thumb ); ?>
</a>
</figure> <!-- /end .thumb-container -->
<?php } // end Generate the Project Image (Thumb) ?>
<h2 class="project-title">
<?php if ($portfolio_permalink) { ?>
<a target="_blank" href="<?php echo esc_url( $portfolio_permalink ); ?>" rel="bookmark"><?php the_title(); ?></a>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
</h2>
<?php if ($portfolio_desc) { ?><p class="project-description"><?php echo do_shortcode(stripslashes($portfolio_desc)); ?></p><?php } ?>
<footer class="entry-meta">
<span class="posted-on">
<?php _e( '<span class="posted-on-title">Posted on: </span>', 'mav_framework' ); ?><?php /* http://codex.wordpress.org/Formatting_Date_and_Time */ echo get_the_date('F j, Y'); ?>
</span> <!-- /end .posted-on -->
<?php
$project_categories = wp_get_object_terms($post->ID, 'project_category');
if ($project_categories) { ?>
<span class="cat-links">
<?php
_e( '<span class="cat-links-title">Category: </span>', 'mav_framework' );
$project_category = array();
foreach($project_categories as $category) {
$project_category[] = '' . $category->name . '';
}
echo implode(', ', $project_category);
?>
</span> <!-- /end .cat-links -->
<?php } ?>
<?php // Project Tags
$project_tags = wp_get_object_terms($post->ID, 'project_tag');
if ($project_tags) {
$project_tag = array();
foreach($project_tags as $tag) {
$project_tag[] = '' . $tag->name . '';
} ?>
<span class="tag-links">
<?php _e( '<span class="tag-links-title">Tagged: </span>', 'mav_framework' ); ?> <?php echo implode(', ', $project_tag); ?>
</span> <!-- /end .tag-links -->
<?php } ?>
</footer> <!-- /end .entry-meta -->
</article> <!-- /end #project-<?php the_ID(); ?> .element -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</section> <!-- /end #projects -->
</div> <!-- /end .wrapper -->
</section> <!-- /end #content -->
<?php endif; ?>
<?php
/**
* Add portfolio bottom quote
*/
if ( $mav_data['portfolio_quote_text'] ) : ?>
<section id="portfolio-quote" class="clearfix">
<div class="wrapper clearfix">
<?php get_template_part( 'inc/block_portfolio_quote' ); ?>
</div> <!-- /end .wrapper -->
</section> <!-- /end #portfolio-quote -->
<?php endif; ?>
<?php get_footer(); ?>
This problem is not related with the Wordpress itself but with the plugin that you, or your theme are using to sort the data.
In this case, it's using Isotope, which is what is filtering the results you see on the page.
By addressing the Isotope's documentation in terms of filtering, we see that the filters can be done through functions or buttons, in this case we're using a combination of both as we want the data to change while clicking but at the same time it already needs to come filtered, as you requested.
So, to solve the problem, it's one line in the isotope's function file:
$container.isotope({
itemSelector : '.element',
filter: '.workshops'
});
I just added the last one.
Apart from this, you just need to hack the code you provided in order to add the "selected" class to the button:
echo( '<ul id="filters" class="option-set">
<li>All</li>' );
foreach ( $categories as $category ) { ?>
<li>
<span class="sep">/</span>
<?php echo $category->name;?>
</li>
<?php
}
echo( '</ul>' );
Now some considerations:
If you change the slug, this will stop working as it's checking for the class "workshops", if you do it, you need to edit the code provided above;
Isotope was masked inside your theme's custom jquery file, which is not a good practice at all, specially for maintenance issues.
I am using this in my mu-plugins.php file//
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<span>Cool stuff brah.</span>
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
and in my page template I used <?php the_content(); ?> to display everything.
UPDATE//
Full Page Template code
<div id="container" class="clearfix">
<div id="left-content">
<?php get_sidebar('two');?>
</div>
<div id="right-content">
<h1><?php wp_title(''); ?></h1>
<ul class="grid cs-style-3">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--right-content-->
</div><!--container-->
<?php get_footer(); ?>
But I am recieving this error//
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/domain.com/testing/wp-content/mu-plugins/must-use.php on line 15
I am trying this method of displaying content because I want to use the WP-Members plugin, and I realized that the plugin will only work for content within the_content().
So my question is how can I fix the code I posted above to display the title, thumbnail, links etc the correct way?
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>';
$content .='<figure>';
$content .= the_post_thumbnail('thummy');
$content .= '<figcaption>';
$content .= '<h3>';
$content .= the_title();
$content .= '</h3>';
$content .= '<span>Cool stuff brah.</span>';
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>';
}
$content .= '</figcaption>';
$content .= '</figure>';
$content .= '</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
Just try this code. If you still get same error we'll check.
FOUND A SOLUTION
I remembered that you can add content inside of shortcodes.
So I created my own shortcode [textures_content]
Then used the code pasted below to display the content within <?php the_content(); ?> function//
add_shortcode( 'textures_content', 'textures_shortcode' );
function textures_shortcode( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'textures',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Now the Members only plugin works as expected and the blocked content is displayed once logged in :)