Wordpress tag auto close - wordpress

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.

Related

How to override a function in parent WordPress theme

I am building a child theme using Flaxseed as a parent. This theme includes several 'content styles' which can be selected using Theme > Customizer. Each of these styles has a function in the parent functions.php file that renders the CSS and wordpress content into one. I want to edit some of the Loop functions that are in here, specifically updating the_excerpt to the_content, but I want to do so in a way that works with my child theme.
Since this code is in the parent functions.php, and since the theme customizer calls it specifically by name, I cannot simply add a new function, and I cannot override it using the same function name.
My best guess is that I need to somehow remove this function from the parent functions.php before loading a new function of the same name in my child function.php, but I cannot seem to figure out how.
Below is the code from the template file where the function is loaded into the template:
<?php for ($i = 1; $i < 8; $i++)
if (get_theme_mod('flaxseedpro_fa_'.$i) != 'none') {
flaxseedpro_display_featured_area(
get_theme_mod('flaxseedpro_fa_'.$i),
get_theme_mod('flaxseedpro_fa_title'.$i),
get_theme_mod('flaxseedpro_fa_cat'.$i)
);
}
?>
Here, the $i variable is the value that is set in the Theme > Customizer screen. This file and code could be modified easily as a part of the child theme.
Below are two code snips from the parent functions.php which select the appropriate featured area code:
function flaxseedpro_display_featured_area($style, $fa_title, $cat) {
if (is_home()) :
switch ($style) {
case 'carousel':
flaxseedpro_carousel($fa_title, $cat);
break;
case 'style1':
flaxseedpro_featured_style1($fa_title, $cat);
break;
case 'style2':
flaxseedpro_featured_style2($fa_title, $cat);
break;
case 'style3':
flaxseedpro_featured_style3($fa_title, $cat);
break;
case 'style4':
flaxseedpro_featured_style4($fa_title, $cat);
break;
default:
break;
}
endif;
}
which leads to several functions such as this:
function flaxseedpro_featured_style2($fa_title, $cat) {
?>
<div class="featured-style1 featured-style2">
<?php if ('' !== $fa_title) : ?>
<h2 class="featured-section-title container">
<?php echo esc_html($fa_title); ?>
</h2>
<?php endif; ?>
<div class="featured-posts-inner container">
<?php
$args = array(
'posts_per_page' => 5,
'cat' => $cat,
);
$fa_posts = new WP_Query($args);
if ($fa_posts->have_posts()) :
$counter = 0;
while ($fa_posts->have_posts()) :
$fa_posts->the_post();
$counter++;
if (1 === $counter) {
?>
<div class="feat-col1 md-6 sm-6">
<div class="md-12 pr-0 pl-0">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="item-container mb-3">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium-large', array('class' => 'featured-post-thumbnail-primary')); ?>
<?php else : ?>
<img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>">
<?php endif; ?>
</div>
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<div class="post-author">
<?php esc_html_e('By', 'flaxseed-pro'); ?> <?php the_author(); ?> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?>
</div>
<div class="entry-excerpt body-font mb-3"><?php the_excerpt(); ?></div>
</div>
<?php _e('Read More','flaxseed-pro') ?>
</article>
</div>
</div>
<?php
} else {
?>
<div class="feat-col2 md-6 sm-6">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="md-4 xs-4">
<div class="item-container">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium', array('class' => 'featured-post-thumbnail-secondary')); ?>
<?php else : ?>
<img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>">
<?php endif; ?>
</div>
</div>
<div class="md-8 xs-8">
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<br><small><?php esc_html_e('By', 'flaxseed-pro'); ?> <?php the_author(); ?> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></small>
</div>
</div>
</article>
</div>
<?php
}
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
<?php
}
This is the the code I've been trying to override but unable to find a solution as a part of the child theme. All of the answers I've found seem to require a hook, which I cannot discern from this code.
This is solved.
I changed the function name that is called in my template file (flaxseedpro_display_featured_area >> flaxseedpro_display_featured_area2), then redefined the switch function to replace 'style3' with a new 'style5'. Then added the new 'style5' function with updated CSS code.

Wy am I getting fatal error message with this code

I am using the genesis sample child theme and I getting the following error message: Fatal error: Cannot redeclare landing_do_content() (previously declared in XXX. I have reviewed all files and the function is not declared anywhere else.
`// Display content of flexible content layouts
add_action( 'genesis_entry_content', 'landing_acf_content' );
function landing_acf_content() {
if( have_rows('landing_page_content') ) { ?>
// Hero with text and button layout
if( get_row_layout() == 'hero_with_text_and_button' ) { ?>
<div class="landing-hero" style="background: url(<?php echo get_sub_field('hero_image'); ?>); background-size: cover;">
<div class="hero-content light dark">
<div class="wrap">
<h1 class="hero-title"><?php echo get_sub_field('headline'); ?></h1>
<p><?php echo get_sub_field('text'); ?></p>
<a class="button" href="<?php echo get_sub_field('url'); ?>"><?php echo get_sub_field('button'); ?></a>
</div>
</div>
</div>
<?php }
// Headline with WYSIWYG
else if( get_row_layout() == 'headline_with_wysiwyg' ) { ?>
<div class="heading-text" style="background-color: <?php echo get_sub_field('background_color'); ?>">
<div class="wrap">
<h2 class="plain-title"><?php echo get_sub_field('headline'); ?></h2>
<?php echo get_sub_field('content'); ?>
</div>
</div>
<?php }
<?php }
}`
If you are declaring a global function and not one inside a class, make sure to only call the file with this function only once, if you require or include the php file more than once, it will result in redeclaration error.

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)

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