First post in Wordpress blog displays incorrectly - wordpress

I am styling a wordpress theme and the top blog post always displays incorrectly. The code is here:
<div class="bordered centered">
<p class="negative-margin alignleft header"><?php the_title(); ?></p><p class="negative-margin alignright date"><?php the_date(); ?></p><div style="clear:both;"></div>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php if ( has_post_thumbnail() ):?>
<div id="entry-left"><?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail($post->ID, 'large');
echo '</a>';?>
</div>
<div class="entry-right">
<?php the_content( __( 'More <span class="meta-nav">→</span>', 'huckleberry' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'huckleberry' ), 'after' => '</div>' ) ); ?>
</div>
<div class="entry-right">
<table>
<tr>
<td><p class="header">Details</p><br />
<?php $my_meta = get_post_meta( $post->ID, 'link', false ); ?>
<?php if ($my_meta): var_dump($my_meta) ?>
<?php foreach ($my_meta as $currMeta): ?>
<?php if( $currMeta && '' != $currMeta ): $currMeta=explode($currMeta,";",2);?>
<?php $my_meta[0];?>
<?php endif; endforeach; endif; ?>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?>
</a>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'tertiary-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'tertiary-image'); endif; ?>
</a>
</td>
<td>
<a href="<?php echo MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'fourth-image');?>">
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'fourth-image'); endif; ?>
</a>
<td>
</tr>
</table>
</div>
with the relevant CSS here:
.alignleft {
float:left;
}
.alignright {
float:right;
}
.bordered {
border:2px solid black;
}
.negative-margin {
margin-top:-25px;
margin-left:5px;
background:white;
}
.header {
font-size:200%;
}
.date {
font-size:150%;
}
.centered {
width:640px;
margin-left:auto;
margin-right:auto;
}
#entry-left {
float:left;
width:200px;
}
.entry-right {
float:right;
width:430px;
}
All the other posts have the border, with the title and date on the border, but the top post has a border that goes over the header, like so: http://imgur.com/6pE79ro
I'm relatively new to Wordpress and can't seem to figure out what's going on here.
Also, I'm trying to use Custom Fields to display links under "details." The idea is to have the key for the links always be "link", and the value to be in the format "description;url" so that I can break the value into two values to have a link description and URL display. I'm unable to get that to actually display any links. Any help would be greatly appreciated.

Perhaps irrelevant, but where do you close the following divs? <div class="bordered centered"> and <div id="entry-left">?
This is not exactly answering your second questions, but as for custom fields: I would suggest using a great WP module - ACF. It allows you to create and read values of custom-created fields easily, coming with many more useful features. The plugin can be found here: http://wordpress.org/extend/plugins/advanced-custom-fields/

Related

How to scroll to post link in Wordpress

My 'hero' element is taking most of the top page, and an user would have to manually scroll past it to get to the content.
I want to change it so that clicking the links will scroll past the image and down to the posts title. At the minute, clicking the post reloads the page and the hero element is on top. But if you click 'more' tag, it scrolls nicely.
How do I make it so that clicking the link will scroll the page down in Wordpress? I don't mean 'more' tag. Maybe there is a way to update the link functions in WP so the links will create anchor like 'more' tag?
I haven`t got a code that creates a link, as they are created by WP (they are post links).
<div class="big">
</div>
<article><div class="post">
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
</div></article>
.big {
height: 1200px;
width: 900px;
background-color: grey;
}
JS Fiddle: https://jsfiddle.net/tvue1mwo/
single.php code:
<?php
if (is_single()) {
// Currently using get_header('posts'), so I can hide hero element by css and unhide it with js
get_header('posts');
// If I understand right, here should go the ANCHOR link?
}
else {
// Loads normal hero withou extra css class
get_header();
}
?>
<div class="main-section wrapper">
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="post-content u-cf">
<h2 class="post"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h2>
<p class="post-info"><i class="fa fa-folder-open" aria-hidden="true"></i>
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories) {
foreach ($categories as $category) {
$output .= '' . $category->cat_name . '' . $separator;
}
echo trim($output, $separator);
}
?>
|
<i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_time('j/m/Y'); ?>
</p>
<div class="banner-image"><?php the_post_thumbnail('banner-image'); ?></div>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<?php
else :
echo '<p> No Content found</p>';
endif; ?>
</div>
<?php get_footer(); ?>
index.php:
<?php
if (have_posts()) :?>
<?php $count = 1;
while (have_posts()) : the_post(); ?>
<div class="post-content u-cf">
<?php if (has_post_thumbnail()) {
?>
<div class="post-thumbnail u-cf"><a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('small-thumbnail') ?></a>
</div>
<?php } ?>
<h2 class="post">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<p class="post-info"><i class="fa fa-folder-open" aria-hidden="true"></i>
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories) {
foreach ($categories as $category) {
$output .= '' . $category->cat_name . '' . $separator;
}
echo trim ($output, $separator);
}
?>
|<i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_time('j/m/Y'); ?>
</p>
<?php the_content(); ?>
<hr>
<?php if ( 2 === $count ) { ?>
<div class="main-content-advert">
<p>Lorem ipsum</p><p>Lorem impsum</p>
</div>
<hr>
<?php }
$count++; ?>
<?php endwhile; ?>
</div>
<?php
else :
echo '<p> No Content found</p>';
endif; ?>
If you are using index.php to display the homepage & archives, you can do the following:
<?php
/* 1. define the name of the anchor to jump to */
$anchorname = "skipheroimage";
$count = 0;
if (have_posts()) :
while (have_posts()) : the_post();
?>
<?php
$count++; /* increment the count so that each anchor is unique on the page */
/* 2. add the anchor to the permalink so it will jump directly to the anchor */
$linkwithanchor = get_permalink()."#".$anchorname.$count;
?>
<div class="post-content u-cf">
/* 3. use our $linkwithanchor variable to create the link */
<h2 class="post"><a href="<?php echo $linkwithanchor; ?>">
<?php the_title(); ?></a></h2>
/* no change to the code that was here, so keep it as it was...
... I just removed it to make my changes easier to find */
<div class="banner-image"><?php the_post_thumbnail('banner-image'); ?></div>
/* 4. add our anchor - this is where the link will jump to */
<a id="<?php echo $anchorname.$count; ?>" name="<?php echo $anchorname.$count; ?>"></a>
<?php the_content(); ?>
</div> /* NOTE - you had this in the wrong place. */
<?php endwhile; ?>
<?php
else :
echo '<p> No Content found</p>';
endif; ?>
This will create an anchor directly after the banner image, and add the anchor name to the link so that it will jump directly to it.
I have commented and numbered each step directly in the code.
You will need to do this for any templates you have that displays the links (e.g. archive.php)

ACF Image field in Repeater

I'm having trouble switching an ACF Image field from the default fullsize to the thumbnail image in the HTML output.
I have an Image type sub-field ID 'homepage_custom_navigation_image' within a Repeater field ID 'homepage_custom_navigation'. Any advice will be greatly appreciated.
Here's the code I have so far, which is displaying the fullsize image OK but making the download time of my page pretty long:
<div id="homepage-navigation-container">
<?php
$rows = get_field('homepage_custom_navigation');
if($rows)
{
foreach($rows as $row)
{
echo '<div class="homepage-navigation-item">
<div class="homepage-navigation-item-image">
<a href=' . $row['homepage_custom_navigation_link'] . '><img src=' . $row['homepage_custom_navigation_image'] . ' alt=' . $row['homepage_custom_navigation_title'] . '></a>
</div>
<div class="homepage-navigation-item-title">
<a href=' . $row['homepage_custom_navigation_link'] . '><h2>' . $row['homepage_custom_navigation_title'] . '</h2></a>
</div>
</div>';
}
}
?>
</div>
If you'd like to control the size of an image, I can show you how I normally output them using ACF.
<div id="homepage-navigation-container">
<?php
if(get_field('homepage_custom_navigation'))
{
while(has_sub_field){
}
foreach($rows as $row)
{
echo '<div class="homepage-navigation-item">
<div class="homepage-navigation-item-image">
<a href=' . get_sub_field('homepage_custom_navigation_link'). '>'. wp_get_attachment_image(get_sub_field('homepage_custom_navigation_image'), 'thumbnail'). '</a></div>
<div class="homepage-navigation-item-title">
<a href=' . get_sub_field('homepage_custom_navigation_link') . '><h2>' . get_sub_field('homepage_custom_navigation_title') . '</h2></a>
</div>
</div>';
}
}
?>
This is assuming that 'homepage_custom_navigation_title', 'homepage_custom_navigation_link', and 'homepage_custom_navigation_image' are all subfields within your repeater. The key is to using wp_get_attachment_image. This will use the WordPress function, that creates an image based on the ID and size value input in the parameters. 'Thumbnail' is one of the WordPress default sizes though they can be tweaked and customized in your functions.php file.
Thanks everyone for your help a guidance. I took a bit of info from each of your suggestions and came up with this as an answer:
<!-- Homepage custom navigation here -->
<div id="homepage-navigation-container">
<?php if( have_rows('homepage_custom_navigation') ): ?>
<?php while( have_rows('homepage_custom_navigation') ): the_row();
// vars
$thumb = wp_get_attachment_image_src(get_sub_field('homepage_custom_navigation_image'), 'thumbnail');
$desc = get_sub_field('homepage_custom_navigation_title');
$link = get_sub_field('homepage_custom_navigation_link');
?>
<div class="homepage-navigation-item">
<div class="homepage-navigation-item-image" style="background-image: url(<?php echo $thumb[0]; ?>); background-size: 120px auto; background-repeat: no-repeat; ">
</div>
<div class="homepage-navigation-item-title">
<h2><?php echo $desc;?></h2>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Try this:
<div id="homepage-navigation-container">
<?php if( have_rows('homepage_custom_navigation') ): ?>
<?php while( have_rows('homepage_custom_navigation') ): the_row();
// vars
$gal = get_sub_field('homepage_custom_navigation_image');
$desc = get_sub_field('homepage_custom_navigation_title');
$url = $gal['url'];
$title = $gal['title'];
$alt = $gal['alt'];
$size = 'thumbnail';
$thumb = $gal['sizes'][ $size ];
?>
<div class="homepage-navigation-item">
<div class="homepage-navigation-item-image">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" /></div>
<div class="homepage-navigation-item-title">
<?php echo $desc;?></h2>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
Just make sure that in "Return Value" you have selected "Image Object" and it should work (I have added alt for proper HTML5 and you can even add the image size if you want)

Wordpress tag auto close

Wordpress corrects the html markup in posts and also in template specific files.
I'm working on a template and what I'm trying to achieve is wrap a div in <a href"#"></a> tag.
Before the foreach loop there is placed, then some more php and html markup and right before the foreach loop ends I paste in the ending tag .
What wordpress doeas it automatically adds the ending tag right after a href string tag, removes the correctly placed ending tag making the link empty.
He're is what I type:
<li <?php if (get_option('square_portfolio_filter') != "true") { ?> data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } ?>" <?php ; } ?>>
<a href="#">
<div class="thumbs-animate">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(255,191), array('title' => ''.get_the_title().''));
}
?>
<div class="details">
<h5><?php the_title(); ?></h5>
<p><?php content('15'); ?></p>
<a href="<?php the_permalink(); ?>">
<?php
if(get_option('square_view_project_link')!="") {
echo get_option('square_view_project_link');
}else {
_e( 'View Project', 'square_lang' );
}
?>
</div>
</div>
</a>
</li>
And here is what Wordpress ends up saving:
<li <?php if (get_option('square_portfolio_filter') != "true") { ?> data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } ?>" <?php ; } ?>>
<div class="thumbs-animate">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(255,191), array('title' => ''.get_the_title().''));
}
?>
<div class="details">
<h5><?php the_title(); ?></h5>
<p><?php content('15'); ?></p>
<a href="<?php the_permalink(); ?>">
<?php
if(get_option('square_view_project_link')!="") {
echo get_option('square_view_project_link');
}else {
_e( 'View Project', 'square_lang' );
}
?>
</div>
</div>
</li>
I temporarily fixed the issue using some javascript code but its neither good practice nor what I want to have:
onclick="location.href='<?php the_permalink();?>';" style="cursor:pointer;"
Just wanted to comment that wrapping a block-level element is inside an inline element is perfectly fine in HTML 5.
<div>...</div> is valid html 5.
<div>...</div> is invalid HTML. You're not supposed to have a block-level element (div) inside of an inline element (a). WordPress is likely trying to protect you from that.
I can't tell if using # as your href is part of the actual code or just something you did for simplification, but if that is the actual code and you're attaching an onclick or something to the a, you could instead attach that to the div and it would work just as well.

Wordpress, Alternating float on featured image

I want each loop on a specific category to alternate float on the "featured image". So that each new post the "featured image" change float (right side, left side).
I use folowing code to pull odd or even classes on each post,
<?php query_posts('showposts=5&cat=5,'); if (have_posts()) : ?>
<?php $c = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class((++$c % 2 === 0) ? 'odd' : 'even'); ?>>
<h2 class="entry-title"><a>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="featuredImage">
<?php the_post_thumbnail('medium'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endif; wp_reset_query();?>
Then I was trying to change the float on the "featured image" with css on the different post classes (odd or even). Se example,
.even { width:650px; height:250px; background-color: #000;}
.odd{ width:650px; height:250px; background-color: #616161;}
.odd, .featuredImage{ float:left;}
.even, .featuredImage{ float:right;}
My example, http://fskador.se/myPerformanceLast/?page_id=49
But cant get it to work!! please help!
Incorrect
<h2 class="entry-title"><a>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
Corrected
<h2 class="entry-title"><a title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
CSS
Incorrect
.odd, .featuredImage{ float:left;}
.even, .featuredImage{ float:right;}
Corrected
div.odd .featuredImage{ float:left;}
div.even .featuredImage{ float:right;}
Or
.odd .featuredImage{ float:left;}
.even .featuredImage{ float:right;}

PHP array displays values within HTML tags but not in CSS selectors (Wordpress)?

I'm following this tutorial to create custom Wordpress options
the function within theme/functions/admin-menu.php which changes the background color:
// Color Scheme
function color_scheme_setting() {
$options = get_option('plugin_options');
$items = array("Red", "Green", "Blue");
echo "<select name='plugin_options[color_scheme]'>";
foreach ($items as $item) {
$selected = ( $options['color_scheme'] === $item ) ? 'selected = "selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
}
header.php:
<style>
body {
background: <?php echo $options['color_scheme']; ?>
}
</style>
</head>
<body <?php body_class(); ?>>
<div id="wrapper">
<div id="header">
<h1>
<?php bloginfo( 'name' ); ?>
</h1>
<?php $options = get_option('plugin_options'); ?>
<h2> <?php echo $options['banner_heading']; ?> </h2>
<img src="<?php echo $options['logo']; ?>" alt='' />
<p><?php echo $options['color_scheme']; ?></p>
<div id="lang">
<?php do_action('icl_language_selector'); ?>
<?php _e( 'english', 'starkers' ); ?>
</div>
<ul id="nav">
<?php wp_list_pages('title_li='); ?>
</ul>
When I select "green" in the dashboard, $options['color_scheme'] in the background selector doesn't appear.
(but it does appear in $options['color_scheme'] inside the <p> tags
Any suggestions to make this array work in the background selector?
(I'm using Wordpress 3.03)
EDIT:
I just tried this:
<p style="color: <?php echo $options['color_scheme']; ?>"><?php echo $options['color_scheme']; ?></p>
and the <p> tag changed its color
Why it doesn't work between the <style> tags?
The $options variable is only initialized after your <h1> element is rendered. Its value is not available yet when you want to use it in the <style> block.
You might want to initialize that variable earlier:
<?php $options = get_option('plugin_options'); ?>
<style>
body {
background: <?php echo $options['color_scheme']; ?>
}
</style>

Resources