How to scroll to post link in Wordpress - 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)

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.

Why if loop is causing unexpected end of file (code attached) (Wordpress)

This code is causing
Parse error: syntax error, unexpected end of file in C:\wamp64\www\pss\wp-content\themes\pss-theme\index.php on line 18
<?php
get_header();
if (have_posts()) :
$count = 1;
while (have_posts()) : the_post();
get_template_part ('content');
get_sidebar();
get_footer();
?>
But if I move if, count and while from index.php,
<?php
get_header();
get_template_part ('content');
get_sidebar();
get_footer();
?>
to the top of the content template part like this:
<?php if (have_posts()) :
$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">
<?php the_title(); ?></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++; ?>
</div>
<?php endwhile; ?>
<?php
else :
echo '<p> No Content found</p>';
endif;
?>
</article>
</div>
The error is not present.
Now, I understand that it has got something to do with the loop or the count?
That is completely normal, that error its expected because you are missing the end tags of the if and the while.
You cant have those in a different file, PHP expects the start and end to be in the same file.
Its not like its going to do the get_XXXX functions and create a big file with all those parts and then start with the content of them.
If you want to have the variable $count to be in the template part, move the end tags to your index, and have only the content in the content template, that is his purpose ;)
<?php
get_header();
if (have_posts()) :
$count = 1234;
while (have_posts()) : the_post();
get_template_part('template-parts/post/content');
endwhile;
else :
echo '<p> No Content found</p>';
endif;
?>
</article>
</div>
<?php
get_sidebar();
get_footer();
?>
then in content.php use global to get the variable that is not in scope:
<?php
global $count;
var_dump($count);//THIS WILL SHOW THE NUMBER 1234
?>
<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">
<?php the_title(); ?></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++;
?>
</div>
you can find more info here variable scope

How to link image in the article to image itself by default in WordPress

Is there anyway i can set the image link by default to the post itself?
I don't want to set the image link in the media up loader every time.
My code that gets content for post is this:
<?php if (!$is_paged && $archives == "false") { ?>
<?php woo_image('class=thumbnail&width='.get_option('woo_home_thumb_width').'&height='.get_option('woo_home_thumb_height')); ?>
<div class="post-title">
<h3><?php the_title(); ?></h3>
<p class="post-details"><?php _e('Posted on',woothemes); ?> <?php the_time('d. M, Y'); ?> <?php _e('by',woothemes); ?> <?php the_author_posts_link(); ?>.</p>
</div>
<?php } else { ?>
<?php woo_image('class=alignleft thumbnail&width='.get_option('woo_thumb_width').'&height='.get_option('woo_thumb_height')); ?>
<h2><?php the_title(); ?></h2>
<p class="post-details"><?php _e('Posted on',woothemes); ?> <?php the_time('d. M, Y'); ?> <?php _e('by',woothemes); ?> <?php the_author_posts_link(); ?>.</p>
<!-- <div class="comment-cloud">
<?php comments_number('0','1','%'); ?>
</div> -->
<?php } ?> <?php global $more; $more = 0;?>
<?php if ( get_option('woo_content') == "true" ) { the_content('Read More >>'); } else { the_excerpt();?><?php } ?>
</div>
<!-- Normal Post Ends -->
What i do is post an image with some lines below it and the insert a read more tag.
Is there a way i can use first image in the post and link to the article, just as the title does?
At top of the page
<?php
global $post;
global $wpdb;
?>
<a href="<?php echo get_permalink($post->ID); ?>" >
<img src="your_source_here" title="image" />
</a>
get_parmalink(); will take you to the detail page of the post respectively.

WP – Content code keeps subpage from appearing in sidebar

In one of my wordpress templates, the content code seems to keep the subpage menu from displaying in the sidebar. When ever I remove all code from the content div, the subpage menu appears as expected.
Could anyone point out where the code goes wrong?
Any help would be greatly appreciated!
<div id="content">
<div class="pageinfo clearfix">
<div class="info">
<h1><?php the_title();?></h1>
</div>
</div>
<?php
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
global $post;
$myposts = query_posts("category_name=intern&posts_per_page=3");
foreach($myposts as $post) :
setup_postdata($post);
?>
<div class="postitem clearfix">
<div class="new"></div>
<div class="info">
<h2><?php the_title()?></h2>
<?php the_date()?> | Skrevet af <?php the_author_posts_link(); ?> | Gemt under:
<?php
// heres just the name and permalink:
foreach((get_the_category()) as $category) {
$category_name = $category->category_nicename . ' ';
$category_link = get_category_link($category->cat_ID);
echo "<a href='$category_link'>$category_name</a>";
}
?>
</div>
<?php
$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];?>
<?php if($thumb != NULL) { ?>
<a href="<?php the_permalink() ?>">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumb ?>&h=110&w=110&zc=1" alt="<?php the_title(); ?>" width="110" height="110" class="leftImage" >
</a>
<?php } ?>
<?php the_content('<br />Læs resten her » '); ?>
<div class="clearBoth"></div>
</div><!-- /postitem -->
<?php endforeach; ?>
</div><!-- /content -->
try rewind_posts(); or wp_reset_query(); somewhere before your sidebar is loaded up (and after the endforeach).

Add links to taxonomy links area

I have to add a few links to where the taxonomy terms are displaying, I am using a custom module. I tried hook_link but it add links at the end of the node, How can I add links to the right side of the node title
Thank you very much
To extend Scott's answer:
you can still use your custom module with hook_link(), but you need to edit node.tpl.php or node-type.tpl.php.
i.e. the Garland node.tpl.php looks like:
<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><?php print $title ?></h2>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
<div class="content clear-block">
<?php print $content ?>
</div>
<div class="clear-block">
<div class="meta">
<?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>
</div>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>
what you need to do is move the <?php if ($links): ... block somewhere before <?php if ($submitted): ...
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><?php print $title ?></h2>
<?php endif; ?>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
...
then float both the title and the links block, for example.
you could theme nodes yourself (i.e., create your own node.tpl.php or 'node-type.tpl.php') and add whatever you want after the $terms variable (or anywhere).

Resources