i'm trying to display my portfolio items in a gird, but can't quite figure it out. ideally, i'd like the thumbnails to display 3 or 4 across(with the rest forming a second and/or third line) and once the thumbnails are clicked on, i'd like it to go to my normal post page.
css-
/* Portfolio */
#content {
width: 603px;
}
#portfolio-item {
display: inline;
float: left;
margin: 0 0 0 60px;
width: 280px;
}
#portfolio-item h1 {
font-size: 16px;
margin: 20px 0 5px 0;
}
#portfolio-item img {
height: 50%;
width: 50%;
}
portfolio.php:
<?php
/*
Template Name: Portfolio
*/
?>
<?php get_header(); ?>
<div id="portfolio">
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 10));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div id="portfolio-item">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
functions.php:
<?php // sidebar functions
if(function_exists('register_sidebar'))
{
register_sidebar();
}
// portfolio functions
add_action('init', 'create_portfolio');
function create_portfolio() {
$portfolio_args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('portfolio',$portfolio_args);
}
// custom input- portfolio backend
add_action("admin_init", "add_portfolio");
add_action('save_post', 'update_website_url');
function add_portfolio(){
add_meta_box("portfolio_details", "Portfolio Options", "portfolio_options", "portfolio", "normal", "low");
}
function portfolio_options(){
global $post;
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
}
function update_website_url(){
global $post;
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}
// detail columns- portfolio backend
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
add_action("manage_posts_custom_column", "portfolio_columns_display");
function portfolio_edit_columns($portfolio_columns){
$portfolio_columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Project Title",
"description" => "Description",
);
return $portfolio_columns;
}
function portfolio_columns_display($portfolio_columns){
switch ($portfolio_columns)
{
case "description":
the_excerpt();
break;
}
}
// add thumbnail support
add_theme_support('post-thumbnails');
// disable autoformat with raw
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
?>
http://www.cssbakery.com/2010/07/image-grid-using-css-floats_6950.html
This should be what you're looking for.
You can hardcode it was well with PHP if you'd like using a table if you can't get that CSS working, but that's not good coding technically.
Related
When I paste code in the function.php, my website stop working and an error shows up "The site is experiencing technical difficulties"
/**
* Adding Shortcode 'box'
*/
function boxShow($atts, $content = null ){
//default values
$option = shortcode_atts( array(
'type' => '',
), $atts );
ob_start();
$class = $option[ 'type' ] ? str_replace( '"', '', $option[ 'type' ] ) : 'normal';
//HTML goes here
?>
<div class="box <?php echo $class; ?>"><?php echo $content; ?></div>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode( 'box', 'boxShow' );
and CSS Code
.shadow , .success , .info{
padding: 12px 12px;
background-color: #fafafa;
border: 1px solid black;
margin-bottom:8px;
I have this code that retrieves all images for the active page:
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
I need some help to open the images in a lightbox with the post/page title and navigation such as this https://simplelightbox.com/ one.
I am just learning the code so I desperately need some help.
I tried using fancy lightbox plugin but that did not work as the lightbox did not launch. For now I am hiding and showing the images using javascript.
From your code, I've created the following shortcode:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail');
$output .= '</a>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
Add [all_images_lightbox] to the site you want the gallery to be on. And it will display the images with a link to the image file.
Then you'll also need a lightbox-container somewhere in your documents (preferably short before the footer to avoid stacking problems):
function so_output_lightbox_container() { ?>
<div id="imagelightbox" class="image-lightbox">
<a id="close-lightbox" title="Close Lightbox" class="close-lightbox"></a>
<div id="imagecontainer"></div>
</div>
<?php }
add_action('get_footer', 'so_output_lightbox_container');
and the styling for it (you could also put this in the customizer css or your child theme css)
function so_output_lightbox_container_css() { ?>
<style>
.image-lightbox {
display: none;
z-index: -1;
background-color: rgba(0,0,0,.90);
width: 0;
height: 0;
position: fixed;
top: 0;
left: 0;
}
.image-lightbox.active {
display: block;
z-index: 99;
width: 100%;
height: 100%;
}
#imagecontainer {
display: flex;
align-items: center;
justify-content: center; height: 100vh;
position: relative;
}
</style>
<?php }
add_action('wp_head', 'so_output_lightbox_container_css');
Finally, you'd need some javascript to load the according images to the lightbox instead of follwing the link:
function so_output_lightbox_container_js() { ?>
<script>
( function() {
lightboxInit = function () {
const lightboximage = document.getElementsByClassName('lightboximage');
const imagelightbox = document.getElementById('imagelightbox');
const imagecontainer = document.getElementById('imagecontainer');
const lightboxbclose = document.getElementById('close-lightbox');
Array.prototype.forEach.call(lightboximage, function (el) {
el.addEventListener('click', function () {
event.preventDefault();
let imgfile = this.getAttribute("href");
imagelightbox.classList.add("active");
imagecontainer.innerHTML = '<img alt="Click to close" src="' + imgfile + '">';
});
});
lightboxbclose.addEventListener('click', function () {
imagelightbox.classList.remove("active");
imagecontainer.style.height = '';
imagecontainer.style.paddingBottom = '';
});
imagelightbox.addEventListener('click', function () {
imagelightbox.classList.remove("active");
});
}
lightboxInit();
} )();
</script>
<?php }
add_action('wp_footer', 'so_output_lightbox_container_js');
Tested by quickly throwing all of it in my functions.php and adding the shortcode to a site: Works. Click anywhere to close the lightbox. The styling of the additional close-button, I'll leave to you ;-)
Update/Edit:
To run the function from a template, just call the function within the template instead of running the shortcode. Add this to the template where needed:
echo so_output_lightbox_container();
You can then remove the line
add_shortcode('all_images_lightbox','so_all_images_lightbox');
of the first code block then, or leave it in place in case you may need the shortcode as well later.
Update 2:
To use an icon as a link, try replacing the first block with:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<span class="imgbx"><a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= '<i class="fas fa-camera"></i>';
$output .= '</a>';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail').'</span>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
I'm using the below code to make wordpress search only by product titles. However, the woocommerce "Filter by Attributes" widget still shows filters as if the search was done on body text too.
So for example if I search "face" the results that show up all have "face" in the title but the Filter By Attribute widget will still show Chocolate as an attribute because there is a chocolate bar that has a description "Face it, we all love chocolate"
Then if you click the "Chocolate" filter, it will show no results, because the search is only showing results with "face" in the title.
I don't even know where to begin looking to change the behaviour of the filter by attributes widget.
// Search titles only
function __search_by_title_only( $search, $wp_query )
{
global $wpdb;
if(empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search =
$searchand = '';
foreach ((array)$q['search_terms'] as $term) {
$term = esc_sql($wpdb->esc_like($term));
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if (!empty($search)) {
$search = " AND ({$search}) ";
if (!is_user_logged_in())
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);
I've cobbled together my own filter that plays nice with the above search (only the titles) and doesn't show any Brands that have items that don't have the keyword in the title. My only issue now is figuring out how to get the count of items.
<?php
$searchterm = htmlentities($_GET["s"]);
$filter_brand = htmlentities($_GET["filter_brand"]);
$current = $_SERVER['REQUEST_URI'];
$site_url = home_url();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
's' => $searchterm
);
// The Query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) :
echo'<div id="woocommerce_layered_nav-3" class="widget woocommerce widget_layered_nav woocommerce-widget-layered-nav">
<ul class="woocommerce-widget-layered-nav-list">';
$unique_singler = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
$brand_terms = get_the_terms( $post, 'pa_brand' );
$singler = array_values($brand_terms)[0];
$name = $singler->name;
$name_string = strtolower($name);
$name_slug = str_replace(" ","-",$name_string);
// only create option if city hasn't been added yet
if( ! in_array( $name, $unique_singler ) ) :
// add city to array so it doesn't repeat
$unique_singler[] = $name;
;?>
<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term <?php if (!($filter_brand == $name_slug) && !empty($filter_brand)) { echo 'hidden'; }?>">
<?php echo '<a class="remover ';
if ($filter_brand == $name_slug) { echo 'shown'; }
echo '" href="'.$site_url.'/?s='.$searchterm.'&tags=1&ixwps=1&post_type=product"><i class="fas fa-times"></i></a>';
?><?php
echo '<a class="dup" href="'.$current.'&filter_brand='.$name_slug.'">'.$name.'</a> <span class="count">(' .count($XXXXXXX). ')</span>';
?></li>
<?php
endif;?>
<?php endwhile;?>
</ul>
</div>
<?php else :?>
<p>Nothing to see</p>
<?php endif; ?>
<style>
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover {
display:none;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover.shown {
display:inline-block;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term.hidden
{
display:none;
}
.x-sidebar .widget ul li {
border-top: 1px solid rgba(0,0,0,0.085) !important;
}
ul.woocommerce-widget-layered-nav-list
{
border-top: 1px solid rgba(0,0,0,0.085) !important;
border-bottom: 1px solid rgba(0,0,0,0.085) !important;
}
.woocommerce-widget-layered-nav-list, li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term {
margin-left:0 !important;
}
.x-sidebar .widget ul li a {
padding-top: 8px;
padding-bottom: 8px;
}
div#woocommerce_layered_nav-3 {
margin: 0;
}
a.remover.shown i {
text-decoration: underline !important;
margin-right:0.5em;
}
</style>
I am trying to write a custom page type that gives a page a random background in Wordpress. (i.e nice big image on the homepage). I have got it working! But am returning a large image, any ideas how to quickly add make it display the full image size URL?
Help much appreciated!
(I have stuck the full code in so anyone can use it on their own sites if it helps?)
<?php
/**
* Template Name: Fullscreen Random
*/
get_header();
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<style media="screen" type="text/css">
body {
background: url(<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'orderby' => 'rand', 'post_status' => null, 'post_parent' => 5 );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo $attachment->guid;
}
}
?>) no-repeat center center fixed;
webkit-background-size: cover;
moz-background-size: cover;
o-background-size: cover;
background-size: cover;
}
#menu {background:rgba(0, 0, 0, 0.2)!important;}
* {color:white!important;}
</style>
<?php endwhile; ?>
<? get_footer(); ?>
Using wp_attachment_is_image to make sure you're getting only your images and wp_get_attachment_image_src with the attachment id and "full" as parameters you can get the url for the full-size attachment image from the first element of the returned array (haven't tested this but it seems viable).
For those interested in the final script with #"Edgar Allan Pwn" suggestion incorporated it is below:
<?php
/**
* Template Name: Fullscreen Random
*/
get_header();
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<style media="screen" type="text/css">
body {
background: url(<?php
$args = array(
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) { ?>
<?php $full = wp_get_attachment_image_src( $attachment->ID, 'full', false, false );
echo $full[0];
}
}
?>) no-repeat center center fixed;
webkit-background-size: cover;
moz-background-size: cover;
o-background-size: cover;
background-size: cover;
}
#menu {background:rgba(0, 0, 0, 0.2)!important;}
* {color:white!important;}
</style>
<?php endwhile; ?>
<? get_footer(); ?>
In wordpress (3.4) I created some code which returns an alphabetical list/index of custom tags and filters the custom posts in a grid layout. The tags are named as "tagdirectory". The custom post is named "directory".
This is the code:
<?php $list = '';
$tags = get_terms( 'tagdirectory' );
echo '<ul id="portfolio-filter">';
echo'<li>All</li>';
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li>'.$name.'</li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
echo "</ul>";
?>
This works perfectly but I would also like for empty letters from the alphabet to be shown.
For example, now it returns:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
F
Fitszimmons Maureen
... and so on
But it doesn't show the empty letter groups because there are no tags starting with this letter. I do need it to show the capital letter for empty groups though, like so:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
C
D
E
F
Fitszimmons Maureen
G
... and so on
Can anybody help me and tell me what code I should add for this to work?
Thanks!
I have just edited this solution> http://wordpress.mcdspot.com/2010/12/03/template-to-list-posts-by-first-letter-of-title/
I got it to display a custom post type by changing the 'post-type' on line #37 to the correct custom taxonomy ('distributors' as I am using to list companies by name) and it's done the trick.
Here is the code:
<?php
/*
Template Name: A-Z Pages
A WordPress template to list page titles by first letter.
You should modify the CSS to suit your theme and place it in its proper file.
Be sure to set the $posts_per_row and $posts_per_page variables.
*/
$posts_per_row = 3;
$posts_per_page = 15;
?>
<?php get_header(); ?>
<style type="text/css">
.letter-group { width: 100%; }
.letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
.row-cells { width: 70%; float: right; margin-right: 180px; }
.title-cell { width: 30%; float: left; overflow: hidden; margin-bottom: 8px; }
.clear { clear: both; }
</style>
<div id="main-background">
<div id="main-column">
<h1><?php the_title(); ?></h1>
<div class="margin-top"></div>
<div id="a-z">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'posts_per_page' => $posts_per_page,
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) {
$in_this_row = 0;
while ( have_posts() ) {
the_post();
$first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
if ($first_letter != $curr_letter) {
if (++$post_count > 1) {
end_prev_letter();
}
start_new_letter($first_letter);
$curr_letter = $first_letter;
}
if (++$in_this_row > $posts_per_row) {
end_prev_row();
start_new_row();
++$in_this_row; // Account for this first post
} ?>
<div class="title-cell"><?php the_title(); ?></div>
<?php }
end_prev_letter();
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Higher Letters') ?></div>
<div class="alignright"><?php previous_posts_link('Lower Letters »') ?></div>
</div>
<?php } else {
echo "<h2>Sorry, no posts were found!</h2>";
}
?>
</div><!-- End id='a-z' -->
</div><!-- End class='margin-top -->
</div><!-- End id='rightcolumn' -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php
function end_prev_letter() {
end_prev_row();
echo "</div><!-- End of letter-group -->\n";
echo "<div class='clear'></div>\n";
}
function start_new_letter($letter) {
echo "<div class='letter-group'>\n";
echo "\t<div class='letter-cell'>$letter</div>\n";
start_new_row($letter);
}
function end_prev_row() {
echo "\t</div><!-- End row-cells -->\n";
}
function start_new_row() {
global $in_this_row;
$in_this_row = 0;
echo "\t<div class='row-cells'>\n";
}
?>