Wordpress Shortcode in functions.php - php

I'm trying to create a shortcode to display blogs using this code in the functions.php file, I copied it from the blog template.
When I use the shortcode in a wordpress post, it isn't displaying the blogs as I want them to. Is it because of the php tags inside of the html?
function wpse_143641_homebox_shortcode( $atts ) {
return <<<HOMEBOX
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="container blog">
<div class="row">
<div class="col-md-8 col-md-push-2">
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=6' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_post_thumbnail(); ?>
<div class="entry-meta">
<?php neue_posted_on(); ?> by <?php the_author(); ?>
</div><!-- .entry-meta -->
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
</div> <!-- /.col-md-8 -->
</div> <!-- /.row -->
<?php wp_reset_postdata(); ?>
</div> <!-- /.container -->
</main><!-- #main -->
</div><!-- #primary -->
HOMEBOX;
}
add_shortcode( 'homebox', 'wpse_143641_homebox_shortcode' );

please try below code. it will help you
function wpse_143641_homebox_shortcode( $atts ) {
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="container blog">
<div class="row">
<div class="col-md-8 col-md-push-2">
<?php
global $wp_query;
$temp = $wp_query;
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=6' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_post_thumbnail(); ?>
<div class="entry-meta">
<?php //neue_posted_on(); ?> by <?php the_author(); ?>
</div><!-- .entry-meta -->
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php $wp_query = null; $wp_query = $temp; ?>
</div> <!-- /.col-md-8 -->
</div> <!-- /.row -->
<?php wp_reset_postdata(); ?>
</div> <!-- /.container -->
</main><!-- #main -->
</div><!-- #primary -->
<?php
}
add_shortcode( 'homebox', 'wpse_143641_homebox_shortcode' );

Related

Getting a page.php error when I create a new page in WordPress

<?php get_header(); ?>
</div><!--END CONTAINER-->
<?php
if (get_field('header_image')){
$image = get_field('header_image');
if (get_field('do_not_use_page_title')){
$title = get_field('alternate_page_title');
}else {
$title = get_the_title();
}
if (get_field('subheading')) $subheading = get_field('subheading');
?>
<div class="page-header" style="background-image: url(<?php echo $image['url']?>);">
<?php echo "<h1>$title</h1>"; ?>
<?php if ($subheading) { echo "<h2>$subheading</h2>";} ?>
</div>
<?
}
?>
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col-sm-8 clearfix" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<?php if (!(get_field('header_image'))) { ?>
<header>
<div class="page-header"><h1 class="page-title" itemprop="headline"><?php the_title(); ?></h1></div>
</header> <!-- end article header -->
<?php } ?>
<section class="post_content clearfix" itemprop="articleBody">
<?php the_content(); ?>
</section> <!-- end article section -->
</article> <!-- end article -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- end #main -->
</div> <!-- end #content -->
When creating a new page in WP I get an error
Parse error: syntax error, unexpected end of file in /home/example.com/example/public_html/wp-content/themes/exampletheme/page.php on line 69
I haven't been able to find any errors in the php that would trigger this parse error. Other pages that were already present on the website work fine, but making new pages and then viewing/previewing them brings an empty page with the above error. Any help would be appreciated, Thanks.

Paginate a category page of posts in Wordpress

Is it possible to add next and previous post links to a page that is querying posts from one category and displaying one post of that category per page?
This is what I currently have:
<?php query_posts('cat=2&posts_per_page=1'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="inner">
<div class="gallery" style="background-image: url(<?php the_field('image'); ?>);">
<div class="close" data-home="<?php echo home_url(); ?>">
<span class="oi" data-glyph="x"></span>
</div>
</div>
<div class="copy">
<h2><?php the_title(); ?></h2>
<?php the_field('news_content'); ?>
Next
</div>
</div>
</article>
<!-- /article -->
<?php endwhile; ?>
You can try with next_post_link()
<?php next_posts_link(); ?>
This seemed to be the only solution that worked for me, if anyone needs a template:
<?php get_header(); ?>
<main role="main">
<!-- section -->
<section>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="overlay" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="inner">
<div class="copy">
<h2><?php the_title(); ?></h2>
<?php the_field('news_content'); ?>
<?php the_field('copy'); ?>
<br>
<br>
<?php next_post_link( '%link', 'Next', TRUE, 'post_format' ); ?> | <?php previous_post_link( '%link', 'Previous', TRUE, 'post_format' ); ?>
</div>
</div>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</article>
<!-- /article -->
<?php endif; ?>
</section>
<!-- /section -->
</main>
<?php get_footer(); ?>

Display wordpress loop in two columns using bootstrap grid

Hi I need to modify my WP theme to make it show one post in two columns.
like this :
but when I add a second post it gets like this :
This is current code of the blog section custom php file, I can do a wipeout and forget this code and start from scratch, i've tried but I cannot make it work... Really appreciate any hint/ suggestion...I'm not a specialist, i'm learning
<div class="container content">
<div class="row blog-page">
<div class="col-md-9 md-margin-bottom-40">
<!--Blog Post-->
<div class="row blog blog-medium margin-bottom-40">
<?php // Display blog posts on any page # http://m0n.co/l
$category_id = get_cat_ID('Blog');
$catquery = new WP_Query( 'cat=' .$category_id. '&posts_per_page=5' . '&paged='.$paged );
while ($catquery->have_posts()) : $catquery->the_post(); ?>
<div class="col-md-5">
<?php
the_post_thumbnail('medium') ;
?>
</div>
<div class="col-md-7">
<h2><?php the_title(); ?></h2>
<?php the_excerpt();
?>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
<hr class="margin-bottom-40">
<!--End Blog Post-->
<!--End Blog Post-->
</div>
<!-- Right Sidebar -->
<?php get_sidebar() ; ?>
<!-- End Right Sidebar -->
</div><!--/row-->
</div><!--/container-->
thank you in advance for your help
Please replace this code with your code and display output like image.
<div class="container content">
<div class="row blog-page">
<div class="col-md-12 md-margin-bottom-40">
<!--Blog Post-->
<div class="row blog blog-medium margin-bottom-40">
<?php
// Display blog posts on any page # http://m0n.co/l
$category_id = get_cat_ID('Blog');
$catquery = new WP_Query('cat=' . $category_id . '&posts_per_page=5' . '&paged=' . $paged);
while ($catquery->have_posts()) : $catquery->the_post();
?>
<div class="col-md-2">
<?php
the_post_thumbnail('medium');
?>
</div>
<div class="col-md-4">
<h2><?php the_title(); ?></h2>
<?php the_excerpt();
?>
</div>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
<hr class="margin-bottom-40">
<!--End Blog Post-->
<!--End Blog Post-->
</div>
<!-- Right Sidebar -->
<?php get_sidebar(); ?>
<!-- End Right Sidebar -->
Please check and let me know.

wp_loop results in one conteiner but in two divs?

I have extra field in my template, it looks like:
" >
<div class="entry-content clearfix">
<div class="col-left">
<?php the_content(); ?>
</div>
<div class="col-right">
<?php
if(function_exists('get_field')) {
$sc = get_field('second_column');
echo apply_filters( the_post_thumbnail( 'thumbnail' ), $sc );
}
?>
</div>
</div><!-- .entry-content -->
</div><!-- #post-## -->
It should be like that: col-left displaying posts (recent 10) and in col-right displaying thumnail to each post.
How I should put while, to have all content inside this template? Sitting a while on it, and no idea for it. The best, but not working as I need, is:
<?php if ( $category_posts->have_posts() ) ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content clearfix">
<div class="col-left">
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="col-right">
<?php
while ( $category_posts->have_posts() ) : $category_posts->the_post();
if(function_exists('get_field')) {
$sc = get_field('second_column');
echo apply_filters( the_post_thumbnail( 'thumbnail' ), $sc );
}
?>
</div>
</div><!-- .entry-content -->
</div><!-- #post-## -->
Tomu, if you want to run loop twice on same page than you have to use <?php rewind_posts(); ?>. You can try below code.
<?php if ( $category_posts->have_posts() ) ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content clearfix">
<div class="col-left">
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="col-right">
<?php rewind_posts(); ?>
<?php
while ( $category_posts->have_posts() ) : $category_posts->the_post();
if(function_exists('get_field')) {
$sc = get_field('second_column');
echo apply_filters( the_post_thumbnail( 'thumbnail' ), $sc );
}
?>
</div>
</div><!-- .entry-content -->

"Previous Posts" is Not Working

For some reason, when you click "Previous Posts" on my wordpress blog, the page URL changes, but the first ten posts are displayed again.
You can see the issue here: http://onedirectionconnection.com/ and then page two http://onedirectionconnection.com/page/2/#sthash.0SQiq9AP.dpbs (that's another thing - I'm not sure why that code is being added at the end of the following page's URL)...
Anyway, here is the code I'm using for my front page template saved in a file called front-page.php
<?php
/*
Template Name: Splash Page
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="section-container">
<h1 class="section-title">Latest News</h1>
</div>
<?php $my_query = new WP_Query('showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h1 class="entry-title bottommargin big">
<?php the_title(); ?>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<div class="row">
<div class="col-md-12">
<footer class="row no-margin">
<div class="col-md-3 meta-entry">
Author: <br>
<?php the_author_link(); ?>
</div>
<div class="col-md-3 meta-entry">
Posted On:<br>
<?php the_time('F j, Y'); ?>
</div>
<div class="col-md-3 meta-entry">
Categorized:<br>
<?php echo get_the_category_list(', '); ?>
</div>
<div class="col-md-3 meta-entry-right">
Discussion:<br>
<?php comments_number(); ?>
</div>
</footer>
</div>
</div>
<?php endwhile; ?>
<div class="section-container">
<h1 class="section-title">More News</h1>
</div>
<?php
$custom_query = new WP_Query(array(
'posts_per_page' => 10,
'offset' => 1,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="row topmargin">
<div class="col-md-3 no-padding center">
<?php the_post_thumbnail('thumbnail', array('class' => 'img-thumbnail img-responsive')); ?>
</div>
<div class="col-md-9">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_excerpt(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'professional1d' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
</div>
</div>
<footer class="row no-margin">
<div class="col-md-3 meta-entry">
Author: <br>
<?php the_author_link(); ?>
</div>
<div class="col-md-3 meta-entry">
Posted On:<br>
<?php the_time('F j, Y'); ?>
</div>
<div class="col-md-3 meta-entry">
Categorized:<br>
<?php echo get_the_category_list(', '); ?>
</div>
<div class="col-md-3 meta-entry-right">
Discussion:<br>
<?php comments_number(); ?>
</div>
</footer>
<?php endwhile; // end of the loop. ?>
<div class="center">
<?php posts_nav_link(); ?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Everything seems pretty standard so I really have no clue what the issue is. If anyone could help me out with this issue, it would be greatly appreciated!
After comparing two - three post/pages of your blog. i think, it add the # tag at the end of url. this happen only after loading the entire page. which means it is added by one of your plugin. the plugin may be for "load fresh content from the server instead of the local browser cache"
so first deactivate the plugin you installed for this kind of features

Categories